title: Java TCP - Sending a file using DataOutputStream
body: <p>Hi and thanks in advance.</p>
  
  <p>So currently, I am trying to have my TCP program read a file from a directory (this is server side), and then send that file over to the socket that requested it (the client).</p>
  
  <p>Here is my code:</p>
  
  <p>Server side:</p>
  
  <pre><code>File FileList = new File(".....filepath....");
  Scanner scan = new Scanner(FileList);
  
  //idToFile just searching for the name of the file the client asked for
  String TheName = idToFile(Integer.toString(result));
  
  //Opens the chosen file such as an image to be used     
  File myImageFile = new File("....filepath...." + TheName);
  
  //sendResponse is a function with a writeUTF (but only works for strings)
  sendResponse("#OK");
  sendResponse(TheName);
  sendResponse(Long.toString(myImageFile.length()));
  
  //This line causes the error and says that it expected a string and not a file          
  output.writeUTF(myImageFile);
  
  
  private void sendResponse(String response)
      {
          try 
          {
              output.writeUTF(response);
          } 
          catch (IOException ex) 
          {
              ex.printStackTrace();
          }
      }
  </code></pre>
  
  <p>Client side:</p>
  
  <pre><code>ClientHandler client = new ClientHandler();
  
  
  //getResponse just catches the written strings, but it can't work with a file either
  String TheMessage = client.getResponse();
  String Name = client.getResponse();
  long FileSize = Long.parseLong(client.getResponse());
  </code></pre>
  
  <p>Here is the code for ClientHandler:</p>
  
  <pre><code>public class ClientHandler 
  {
      private static int port = 2016;
  
      private DataInputStream input;
      private DataOutputStream output;
  
      private Socket cSocket; 
  
  
      public ClientHandler()
      {
          cSocket = null;
  
          try
          {
              cSocket = new Socket("localhost", port); //connecting to the server 
              System.out.println("Client is connected on port " + port);
  
              output = new DataOutputStream(cSocket.getOutputStream());
              input = new DataInputStream(cSocket.getInputStream());
          }
          catch(IOException ex)
          {
              ex.printStackTrace();
          }
      }
  
      public void sendMessage(String message) //checking if the message is sent
      {
          try
          {
              output.writeUTF(message);
              output.flush();
              System.out.println("Message sent to the server");
          } 
          catch (IOException e) 
          {
              e.printStackTrace();
          }
      }
  
      public String getResponse()
      {
          String msg = "";
          try
          {
                msg = input.readUTF();
          }
          catch(IOException ex)
          {
              ex.printStackTrace(); 
          }
          return msg;
      }
  
  }
  </code></pre>
  
  <p>So how can I use a DataOutputStream to send any kind of file from my server to my client, and how can my client catch that file using DataInputStream and save it to disk?</p>
  
  <p>Thank you.</p>