2013-02-28 86 views
1

我的服务器正在向我的客户端发送几个文件。但是在客户端,它只接收第一个文件,因为我不知道如何迭代并获取第二个文件。Socket编程Java:如何从outputstream接收多个文件?

服务器发送这样的:

ListIterator iter = missingfiles.listIterator(); 
        //missingfiles contain all the filenames to be sent 
String filename; 
while (iter.hasNext()) { 
    // System.out.println(iter.next()); 
    filename=(String) iter.next(); 
    File myFile = new File("src/ee4210/files/"+filename); 
    byte[] mybytearray = new byte[(int) myFile.length()]; 

    FileInputStream fis = new FileInputStream(myFile); 
    BufferedInputStream bis = new BufferedInputStream(fis); 
    //bis.read(mybytearray, 0, mybytearray.length); 

    DataInputStream dis = new DataInputStream(bis);  
    dis.readFully(mybytearray, 0, mybytearray.length); 

    OutputStream os = _socket.getOutputStream(); 

    //Sending file name and file size to the server 
    DataOutputStream dos = new DataOutputStream(os);  
    dos.writeUTF(myFile.getName());  
    dos.writeLong(mybytearray.length);  
    dos.write(mybytearray, 0, mybytearray.length);  
    dos.flush(); 
} 

客户端收到这样的:(这只会收到第一个文件,我不知道如何使它循环到接收下一个文件)

int bytesRead; 
int current = 0; 
int filecount = 0; 
InputStream in; 
try { 
    in = _socket.getInputStream(); 
    DataInputStream clientData = new DataInputStream(in); 
     String fileName = clientData.readUTF(); 
     OutputStream output = new FileOutputStream(
           "src/ee4210/files/"+ fileName); 
     long size = clientData.readLong(); 
     byte[] buffer = new byte[1024]; 
     while (size > 0 
       && (bytesRead = clientData.read(buffer, 0, 
         (int) Math.min(buffer.length, size))) != -1) { 
      output.write(buffer, 0, bytesRead); 
      size -= bytesRead; 
     } 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
+1

“我不知道如何让它循环接收下一个文件” - 呃......只是在读取文件的行周围放一个'while' (从'String fileName = ...'开始,并在'catch(...)'块之前结束,也许可以从客户端添加一个sentinel值,告诉您它已经完成发送文件,或者让它发送文件数它会在传输开始时发送,或者只是关闭连接并让'while'循环继续,直到发生这种情况。 – millimoose 2013-02-28 14:20:17

+0

当你在服务器上使用'DataOutputStream'时,你为什么不读取数据'DataInputStream'在客户端?我猜你实际读的内容可能最终会出现乱码,如果'DOS'没有像你想象的那样真正直接输出数组。 – millimoose 2013-02-28 14:22:50

+0

我不知道该怎么做。 new to Java。我现在该做什么? – Ferrino 2013-02-28 15:28:13

回答

0

如何从outputstream接收多个文件?

显而易见的答案是'一次一个,有额外的信息告诉你一个人停下来,另一个人开始'。最常用的技术是在文件之前发送文件大小,例如,作为一个长通过DataOutputStream.writeLong(),并在接收器改变你的阅读循环停止后,确切的很多字节,关闭输出文件,并继续外部循环读取下一个长或流的结束。

0

你可以试试这个。 我用一种懒惰的方法来检查所有3个文件的末尾都已收到。

int bytesRead; 
    int current = 0; 
    int filecount = 0; 
    InputStream in; 
    try 
    { 
     in = _socket.getInputStream(); 
     DataInputStream clientData = new DataInputStream(in); 

     while(true) 
     { 
      String fileName = clientData.readUTF(); 
      // will throw an EOFException when the end of file is reached. Exit loop then. 

      OutputStream output = new FileOutputStream("src/ee4210/files/"+ fileName); 
      long size = clientData.readLong(); 
      byte[] buffer = new byte[1024]; 
      while (size > 0 
        && (bytesRead = clientData.read(buffer, 0, 
          (int) Math.min(buffer.length, size))) != -1) 
      { 
       output.write(buffer, 0, bytesRead); 
       size -= bytesRead; 
      } 
      output.close(); 
     } 

    } 
    catch (EOFException e) 
    { 
     // means we have read all the files 
    } 
    catch (IOException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    }