Wednesday, 2 October 2013

File Image to Servlet Fails

File Image to Servlet Fails

I am trying to upload an image to a servlet, but every once and a while
during automated testing, it silently fails. It looks like the number of
bytes available to be read is zero.
Do you guys know what would cause this?
Here is the code:
List<FileItem> 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 = item.getInputStream();
if (inputStream.available()==0){
Logger.log(LogLevel.WARN, "Item shows file type, but no
bytes are available");
}
try {
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 InternalValidationException("imageSource",
"ImageInvalid",
"Image provided is not a valid image");
} finally {
IOUtils.closeQuietly(inputStream);
}
}
}
if (image == null) {
Logger.log(LogLevel.ERROR, "Image was supposedly read correctly,
but was null afterwards");
throw new InternalValidationException("imageSource",
"ImageInvalid",
"Image provided is not a valid image");
}
Here is the output:
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]

No comments:

Post a Comment