Note that there are some explanatory texts on larger screens.

plurals
  1. POLose HttpServletRequest Parts After Reading Them
    primarykey
    data
    text
    <p>I have a servlet that receives an uploaded file. We've been having issues with a certain client's request not having a file attached or so the servlet thinks. The upload servlet is a replacement for an old one and we're using the Apache Commons FileUpload library to parse the file from the request. The old code uses the JavaZoom library. The requests client we're having issues with work perfectly fine in the old code.</p> <p>In order to troubleshoot the problem, I added a bunch of logging to look at the request headers and parts to compare requests from a client that works with the one that doesn't. This is a snippet of how I'm looking at the parts:</p> <pre><code>Collection&lt;Part&gt; parts = request.getParts(); for(Part part : parts) { String partName = part.getName(); log.debug("Part=" + partName); Collection&lt;String&gt; headerNames = part.getHeaderNames(); for(String headerName : headerNames) { String headerValue = part.getHeader(headerName); log.debug(headerName + "=" + headerValue); InputStream inputStream = part.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); StringBuilder builder = new StringBuilder(); try { for(String line=bufferedReader.readLine(); line!=null; line=bufferedReader.readLine()) { builder.append(line); builder.append('\n'); } } catch (IOException ignore) { // empty } finally { inputStream.reset(); } log.debug("InputStream=" + builder.toString()); } } </code></pre> <p>All this code works fine and I get the logging I'm expecting. However, this next bit of code doesn't act as expected:</p> <pre><code>if (isMultipart) { // Create a factory for disk-based file items FileItemFactory factory = new DiskFileItemFactory(); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); @SuppressWarnings("rawtypes") List items = null; // Parse the request try { items = upload.parseRequest(request); log.debug("items=" + items); } catch (FileUploadException ex) { log.warn("Error parsing request", ex); response.sendError(HttpServletResponse.SC_BAD_REQUEST, ex.getMessage()); } </code></pre> <p>the <code>items</code> variable is empty when it's logged. If I comment out the code for logging the request parts, this bit of code works and the <code>items</code> variable contains the uploaded file.</p> <p>I can only assume that the act of getting/reading the parts from the request somehow removes them from it and are no longer available for further processing. Is there some way to read them for logging purposes and still retain them in the request for further processing?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload