Commons FileUpload is nice, but not enough in your case. It will analyze the entire object in memory before providing files (and streams). You are not interested in individual items. Basically, you just want to transparently transfer the request body from one to another without changing it or storing it in memory. FileUpload will analyze the request body for only some βusableβ Java objects, and HttpClient will only create the same request object again based on these Java objects. These Java objects also consume memory.
You donβt need a library for this (or it should be Commons IO to replace the for loop with oneliner using IOUtils#copy() ). A simple Java NET and IO API is enough. Here is an example run:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { URLConnection connection = new URL("http://your.url.to.panda").openConnection(); connection.setDoOutput(true); // POST. connection.setRequestProperty("Content-Type", request.getHeader("Content-Type")); // This one is important! You may want to check other request headers and copy it as well. // Set streaming mode, else HttpURLConnection will buffer everything. int contentLength = request.getContentLength(); if (contentLength > -1) { // Content length is known beforehand, so no buffering will be taken place. ((HttpURLConnection) connection).setFixedLengthStreamingMode(contentLength); } else { // Content length is unknown, so send in 1KB chunks (which will also be the internal buffer size). ((HttpURLConnection) connection).setChunkedStreamingMode(1024); } InputStream input = request.getInputStream(); OutputStream output = connection.getOutputStream(); byte[] buffer = new byte[1024]; // Uses only 1KB of memory! for (int length = 0; (length = input.read(buffer)) > 0;) { output.write(buffer, 0, length); output.flush(); } output.close(); connection.getInputStream(); // Important! It lazily executed. }
source share