我在下面发布的代码适用于套接字上的单个文件传输。但它不适用于套接字上的多个文件传输。当通过套接字尝试多个文件传输时,代码崩溃。如何通过byte []流读取多个文件的Socket InputStream?
我通过循环发送代码x次的服务器发送多个文件,并运行接收代码x次。当试图发送多个文件时,第一个文件将成功发送,第二个文件名和大小将被成功读取,但在此之后发生我代码中的错误。
在我的接收客户端,我尝试使用这里发布的建议:Java multiple file transfer over socket但没有成功。
错误在客户端。
我问的问题是:为什么不是这个代码适用于多个文件,我该如何解决它?
服务器发送
try{
byte[] bytes = new byte[(int)file.length()];
FileInputStream fis = new FileInputStream(file);
OutputStream os = socket.getOutputStream();
out.println(file.getName()); // Send Filename
out.println(file.length()); // Send filesize
int count;
while ((count = fis.read(bytes)) > 0) {
os.write(bytes, 0, count);
}
os.flush();
fis.close();
}catch(IOException e){
e.printStackTrace();
}
}
客户Recieving
try{
String file = in.readLine(); // Read filename
int fileSize = Integer.parseInt(in.readLine()); // Read Filesize
//ERROR HAPPENING ON LINE ABOVE IN LOOPS AFTER THE FIRST
byte [] buf = new byte [fileSize];
FileOutputStream fos = new FileOutputStream(file);
InputStream is = socket.getInputStream();
int count = 0;
while (fileSize > 0 && (count = is.read(buf, 0, (int)Math.min(buf.length, fileSize))) != -1){
fos.write(buf, 0, count);
fileSize -= count;
}
fos.close();
}catch(IOException e){
e.printStackTrace();
}
该错误是一个NumberFormatException异常,在当客户端接收文件的一部分之后的第一个用于将输入到档案大小环路。
当你说“在这之后我的代码错误发生”,还请添加错误在这里,所以我们不需要去猜测其中一百一些可能的错误之一是。 –
你有异常追踪吗? – efekctive
@ M.Prokhorov在我的文章的底部添加。 – tester