Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why this exception FileItemStream$ItemSkippedException?

in a gwt web application. I have to send a file and some parameter attached to it.

on ServerSide

try {

        ServletFileUpload upload = new ServletFileUpload();

        FileItemIterator iterator = upload.getItemIterator(request);

        while (iterator.hasNext()) {
            FileItemStream item = iterator.next();


            if (item.isFormField()) {

                String fieldName=item.getFieldName();
                String fieldValue = Streams.asString(item.openStream());
                System.out.println(" chk  " +fieldName +"  =  "+ fieldValue);
            } else {
                stream = item.openStream();
                fileName = item.getName();
                mimetype = item.getContentType();
                int c;
                while ((c = stream.read()) != -1) { 
                  System.out.print((char) c); 
                    }
            }
        }
    }catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    System.out.println("out of try");
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    int nRead;
    while ((nRead = stream.read(buffer, 0, buffer.length)) != -1) {
        System.out.println("lenth111" +nRead);
        output.write(buffer, 0, nRead);
    }
    System.out.println("lenth" +nRead);
    output.flush();

with this code i can read the stream. and also on console "out of try" is also printed

And finally on while ((nRead = stream.read(buffer, 0, buffer.length)) != -1) line i got a Warning

WARNING: /UploadFileServlet: org.apache.commons.fileupload.FileItemStream$ItemSkippedException.

How to solve this problem.??

like image 261
GameBuilder Avatar asked Aug 30 '12 13:08

GameBuilder


1 Answers

Answer a bit late but I had the same problem.

Why you get that exception: The JavaDocs of ItemSkippedException explain a little bit:

This exception is thrown, if an attempt is made to read data from the InputStream, which has been returned by FileItemStream.openStream(), after Iterator.hasNext() has been invoked on the iterator, which created the FileItemStream.

You are using the InputStream stream outside the while loop which causes the problem because another iteration is called which closes (skips) the file InputStream you try to read from.

Solution: Use the InputStream inside the while loop. If you need all form-fields before processing the file, ensure you set it in the right order on client side. First all fields, last the file. For example using the JavaScript FormData:

var fd = new window.FormData();

fd.append("param1", param1);
fd.append("param2", param2);

// file must be last parameter to append
fd.append("file", file);

And on server side:

FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
    FileItemStream item = iter.next();
    InputStream stream = item.openStream();

    // the order of items is given by client, first form-fields, last file stream
    if (item.isFormField()) {
        String name = item.getFieldName();
        String value = Streams.asString(stream);
        // here we get the param1 and param2
    } else {
        String filename = item.getName();
        String mimetype = item.getContentType();

        ByteArrayOutputStream output = new ByteArrayOutputStream();
        int nRead;
        while ((nRead = stream.read(buffer, 0, buffer.length)) != -1) {
            System.out.println("lenth111" +nRead);
            output.write(buffer, 0, nRead);
        }
        System.out.println("lenth" +nRead);
        output.flush(); 
    }
}
like image 130
Christof Aenderl Avatar answered Nov 16 '22 15:11

Christof Aenderl