title: What is the best way to transfer a file using Java?
body: <p>I am writing code for uploading a file from a client to my server and the performance isn't as fast as I think it should be.</p>
  
  <p><strong>I have the current code snippet that is doing the file transfer and I was wondering how I could speed up the transfer.</strong></p>
  
  <p>Sorry about all of the code:</p>
  
  <pre><code>InputStream fileItemInputStream ;
  OutputStream saveFileStream;
  int[] buffer;
  while (fileItemInputStream.available() &gt; 0) {
      buffer = Util.getBytesFromStream(fileItemInputStream);
      Util.writeIntArrToStream(saveFileStream, buffer);
  }
  saveFileStream.close();
  fileItemInputStream.close();
  </code></pre>
  
  <p>The Util methods are as follows:</p>
  
  <pre><code>public static int[] getBytesFromStream(InputStream in, int size) throws IOException {
      int[] b = new int[size];
      int count = 0;
      while (count &lt; size) {
          b[count++] = in.read();
      }
      return b;
  }
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>public static void writeIntArrToStream(OutputStream out, int[] arrToWrite) throws IOException {
      for (int i = 0; i &lt; arrToWrite.length; i++) {
          out.write(arrToWrite[i]);
      }
  }
  </code></pre>