1

我的服务器正在向我的客户端发送几个文件。但是在客户端,它只接收第一个文件,因为我不知道如何迭代和获取第二个文件。

服务器发送如下:

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();
}
4

2 回答 2

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();
    }
于 2013-03-01T12:04:11.700 回答
0

如何从输出流接收多个文件?

显而易见的答案是“一次一个,有额外的信息告诉你一个停止和另一个开始”。最常用的技术是在文件之前发送文件大小,例如作为 long via DataOutputStream.writeLong(),并在接收器更改您的读取循环以在恰好那么多字节后停止,关闭输出文件,并继续读取下一个长流或流尾。

于 2013-02-28T23:38:05.197 回答