2014-03-07 62 views
0

我試圖將文件從客戶端複製到服務器,在Java中,像這樣:複製從客戶端文件到服務器

客戶:

public class Client { 

    public static void main(String[] args) throws Exception { 
     String fileName = "D:\\6282.mp3"; 

     try { 

     } catch (Exception e) { 
      Scanner scanner = new Scanner(System.in); 
      String file_name = fileName; 

      File file = new File(file_name); 
      Socket socket = new Socket("localhost", 3332); 
      ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); 
      ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); 

      oos.writeObject(file.getName()); 

      FileInputStream fis = new FileInputStream(file); 
      byte[] buffer = new byte[Server.BUFFER_SIZE]; 
      Integer bytesRead = 0; 

      while ((bytesRead = fis.read(buffer)) > 0) { 
       oos.writeObject(bytesRead); 
       oos.writeObject(Arrays.copyOf(buffer, buffer.length)); 
      } 

      oos.close(); 
      ois.close(); 
      System.exit(0); 
     } 

    } 

} 

服務器:

public class Server extends Thread { 

    public static final int PORT = 3332; 
    public static final int BUFFER_SIZE = 626; 

    @Override 
    public void run() { 
     try { 
      ServerSocket serverSocket = new ServerSocket(PORT); 

      while (true) { 
       Socket s = serverSocket.accept(); 
       saveFile(s); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    private void saveFile(Socket socket) throws Exception { 
     ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()); 
     ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); 
     FileOutputStream fos = null; 
     byte[] buffer = new byte[BUFFER_SIZE]; 

     // 1. Read file name. 
     Object o = ois.readObject(); 

     if (o instanceof String) { 
      fos = new FileOutputStream(o.toString()); 
     } else { 
      throwException("Something is wrong"); 
     } 

     // 2. Read file to the end. 
     Integer bytesRead = 0; 

     do { 
      o = ois.readObject(); 

      if (!(o instanceof Integer)) { 
       throwException("Something is wrong"); 
      } 

      bytesRead = (Integer) o; 

      o = ois.readObject(); 

      if (!(o instanceof byte[])) { 
       throwException("Something is wrong"); 
      } 

      buffer = (byte[]) o; 

      // 3. Write data to output file. 
      fos.write(buffer, 0, bytesRead); 

     } while (bytesRead == BUFFER_SIZE); 

     System.out.println("File transfer success"); 

     fos.close(); 

     ois.close(); 
     oos.close(); 
    } 

    public static void throwException(String message) throws Exception { 
     throw new Exception(message); 
    } 

    public static void main(String[] args) { 
     new Server().start(); 
    } 
} 

當我跑我得到:

run: 
BUILD SUCCESSFUL (total time: 0 seconds) 

但沒有真正發生。這是我第一次在Client-Server上工作,我不確定我的錯誤。

請幫忙。謝謝。

+2

你確定你的客戶端文件是正確的? try塊無所作爲似乎對我來說很腥。 –

+0

我試過不同的文件,但沒有任何反應。或者文件保存在哪裏?我看過localhost,這個項目文件,但沒有任何東西 – ILikeProgramming

+0

「我試圖從客戶端複製一個文件到服務器」,這行代表什麼意思?你是否試圖從「D:\\ 6282.mp3」獲取數據並在服務器控制檯上打印輸出? –

回答

1

在代碼中有些問題是:

對於客戶端,你在catch塊,除非出現異常時,將無法正常工作編寫的全部代碼。

您正試圖在此處傳遞文件的名稱而不是文件。

oos.writeObject(file.getName());

您需要運行服務器,然後運行客戶端。 這裏是一個示例工作代碼:

客戶:

public class Client { 

    public static void main(String[] args) throws Exception { 
     String fileName = "C:\\2048.jpg"; 

     try { 
      File file = new File(fileName); 
      Socket socket = new Socket("localhost", 3332); 
      byte[] mybytearray = new byte[(int) file.length()]; 
      BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); 
      bis.read(mybytearray, 0, mybytearray.length); 
      OutputStream os = socket.getOutputStream(); 
      os.write(mybytearray, 0, mybytearray.length); 
      os.flush(); 
      os.close(); 
     } catch (Exception e) { 
     } 

    } 
} 

服務器:

public class Server extends Thread { 

    public static final int PORT = 3332; 
    public static final int BUFFER_SIZE = 626; 

    @Override 
    public void run() { 
     try { 
      ServerSocket serverSocket = new ServerSocket(PORT); 
      while (true) { 
       Socket s = serverSocket.accept(); 
       saveFile(s); 
      } 
     } catch (Exception e) { 
     } 
    } 

    private void saveFile(Socket socket) throws Exception { 
     InputStream ois = socket.getInputStream(); 
     FileOutputStream fos = new FileOutputStream("C:\\2049.jpg");; 

     byte[] mybytearray = new byte[1024]; 
     System.out.println("Reading file from server..."); 
     BufferedOutputStream bos = new BufferedOutputStream(fos); 
     int bytesRead; 
     while ((bytesRead = ois.read(mybytearray)) != -1) { 
      bos.write(mybytearray); 
     } 

     bos.close(); 
     System.out.println("Writing file complete..."); 

    } 

    public static void main(String[] args) { 
     new Server().start(); 
    } 
} 
+0

感謝[@Sandeep](http://stackoverflow.com/users/2281800/sandeep)的響應。我試用了你的代碼,只是改變了'jpg'文件的名字。即使使用目的地,但仍然沒有。我只得到'跑: 建立成功(總時間:2秒)'。我可能會做錯什麼?請幫忙。 – ILikeProgramming

+0

@ user3189827嗨,你必須先運行服務器,然後運行客戶端。由於服務器是一個線程,它會一直運行,直到您停止運行,並且您發送的所有文件都將被寫入您提供的目錄(例如「C:\\ 2049.jpg」)。如果您在終端(cmd)上運行它,則可能需要在運行良好的終端上運行 – Sandeep

相關問題