java中通过单套接字连接发送多个文件时出现EOF异常
我正在尝试通过java套接字发送多个文件。成功接收第一个文件后,它抛出EOFEException。我不知道出了什么问题。(所有文件均已成功从发送方发送) 发件人代码:java中通过单套接字连接发送多个文件时出现EOF异常,java,sockets,networking,serversocket,eofexception,Java,Sockets,Networking,Serversocket,Eofexception,我正在尝试通过java套接字发送多个文件。成功接收第一个文件后,它抛出EOFEException。我不知道出了什么问题。(所有文件均已成功从发送方发送) 发件人代码: sendToServer = new Socket(receiver,port); DataOutputStream out = new DataOutputStream(sendToServer.getOutputStream()); for(File f: file_to_send){
sendToServer = new Socket(receiver,port);
DataOutputStream out = new DataOutputStream(sendToServer.getOutputStream());
for(File f: file_to_send){
sendMessage = f.getName() + "\n"+ f.length() + "\n";
out.writeUTF(sendMessage);
FileInputStream requestedfile = new FileInputStream(f.getPath());
System.out.println("file path: "+f.getPath());
int count;
byte[] buffer = new byte[8192];
while ((count = requestedfile.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
System.out.println("File transfer completed!! voila! :)");
requestedfile.close();
}
out.close();
sendToServer.close();
System.out.println("File Count : " + fileCount);
for (int count =0; count<fileCount; count++){
String fileName = dis.readUTF();
int length = Integer.parseInt(fileName.split("\n")[1]);
fileName = fileName.split("\n")[0];
System.out.println("File Name : " + fileName);
System.out.println("Length : " + length);
System.out.println("File Data : ");
FileOutputStream fos = new FileOutputStream(new File(fileName));
int c;
byte[] buffer = new byte[8192];
while ((c = dis.read(buffer)) > 0)
{
fos.write(buffer, 0, c);
fos.flush();
}
fos.close();
System.out.println("\nFile received successfully!!! voila !! :)");
}
接收方代码:
sendToServer = new Socket(receiver,port);
DataOutputStream out = new DataOutputStream(sendToServer.getOutputStream());
for(File f: file_to_send){
sendMessage = f.getName() + "\n"+ f.length() + "\n";
out.writeUTF(sendMessage);
FileInputStream requestedfile = new FileInputStream(f.getPath());
System.out.println("file path: "+f.getPath());
int count;
byte[] buffer = new byte[8192];
while ((count = requestedfile.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
System.out.println("File transfer completed!! voila! :)");
requestedfile.close();
}
out.close();
sendToServer.close();
System.out.println("File Count : " + fileCount);
for (int count =0; count<fileCount; count++){
String fileName = dis.readUTF();
int length = Integer.parseInt(fileName.split("\n")[1]);
fileName = fileName.split("\n")[0];
System.out.println("File Name : " + fileName);
System.out.println("Length : " + length);
System.out.println("File Data : ");
FileOutputStream fos = new FileOutputStream(new File(fileName));
int c;
byte[] buffer = new byte[8192];
while ((c = dis.read(buffer)) > 0)
{
fos.write(buffer, 0, c);
fos.flush();
}
fos.close();
System.out.println("\nFile received successfully!!! voila !! :)");
}
接收文件的代码在读取第一个文件后不会停止,而是一直读取到流结束,将发送到同一文件的所有内容都写入 您需要跟踪已读取的字节数和/或仍需读取的字节数:
int remainingBytes = length;
while (remainingBytes > 0 && (c = dis.read(buffer, 0, Math.min(buffer.length, remainingBytes))) > 0)
{
remainingBytes -= c;
fos.write(buffer, 0, c);
// fos.flush(); this is not really necessary
}
顺便说一下,对文件长度使用
int
将您的文件限制为最多2GB。如果这是一个问题,请使用long
而不是int
切勿在循环内刷新。它消除了任何可能存在的输出缓冲的好处。