Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy doesn't servlet find FileItem in multipart request?
    primarykey
    data
    text
    <p>I've written a Servlet that handles file uploads using the Apache commons file upload library. Here is some of the code:</p> <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { boolean isMultipart = ServletFileUpload.isMultipartContent(request); if (isMultipart) { try { DiskFileItemFactory fileItemFactory = new DiskFileItemFactory(); // Set size threshold for storing upload fileItemFactory.setSizeThreshold(1 * 1024 * 50); // 50 KB // Set temporary directory to store uploaded files above threshold size fileItemFactory.setRepository(new File(TEMP_DIRECTORY)); ServletFileUpload upload = new ServletFileUpload(fileItemFactory); //HashMap&lt;String, String&gt; params = new HashMap&lt;String, String&gt;(); FileItemIterator iterator = upload.getItemIterator(request); upload.setSizeMax(REQUEST_MAX_SIZE); List items = upload.parseRequest(request); Iterator it = items.iterator(); while (it.hasNext()) { FileItem item = (FileItem) it.next(); if(item.isFormField()) { } else { String contentType = item.getContentType(); String fileName = item.getName(); String fieldName = item.getFieldName(); boolean isInMemory = item.isInMemory(); long sizeInBytes = item.getSize(); File uploadedFile = new File(PATH + "new_audio1.amr"); item.write(uploadedFile); System.out.println("Field: " + fieldName); System.out.println("File name: " + fileName); System.out.println("Size: " + sizeInBytes); System.out.println("Is in memory:" + isInMemory); } } } catch (Exception ex) { throw new ServletException(ex); } } else { throw new ServletException(); } </code></pre> <p>For some reason that escapes me the List 'items' is empty so I can't grab the uploaded file. </p> <p>For the upload itself, I've written some java code: </p> <pre><code>File audioFile = new File("C:\\Users\\Soto\\Desktop\\test recording.amr"); String url = "http://localhost:8080/AudioFileUpload/UploadServlet"; String charset = "UTF-8"; // random values String latitude = "145"; String longitude = "132"; String speed = "0"; String query; try { query = String.format("latitude=%s&amp;longitude=%s&amp;speed=%s", URLEncoder.encode(latitude, charset), URLEncoder.encode(longitude, charset), URLEncoder.encode(speed, charset)); } catch (UnsupportedEncodingException e) { query = String.format("latitude=%s&amp;longitude=%s&amp;speed=%s", latitude, longitude, speed); } HttpClient httpClient = new DefaultHttpClient(); httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpPost httpPost = new HttpPost(url + "?" + query); MultipartEntity mpEntity = new MultipartEntity(); ContentBody cbFile = new FileBody(audioFile, "audio/AMR"); mpEntity.addPart("audioFile", cbFile); httpPost.setEntity(mpEntity); HttpResponse response = null; try { response = httpClient.execute(httpPost); HttpEntity responseEntity = response.getEntity(); System.out.println(response.getStatusLine()); if(responseEntity != null) System.out.println(EntityUtils.toString(responseEntity)); if(responseEntity != null) { EntityUtils.consume(responseEntity); } httpClient.getConnectionManager().shutdown(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } </code></pre> <p>It feels to me like the file is being attached and uploaded correctly. I also tried doing it through HTML with a multipart/form-data post request, but the file still wasn't found.</p> <p>What am I doing wrong?</p> <p>EDIT: I removed the line at the beginning of the doPost() along with the if statement: </p> <pre><code>ServletFileUpload.isMultipartContent(request); </code></pre> <p>And then the upload worked correctly. Is it possible that this method consumes the request's 'input/output/whatever it is' stream?</p> <p>Thanks </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.
 

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