2016-04-11 4 views
2

私は約500kbの画像を送信しましたが、サーバには受信する画像は500kb以上です。私が作った問題?ネットワークの速度は約100kb/sです。サーバがクライアントからシードを持っている以上のデータを受信するのは奇妙です

クライアント

package client; 

import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.BufferedWriter; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.OutputStream; 
import java.io.OutputStreamWriter; 
import java.io.OutputStreamWriter; 
import java.net.Socket; 

public class client { 
    public static void main(String[] args) { 
     Socket s = null; 
     BufferedOutputStream bo = null; 
     BufferedInputStream bi = null; 
     try { 
      s = new Socket("127.0.0.1",12349); 
      bo = new BufferedOutputStream(s.getOutputStream()); 
      bi = new BufferedInputStream(new FileInputStream("1.jpg")); 
      byte [] bys =new byte[1024]; 
      while((bi.read(bys))!=-1){ 
       bo.write(bys); 
      } 
      bo.flush(); 
      System.out.println("already post the image"); 
      bi.close(); 

     } catch (IOException e) { 

     }finally{ 
      try { 
       s.close(); 
      } catch (IOException e) { 
       System.out.println("close failed"); 
      } 
     } 
    } 
} 

サーバー

package server; 

import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.net.ServerSocket; 
import java.net.Socket; 

public class sserver { 
    public static void main(String[] args) { 
     ServerSocket ss = null; 
     Socket s = null; 
     BufferedOutputStream bo = null; 
     BufferedInputStream bs = null; 
     try { 
      ss = new ServerSocket(12349); 
      s = ss.accept(); 
      bo = new BufferedOutputStream(new FileOutputStream("2.jpg")); 
      bs = new BufferedInputStream(s.getInputStream()); 
      byte [] bys =new byte[1024]; 
      while((bs.read(bys))!=-1){ 
       bo.write(bys); 
      } 
      bo.flush(); 
      System.out.println("upload success"); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     }finally{ 
      try { 
       ss.close(); 
      } catch (IOException e) { 
       System.out.println("close failed"); 
      } 
     } 

    } 
} 

答えて

1
問題は、あなたがバイト数を無視していることである

を読む:

while((bi.read(bys))!=-1){ 
    bo.write(bys); 
} 

これは、あなたが常に1024のバイトを書くことを意味しbiから1024バイト未満を読み取った場合でも、boとなります。

あなたが変数に読み込まれたバイト数を割り当て、書き込みコールにそれを渡す必要があります:

int bytesRead; 
while((bytesRead = bi.read(bys))!=-1){ 
    bo.write(bys, 0, bytesRead); 
} 
+0

は、それが実際に動作されて、どうもありがとうございました! –

0

あなたは

 int len= 0 ; 
     while((len = bi.read(bys))!=-1){ 
      bo.write(bys,0, len); 
     } 

以下同じコードのように受信exacteバイトを入力してくださいクライアント側とサーバー側の両方で変更が必要です。私は私の最後にテストし、両方のファイルがこの変更後に同じサイズを持っていることが判明しました

0

あなたのサーバーコードは、出力ストリームに常にbys配列の全長を書き込みます。

それを行うための適切な方法:

byte [] bys =new byte[1024]; 
while(true) { 
    int numRead = bs.read(bys); 
    if (numRead == -1) 
    break; 
    bo.write(bys, 0, numRead); 
} 
+0

これは正しい方法ではありません。コンパイルできません。 –

関連する問題