【发布时间】:2014-04-22 07:50:51
【问题描述】:
服务器端代码
int count=-1;
byte [] mybytearray = new byte [1024];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile));
os = s.getOutputStream();
System.out.println("Sending " + fileName + "(" + mybytearray.length + " bytes)");
while( (count=(bis.read(mybytearray)))>0) {
System.out.println(count);
os.write(mybytearray,0,count);
os.flush();
}
System.out.println("Done.");
bis.close();
os.close();
客户端代码
int bytesRead=-1;
byte [] mybytearray = new byte [1024];
InputStream isP = p2p.getInputStream();
FileOutputStream fos = new FileOutputStream(path+"RFC "+rfcNum+".txt");
while((bytesRead=isP.read(mybytearray))>0) {
fos.write(mybytearray, 0, bytesRead);
fos.flush();
fileSize+=bytesRead;
}
System.out.println("file Size is "+fileSize);
isP.close();
fos.close();
我正在从服务器到客户端获取一个空文件。即使在服务器上它正在打印不为零的字节数组大小。谁能帮我解决这个问题?
【问题讨论】:
-
@IstvanChung:也试过了。还是不行
-
不要在循环内刷新。它破坏了使用缓冲流的意义。
标签: java sockets file-transfer