使用Java套接字传输多个文件-管道中断异常

使用Java套接字传输多个文件-管道中断异常,java,sockets,tcp,pipe,Java,Sockets,Tcp,Pipe,我一直在尝试编写一个客户端/服务器应用程序,它使用Java套接字发送多个文件。我查看了似乎与此相关的每个线程,但我不明白为什么我发送文件的代码在写入套接字时抛出了一个断管异常。我真的需要一些帮助;我在阳光下试过各种方法,但我想不出来 编辑-谢谢大家!下面的代码工作得很好 发件人代码: long size; dos.writeInt(fileArray.length); //send every file in array for (File fileArray1 : fileArray) {

我一直在尝试编写一个客户端/服务器应用程序,它使用Java套接字发送多个文件。我查看了似乎与此相关的每个线程,但我不明白为什么我发送文件的代码在写入套接字时抛出了一个断管异常。我真的需要一些帮助;我在阳光下试过各种方法,但我想不出来

编辑-谢谢大家!下面的代码工作得很好

发件人代码:

long size;

dos.writeInt(fileArray.length);

//send every file in array
for (File fileArray1 : fileArray) {
    int bytesRead = 0;

    fis = new FileInputStream(fileArray1);

    //send filename                        
    dos.writeUTF(fileArray1.getName());

    //send file size (bytes)
    dos.writeLong(size = fileArray1.length());

    System.out.println("Size: " + size);

    //send file 
    try {
        while ((bytesRead = fis.read(buf)) != -1) {
            dos.write(buf, 0, bytesRead);
            publish(new Progress(null, (int) ((sentByteCount / totalByteCount) * 100)));
        }

        dos.flush();

    } catch (IOException ex) {
    System.out.println("ERROR!!!!");
    }

    //close file stream, has been sent at this point
    fis.close();

}

System.out.println("Done sending files");
dos.close();
clientSocket.close();
接收器代码:

while (true) {

    socket = serverSocket.accept();

    dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));

    //get number of files being received
    numFiles = dis.readInt();

    //read all files
    for (int i = 0; i < numFiles; i++) {

        filename = dis.readUTF();
        System.out.println("Receiving " + filename);

        size = dis.readLong();

        file = new File(filename);               

        fos = new FileOutputStream(filename);

        long total = 0;
        int count = 0;       

        while ((total < size) && ((count = dis.read(buf, 0, (int) Math.min(buf.length, size - total))) > 0)){
            fos.write(buf, 0, count);
            total += count;
        }

        fos.close();

        System.out.println("Received file " + filename);

    }


    dis.close();

}//end while
while(true){
socket=serverSocket.accept();
dis=新的DataInputStream(新的BufferedInputStream(socket.getInputStream());
//获取正在接收的文件数
numFiles=dis.readInt();
//读取所有文件
对于(int i=0;i0)){
fos.写入(buf,0,计数);
总数+=计数;
}
fos.close();
System.out.println(“接收文件”+文件名);
}
dis.close();
}//结束时

接收器中的这一行有一个重要的返回值

        dis.read(buf);
最可能的情况是,您只想使用
readFully()


此外,您不需要为接收器中的每次读取创建缓冲区,而是使用多参数读取方法。

编辑后,您的写入循环仍然是错误的。您正在使用以下语句在文件末尾写入垃圾邮件:

fos.write(buf);
编写该循环的正确方法如下所示:

long total = 0;
while (total < size && (count = in.read(buffer, 0, size-total > buffer.length ? buffer.length : (int)(size-total))) > 0)
{
    fos.write(buffer, 0, count);
    total += count;
}
fos.close();

调用
newfileoutputstream()
已经完成了所有这一切。你只是在重复操作系统已经做过的工作,不管怎样,它还是会做的

编辑您的帖子以包含完整的错误消息、堆栈跟踪和行。我已修复了异常,但我发送的文件仍然损坏。我已经验证了我收到的每个文件发送的字节数与我收到的相同,但它们只是无法读取…这解决了异常,谢谢!但我的测试。dmg仍然是不可数的。我在上面的接收器代码中编辑了while循环。我仍然感到困惑…@user3654725-请参阅EJP的答案,在调用
write()
时,您需要使用正确的长度。谢谢!我已经编辑了我的代码,这样其他人都可以看到什么是有效的。尽管如此,我仍然需要DataInputStream,因为该类包含特殊的读/写函数,例如不在BufferedInputStream类中的readUTF/writeUTF。
if (!file.exists())
    file.createNewFile();