Java Proxy Servlet for sending files

I am trying to use Panda with my GWT application. I can upload video directly to panda server using

POST MY_PANDA_SERVER/videos/MY_VIDEO_ID/upload 

However, I would like to hide my panda server behind my J2EE server (Glassfish). I would like to achieve this:

  • Start uploading to some servlet on my J2EE server
  • User authentication
  • Send the file to my panda server while you upload it to the servlet

Ideally, I would never store the file on the J2EE server, but simply use it as a proxy to access the panda server.

+4
source share
4 answers

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. } 
+6
source

You can use apache commons file upload to get the file. You can then use the http client to upload the file to your panda server using POST. With downloading the apache commons file, you can process the file in memory, so you don't need to store it.

0
source

Based on Enrique's answer, I also recommend using FileUpload and HttpClient. FileUpload can provide you with the stream of the downloaded file:

 // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(); // Parse the request FileItemIterator iter = upload.getItemIterator(request); while (iter.hasNext()) { FileItemStream item = iter.next(); String name = item.getFieldName(); InputStream stream = item.openStream(); if (item.isFormField()) { System.out.println("Form field " + name + " with value " + Streams.asString(stream) + " detected."); } else { System.out.println("File field " + name + " with file name " + item.getName() + " detected."); // Process the input stream ... } } 

Then you can use HttpClient or HttpComponents to perform POST. You can find an example here .

0
source

The best solution is to use the apache-camel servlet component: http://camel.apache.org/servlet.html

0
source

Source: https://habr.com/ru/post/1304483/


All Articles