Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Socket编程Java:如何从outputstream接收多个文件?_Java_Networking_Serversocket - Fatal编程技术网

Socket编程Java:如何从outputstream接收多个文件?

Socket编程Java:如何从outputstream接收多个文件?,java,networking,serversocket,Java,Networking,Serversocket,我的服务器正在向我的客户端发送几个文件。但是在客户端,它只接收第一个文件,因为我不知道如何迭代和获取第二个文件 服务器发送如下内容: ListIterator iter = missingfiles.listIterator(); //missingfiles contain all the filenames to be sent String filename; while (iter.hasNext()) { // System.out.p

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

服务器发送如下内容:

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

客户端的接收方式如下:(它只接收第一个文件,我不知道如何使其循环接收下一个文件)

如何从outputstream接收多个文件

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

你可以试试这个。 我使用了一种惰性方法来检查是否已收到所有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();
    }

“我不知道如何使它循环接收下一个文件”-嗯。。。在读取文件的行周围放置一个
,而
?(以
字符串文件名=…
开头,在
捕获(…)之前结束)
block。还可以从客户端添加一个sentinel值,告诉您发送文件已经完成,或者让它在传输开始时发送它将发送的文件数,或者关闭连接,让
循环继续,直到发生这种情况。并且看到您正在服务器上使用
DataOutputStream
,你为什么不在客户端用
DataInputStream
读取数据呢?我猜如果
DOS
没有像你想象的那样直接输出数组,你实际读取的数据可能会被弄乱。我不知道怎么做。对Java还是新手。我现在该怎么做?阅读关于网络和序列化/lea的教程rn在运行之前是否要进行漫游?首先尝试实现一个更简单的多消息协议,然后再考虑传输复杂对象。因此,这不是进行概念漫游的正确位置。
    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();
    }