我正在尝试为相当复杂的Java服务器编写上传系统。我在下面列出的两个小程序中重现了这个错误。基本上,我使用ObjectOutputStream / ObjectInputStream通过客户端/服务器进行通信。这是一项要求;我有数千行代码在这个ObjectOutputStream / ObjectInputStream设置下运行得非常好,所以我必须能够在上传完成后仍然使用这些流。
要访问文件(在客户端上读取的文件和正在服务器上写入的文件),使用FileInputStream和FileOutputStream。我的客户出现以完美运行;它读入文件并在每次迭代时发送一个不同的字节数组(它一次读取1MB,因此可以处理大型文件而不会溢出堆)。但是,在服务器上,似乎字节数组总是只发送第一个数组(文件的前1MB)。这不符合我对ObjectInputStream / ObjectOutputStream的理解。我正在寻求解决这个问题的工作方法,或者就此问题进行足够的教育,以形成我自己的解决方案。
以下是客户端代码:
import java.net.*;
import java.io.*;
public class stupidClient
{
public static void main(String[] args)
{
new stupidClient();
}
public stupidClient()
{
try
{
Socket s = new Socket("127.0.0.1",2013);//connect
ObjectOutputStream output = new ObjectOutputStream(s.getOutputStream());//init stream
//file to be uploaded
File file = new File("C:\\Work\\radio\\upload\\(Op. 9) Nocturne No. 1 in Bb Minor.mp3");
long fileSize = file.length();
output.writeObject(file.getName() + "|" + fileSize);//send name and size to server
FileInputStream fis = new FileInputStream(file);//open file
byte[] buffer = new byte[1024*1024];//prepare 1MB buffer
int retVal = fis.read(buffer);//grab first MB of file
int counter = 0;//used to track progress through upload
while (retVal!=-1)//until EOF is reached
{
System.out.println(Math.round(100*counter/fileSize)+"%");//show current progress to system.out
counter += retVal;//track progress
output.writeObject("UPACK "+retVal);//alert server upload packet is incoming, with size of packet read
System.out.println(""+buffer[0]+" "+buffer[1]+" "+buffer[2]);//preview first 3 bytes being sent
output.writeObject(buffer);//send bytes
output.flush();//make sure all bytes read are gone
retVal = fis.read(buffer);//get next MB of file
}
System.out.println(Math.round(100*counter/fileSize)+"%");//show progress at end of file
output.writeObject("UPLOAD_COMPLETE");//let server know protocol is finished
output.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
以下是我的服务器代码:
import java.net.*;
import java.io.*;
public class stupidServer
{
Socket s;
ServerSocket server;
public static void main(String[] args)
{
new stupidServer();
}
public stupidServer()
{
try
{
//establish connection and stream
server = new ServerSocket(2013);
s = server.accept();
ObjectInputStream input = new ObjectInputStream(s.getInputStream());
String[] args = ((String)input.readObject()).split("\\|");//args[0] will be file name, args[1] will be file size
String fileName = args[0];
long filesize = Long.parseLong(args[1]);
String upack = (String)input.readObject();//get upload packet(string reading UPACK [bytes read])
FileOutputStream outStream = new FileOutputStream("C:\\"+fileName.trim());
while (!upack.equalsIgnoreCase("UPLOAD_COMPLETE"))//until protocol is complete
{
int bytes = Integer.parseInt(upack.split(" ")[1]);//get number of bytes being written
byte[] buffer = new byte[bytes];
buffer = (byte[])input.readObject();//get bytes sent from client
outStream.write(buffer,0,bytes);//go ahead and write them bad boys to file
System.out.println(buffer[0]+" "+buffer[1]+" "+buffer[2]);//peek at first 3 bytes received
upack = (String)input.readObject();//get next 'packet' - either another UPACK or a UPLOAD_COMPLETE
}
outStream.flush();
outStream.close();//make sure all bytes are in file
input.close();//sign off
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
一如既往,非常感谢您的时间!
答案 0 :(得分:3)
您当前的问题是ObjectOutputStream
使用ID机制来避免多次在流上发送相同的对象。客户端将为buffer
的第二次和后续写入发送此ID,服务器将使用其缓存值。
这个直接问题的解决方案是添加对reset()的调用:
output.writeObject(buffer);//send bytes
output.reset(); // force buffer to be fully written on next pass through loop
除此之外,您通过在它们之上分层自己的协议来滥用对象流。例如,将文件名和文件大小写为由“|”分隔的单个字符串;只需将它们写为两个独立的值。同上每个写入的字节数。