2014-08-29 89 views
1

我正試圖從Android將文件發送到帶有java服務器的計算機終端。 我留下你寫下面的代碼來使用發送和接收文件,並考慮收到錯誤。通過套接字接收文件時出錯Android/Java

的Android(發件人客戶端):

   byte[] mybytearray = new byte[(int) selectedFile.length()]; 
       BufferedInputStream bis = new BufferedInputStream(new FileInputStream(selectedFile)); 
       bis.read(mybytearray, 0, mybytearray.length); 
       OutputStream os = socket2.getOutputStream(); 
       os.write(mybytearray, 0, mybytearray.length); 
       os.flush(); 

的Java(接收服務器):

    76)int lenghtf = Integer.parseInt(lenght); 
        77)byte[] mybytearray = new byte[lenghtf]; 
        78)InputStream is = socket.getInputStream(); 
        79) FileOutputStream fos = new FileOutputStream(namef); 
        80) BufferedOutputStream bos = new BufferedOutputStream(fos); 
        81) int bytesRead = is.read(mybytearray, 0, mybytearray.length); 
        82) bos.write(mybytearray, 0, bytesRead); 
        83)bos.close(); 

我得到這個錯誤

enter image description here

回答

1

接收機:

FileOutputStream out1 = new FileOutputStream("File Ricevuti\\"+namef); 
        byte[] buf = new byte[socket.getReceiveBufferSize()]; 
        int len = 0; 
        while ((len = inp.read(buf)) != -1) { 
         out1.write(buf, 0, len); 
         out1.flush(); 
         //out1.flush(); 
        } 

        out1.close(); 

發件人:

InputStream in = new FileInputStream(f); 

       byte[] buf = new byte[socket2.getSendBufferSize()]; 
       int len = 0; 
       while ((len = in.read(buf)) != -1) { 
        out.write(buf, 0, len); 

       } 
       in . close(); 
       out.close(); 
+1

這是我測試的工作! – 2014-08-30 09:16:13