我用TCP多线程上传图片,原图片内存400KB,但是传出来的图片居然是2G左右,请问这是什么原因?代码如下:
服务器端:
import java.io.*;
import java.net.*;
public class TCP_serverThreadSocket {
public static void main(String[] args) throws IOException {
ServerSocket ss=new ServerSocket(9999);
while(true){
Socket s=ss.accept();
Thread ts=new Thread(new Listenn(s));
ts.start();
}
}
}
class Listenn implements Runnable{
Socket socket=null;
public Listenn(Socket socket){
this.socket=socket;
}
public void run() {
try {
InputStream is = socket.getInputStream();
FileOutputStream fos=new FileOutputStream("D:\\JAVA Eclipse2\\新建文件夹\\new.jpg");
byte []buf2=new byte[1024];
int b=is.read(buf2);
while(b!=-1){
fos.write(buf2, 0, b);
}
socket.shutdownInput();
socket.close();
is.close();
fis.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
客户端:
import java.io.*;
import java.net.Socket;
import java.util.*;
//客户端
public class TCP_UploadDataSocket {
public static void main(String[] args)throws Exception {
Socket socket=new Socket("localhost",9999);
byte []buf=new byte[1024];
FileInputStream fis=new FileInputStream("D:\\JAVA Eclipse2\\新建文件夹\\source.jpg");
OutputStream os=socket.getOutputStream();
int len2=0;
while(( len2=fis.read(buf))!=-1){
os.write(buf, 0, len2);
}
socket.shutdownInput();
socket.close();
os.close();
}
}
相关分类