title: Sending file through ObjectOutputStream and then saving it in Java?
body: <p>I have this simple Server/Client application. I'm trying to get the Server to send a file through an OutputStream (FileOutputStream, OutputStream, ObjectOutputStream, etc) and receive it at the client side before saving it into an actual file. The problem is, I've tried doing this but it keeps failing. Whenever I create the file and write the object I received from the server into it, I get a broken image (I just save it as a jpg, but that shouldn't matter). Here are the parts of the code which are most likely to be malfunctioning (all the seemingly un-declared objects you see here have already been declared beforehand):</p>
  
  <p>Server: </p>
  
  <pre><code>                ObjectOutputStream outToClient = new ObjectOutputStream(
                          connSocket.getOutputStream());
                  File imgFile = new File(dir + children[0]);
                  outToClient.writeObject(imgFile);
                  outToClient.flush();
  </code></pre>
  
  <p>Client: </p>
  
  <pre><code>ObjectInputStream inFromServer = new ObjectInputStream(
                  clientSocket.getInputStream());
          ObjectOutputStream saveImage = new ObjectOutputStream(
                  new FileOutputStream("D:/ServerMapCopy/gday.jpg"));
          saveImage.writeObject(inFromServer.readObject());
  </code></pre>
  
  <p>So, my problem would be that I can't get the object through the stream correctly without getting a broken file. </p>