Java Servlet Image Upload Corrupt (gray bottom)
We have a servlet that accepts image uploads. Sometimes when 开发者_开发问答the uploads originate in our iPhone client (flaky connection) the saved image can end up being partly or completely gray. I suspect this is due to the connection being prematurely terminated and the servlet ending up processing an incomplete image.
Whats the best remedy for this? Is there a way to see if the whole image was uploaded before processing? Should I use HTTP Content-Length header and compare whats uploaded with this number?
Thanks!
Some code for context:
@Path("images/")
@POST
@Consumes("image/*")
@Produces({"application/xml", "application/json"})
public AbstractConverter postImage(byte[] imageData) {
BufferedImage bufferedImage = null;
try {
bufferedImage = ImageIO.read(new ByteArrayInputStream(imageData));
} catch (Exception e) {
}
if (bufferedImage == null) {
throw new PlacesException("Image data not provided or could not be parsed", Response.Status.BAD_REQUEST);
}
...
BufferedImage scaledImage = ImageTool.scale(bufferedImage, imageSize);
BufferedImage thumbnail = ImageTool.scale(bufferedImage, thumbnailSize);
//Save image and thumbnail
File outputfile = new File(path);
ImageTool.imageToJpegFile(scaledImage, outputfile, 0.9f);
File tnOutputfile = new File(thumbnailPath);
ImageTool.imageToJpegFile(thumbnail, tnOutputfile, 0.9f);
...
public static void imageToJpegFile(RenderedImage image, File outFile, float compressionQuality) throws IOException {
//Find a jpeg writer
ImageWriter writer = null;
Iterator<ImageWriter> iterator = ImageIO.getImageWritersByFormatName("jpeg");
if (iterator.hasNext()) {
writer = iterator.next();
} else {
throw new RuntimeException("No jpeg writer found");
}
//Set the compression quality
ImageWriteParam params = writer.getDefaultWriteParam();
params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
params.setCompressionQuality(compressionQuality);
//Write to the out file
ImageOutputStream ios = null;
try {
ios = ImageIO.createImageOutputStream(outFile);
writer.setOutput(ios);
writer.write(null, new IIOImage(image, null, null), params);
} finally {
writer.dispose();
if (ios != null) {
try {
ios.flush();
} catch (Exception e) {
}
try {
ios.close();
} catch (Exception e) {
}
}
}
}
Seems that the upload did not complete properly.
As you point out yourself, your best bet is to use the HTTP Content-Length
header to check that all data has been received. If not, discard the image.
精彩评论