Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么会出现此异常FileItemStream$ItemSkippedException?_Java_File Upload_Apache Commons Fileupload - Fatal编程技术网

Java 为什么会出现此异常FileItemStream$ItemSkippedException?

Java 为什么会出现此异常FileItemStream$ItemSkippedException?,java,file-upload,apache-commons-fileupload,Java,File Upload,Apache Commons Fileupload,在gwt web应用程序中。 我必须发送一个文件和一些参数附加到它 服务器端 try { ServletFileUpload upload = new ServletFileUpload(); FileItemIterator iterator = upload.getItemIterator(request); while (iterator.hasNext()) { FileItemStream item = it

在gwt web应用程序中。 我必须发送一个文件和一些参数附加到它

服务器端

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();
使用此代码,我可以读取流。 而且在控制台上也会打印“试用期外”字样

最后是
while((nRead=stream.read(buffer,0,buffer.length))!=-1)
我得到一个警告

警告:/UploadFileServlet:org.apache.commons.fileupload.FileItemStream$ItemSkippedException


如何解决这个问题?

回答有点晚,但我也有同样的问题

为什么会出现这种异常:ItemSkippedException的JavaDocs解释了一点:

如果在创建FileItemStream的迭代器上调用Iterator.hasNext()后,试图从FileItemStream.openStream()返回的InputStream读取数据,则会引发此异常

您正在while循环之外使用InputStream流,这会导致问题,因为调用了另一个迭代来关闭(跳过)您尝试读取的文件InputStream

解决方案:在while循环中使用InputStream。如果在处理文件之前需要所有表单字段,请确保在客户端将其设置为正确的顺序。首先是所有字段,最后是文件。例如,使用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);
在服务器端:

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(); 
    }
}

嗨,GameBuilder和Thomas Broyer,你们找到解决方案了吗?因为我几天来一直有同样的问题。拜托,有人能帮我们吗?谢谢嘿,我也在找同样的。你有什么解决方案吗?正确,我希望可以将它带到迭代器循环之外。这会导致大文件上载的OutOfMemory错误。当我们使用parseRequest(request)时不会发生这种情况。附近有路吗?