简体   繁体   中英

Issue sending byte array over objectoutputstream in java

I'm trying to write an upload system for a fairly complex java server. I have reproduced the error in the two small programs listed below. Basically, I am using an ObjectOutputStream/ObjectInputStream to communicate via the client/server. This is a requirement; I have thousands of lines of code working perfectly fine around this ObjectOutputStream/ObjectInputStream setup, so I must be able to still use these streams after an upload is complete.

To access the files(the one being read on the client and the one being written on the server), FileInputStream and FileOutputStream is used. My client appears to be functioning perfectly; it reads in the file and sends a different byte array each iteration(it reads in 1MB at a time, so large files can be handled without overflowing the heap). However, on the server it appears as though the byte array is ALWAYS just the first array sent(the first 1MB of the file). This does not conform to my understanding of ObjectInputStream/ObjectOutputStream. I am seeking either a working solution to this issue or enough education on the matter to form my own solution.

Below is the client code:

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();
    }
  }
}

The following is my server code:

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();
    }
  } 
}

As always, many thanks for your time!

Your immediate problem is that ObjectOutputStream uses an ID mechanism to avoid sending the same object over the stream multiple times. The client will send this ID for the second and subsequent writes of buffer , and the server will use its cached value.

The solution to this immediate problem is to add a call to reset() :

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

That aside, you're misusing object streams by layering your own protocol on top of them. For example, writing the filename and filesize as a single string delimited by "|"; just write them as two separate values. Ditto for the number of bytes on each write.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM