1

我正在尝试为一个相当复杂的 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();
    }
  } 
}

一如既往,非常感谢您的宝贵时间!

4

1 回答 1

4

您的直接问题是ObjectOutputStream使用 ID 机制来避免通过流多次发送相同的对象。客户端将在第二次和后续写入时发送此 ID,buffer服务器将使用其缓存值。

这个直接问题的解决方案是添加对reset()的调用:

output.writeObject(buffer);//send bytes
output.reset(); // force buffer to be fully written on next pass through loop

除此之外,您通过在对象流之上分层您自己的协议来滥用对象流。例如,将文件名和文件大小写为由“|”分隔的单个字符串;只需将它们写为两个单独的值。每次写入的字节数同上。

于 2013-07-15T20:11:56.493 回答