为什么这个异常FileItemStream $ ItemSkippedException? [英] why this exception FileItemStream$ItemSkippedException?

查看:1159
本文介绍了为什么这个异常FileItemStream $ ItemSkippedException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在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; ((c = stream.read())!= -1){
System.out.print((char)c);


$ b} catch(Exception e){
// TODO:处理异常
e.printStackTrace();
}
System.out.println(out of try);
ByteArrayOutputStream output = new ByteArrayOutputStream();
int nRead; ((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();

用这段代码我可以读取流。
也在控制台out of try上打印



最后放在 while( nRead = stream.read(buffer,0,buffer.length))!= -1)
我得到了警告


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

如何解决这个问题。


<解决方案

解决方案

为什么你得到这个异常:JavaDocs ItemSkippedException解释了一点点:$ b​​
$ b如果试图从InputStream中读取数据,就会抛出这个异常,FileItemStream.openStream (),在创建了FileItemStream的迭代器上调用了Iterator.hasNext()之后。您正在使用InputStream 之外的while循环引起的问题,因为另一个迭代被称为关闭(跳过) e文件InputStream,你尝试读取。



解决方案:在while循环中使用InputStream。如果在处理文件之前需要所有表单字段,请确保在客户端以正确的顺序进行设置。首先是所有字段,最后是文件。例如,使用JavaScript FormData:

  var fd = new window.FormData(); 

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

//文件必须是最后一个参数才能追加
fd.append(file,file);

而在服务器端:

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

//项目的顺序由客户端,第一个表单字段,最后一个文件流
if(item.isFormField()){
String name = item.getFieldName ();
String value = Streams.asString(stream);
//这里我们得到了param1和param2
} else {
String filename = item.getName();
String mimetype = item.getContentType();

ByteArrayOutputStream output = new ByteArrayOutputStream();
int nRead; ((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();
}
}


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.??

解决方案

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

这篇关于为什么这个异常FileItemStream $ ItemSkippedException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
Java开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆