【问题标题】:Server sending Empty file to client服务器向客户端发送空文件
【发布时间】: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


【解决方案1】:

我正在从服务器到客户端获取一个空文件。

使用该代码,服务器上的文件也必须为空。

即使在服务器上打印的字节数组大小不为零。

那是因为您将其初始化为“新字节[1024]”。它与文件的长度无关。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 2015-01-05
    相关资源
    最近更新 更多