title: Download file from server to client using socket in java
body: <p>I got some code to upload file from client to server and that working fine, the uploaded file saved in Server folder called Server, now would like to download file that exist in server folder to client folder but do not know how to do that.
  please give help</p>
  
  <p>this server code illustrate downloading file from client to server:</p>
  
  <pre><code>  public static void downloadFile() throws Exception{
      try {
          fileEvent = (FileEvent) inputStream.readObject();
          if (fileEvent.getStatus().equalsIgnoreCase("Error")) {
              System.out.println("Error occurred ..So exiting");
              System.exit(0);
          }
          String outputFile = fileEvent.getDestinationDirectory() + fileEvent.getFilename();
          if (!new File(fileEvent.getDestinationDirectory()).exists()) {
              new File(fileEvent.getDestinationDirectory()).mkdirs();
          }
  
          int filenamelength = dis.readInt();
          String filename = makeString(filenamelength, dis);
  
          dstFile = new File(path + filename);
          fileOutputStream = new FileOutputStream(dstFile);
          fileOutputStream.write(fileEvent.getFileData());
          fileOutputStream.flush();
          fileOutputStream.close();
          System.out.println("Output file : " + outputFile + " is successfully saved ");
          Thread.sleep(3000);
          System.exit(0);
  
      } catch (IOException e) {
          e.printStackTrace();
      } catch (ClassNotFoundException e) {
          e.printStackTrace();
      } catch (InterruptedException e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>and this is the client code to upload file from client to server:</p>
  
  <pre><code> public static void sendFile() throws Exception {
      dos.writeInt(6);
      dos.writeChars("upload");
  
      fileEvent = new FileEvent();
     String fileName = sourceFilePath.substring(sourceFilePath.lastIndexOf("/") + 1, sourceFilePath.length());
      String path = sourceFilePath.substring(0, sourceFilePath.lastIndexOf("/") + 1);
      fileEvent.setDestinationDirectory(destinationPath);
      fileEvent.setFilename(fileName);
      fileEvent.setSourceDirectory(sourceFilePath);
      File file = new File(sourceFilePath);
      if (file.isFile()) {
          try {
              DataInputStream diStream = new DataInputStream(new FileInputStream(file));
              long len = (int) file.length();
              byte[] fileBytes = new byte[(int) len];
              int read = 0;
              int numRead = 0;
              while (read &lt; fileBytes.length &amp;&amp; (numRead = diStream.read(fileBytes, read,
                      fileBytes.length - read)) &gt;= 0) {
                  read = read + numRead;
              }
              fileEvent.setFileSize(len);
              fileEvent.setFileData(fileBytes);
              fileEvent.setStatus("Success");
          } catch (Exception e) {
              e.printStackTrace();
              fileEvent.setStatus("Error");
          }
      } else {
          System.out.println("path specified is not pointing to a file");
          fileEvent.setStatus("Error");
      }
      //Now writing the FileEvent object to socket
      try {
  
          outputStream.writeObject(fileEvent);
  
          System.out.println("Choose file: ");
          String str = scanner.nextLine();
  
          String[] parts = str.split(Pattern.quote("\\"));
          String filename = parts[parts.length - 1];
  
          dos.writeInt(filename.length());
          dos.writeChars(filename);
  
          Thread.sleep(3000);
          System.exit(0);
      } catch (IOException e) {
          e.printStackTrace();
      } catch (InterruptedException e) {
          e.printStackTrace();
      }
  
  }
  </code></pre>