Note that there are some explanatory texts on larger screens.

plurals
  1. POFile Image to Servlet Fails
    text
    copied!<p>I am trying to upload an image to a servlet, but every once and a while during automated testing, it silently fails.</p> <p>Do you guys know what would cause this?</p> <p>Here is the code on the server:</p> <pre><code> @ResponseBody @RequestMapping(method = RequestMethod.POST) public String upload(HttpServletRequest request) throws Exception { BufferedImage image = null; @SuppressWarnings("unchecked") List&lt;FileItem&gt; items = new ServletFileUpload( new DiskFileItemFactory()).parseRequest(request); Logger.log(LogLevel.INFO, "Upload contains " + items.size() + " items."); int i = 0; for (FileItem item : items) { Logger.log(LogLevel.INFO, "\tItem " + (i++) + ". Name:\t" + item.getName() + ", Type:\t" + item.getContentType()); // File is of type "file" if (!item.isFormField()) { InputStream inputStream = null; try { inputStream = item.getInputStream(); if (inputStream.available() == 0) { Logger.log(LogLevel.WARN, "Item shows file type, but no bytes are available"); } image = ImageIO.read(inputStream); if (image != null) { break; } } catch (Exception e) { Logger.log(LogLevel.ERROR, "There was an error reading the image. " + ExceptionUtils.getFullStackTrace(e)); throw new Exception("image provided is not a valid image"); } finally { if (inputStream != null) { IOUtils.closeQuietly(inputStream); } } } } if (image == null) { Logger.log(LogLevel.ERROR, "Image was supposedly read correctly, but was null afterwards"); throw new Exception("Image provided could not be read"); } //do stuff with image ... } </code></pre> <p>Here is the test:</p> <pre><code> public void testImageUpload throws Exception { HttpPost httppost = new HttpPost("path/to/endpoint"); File file=new File(imgLoc); FileBody bin = new FileBody(file); StringBody comment = new StringBody("Filename: " + file); MultipartEntity reqEntity = new MultipartEntity(); reqEntity.addPart("upload-file", bin); reqEntity.addPart("comment", comment); httppost.setHeader("Accept", "application/json"); httppost.setHeader("Connection","Keep-Alive"); httppost.setEntity(reqEntity); HttpResponse response =testClient.getClient().execute(httppost); imgResponse=response.getStatusLine().toString(); System.out.println(imgResponse); BufferedReader reader = new BufferedReader( new InputStreamReader(response.getEntity().getContent())); String line; while ((line = reader.readLine()) != null){ output = output + " " +line;} System.out.println("Image Response: "+output); } </code></pre> <p>Here is the output from the server when it fails:</p> <pre><code>2013/10/02 05-53-32,287::LOG:INFO[com.example#upload:L130 -- Upload contains 2 items.] 2013/10/02 05-53-32,288::LOG:INFO[com.example#upload:L133 -- Item 0. Name: Dog.jpg, Type: application/octet-stream] 2013/10/02 05-53-32,288::LOG:WARN[com.example#upload:L140 -- Item shows file type, but no bytes are available] 2013/10/02 05-53-32,289::LOG:INFO[com.example#upload:L133 -- Item 1. Name: null, Type: text/plain; charset=ISO-8859-1] 2013/10/02 05-53-32,290::LOG:ERROR[com.example#upload:L159 -- Image was supposedly read correctly, but was null afterwards] </code></pre> <p>We catch the exception from the image upload and send back a response code of 422 back to the client, so on the test, we get <code>imgResponse</code>==422 which is a failure case.</p> <p><strong>Note: this only happens <em>sometimes</em> you run the test.</strong></p>
 

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