Apache commons fileupload FileItemIterator hasNext()返回false [英] Apache commons fileupload FileItemIterator hasNext() returns false

查看:1671
本文介绍了Apache commons fileupload FileItemIterator hasNext()返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用apache commons fileupload stream api。但是FileItemIterator FileItemIterator iter = upload.getItemIterator(request); 在其hasNext() iter.hasNext()这是什么问题?



代码和web部分如下:

  protected void doPost(HttpServletRequest请求,HttpServletResponse响应)抛出IOException {

/ **
*将使用Apache commons文件上传方法
* /
//检查是否有文件上传请求
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
$ b $ if(isMultipart){
try {
//创建一个新的文件上传处理程序
ServletFileUpload upload = new ServletFileUpload();
//解析请求
FileItemIterator iter = upload.getItemIterator(request); (iter.hasNext()){
FileItemStream item = iter.next();


String name = item.getFieldName();
InputStream stream = item.openStream();
if(item.isFormField()){
System.out.println(Form field+ name +with value+ Streams.asString(stream)+detected。
} else {
System.out.println(File field+ name +with file name+ item.getName()+detected。);
//处理输入流
//
}
}

} catch(FileUploadException ex){
Logger。 getLogger(ResourceUploadServlet.class.getName()).log(Level.SEVERE,null,ex);


$ b $ / code $ / pre
$ b $ jsp页面如下: / p>

 < form action =AServletmethod =POST
enctype =multipart / form-data> ;
< input type =filename =Content/>
描述:< input type =textname =Description/>
< input type =submitvalue =Submit/>

< / form>

Best,

解决方案

在我的web.xml文件中。有一个过滤器

 < filter> 
< filter-name> resourceUploadServlet< / filter-name>
< filter-class> org.mortbay.servlet.MultiPartFilter< / filter-class>
< init-param>
< param-name> maxSize< / param-name>
< param-value> 2147483648< / param-value>
< / init-param>
< / filter>

当我删除过滤器时,问题就解决了...


I'm using the apache commons fileupload stream api. But the FileItemIterator FileItemIterator iter = upload.getItemIterator(request); returns false in its hasNext() iter.hasNext() What is wrong with this?

The code and the web part is as follows:

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

    /**
     * Apache commons file upload method will be used
     */
    // Check that we have a file upload request
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);

    if (isMultipart) {
        try {
            // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload();
            // Parse the request
            FileItemIterator iter = upload.getItemIterator(request);

            while (iter.hasNext()) {
                FileItemStream item = iter.next();
                String name = item.getFieldName();
                InputStream stream = item.openStream();
                if (item.isFormField()) {
                    System.out.println("Form field " + name + " with value " + Streams.asString(stream) + " detected.");
                } else {
                    System.out.println("File field " + name + " with file name " + item.getName() + " detected.");
                    // Process the input stream
                    //...
                }
            }

        } catch (FileUploadException ex) {
              Logger.getLogger(ResourceUploadServlet.class.getName()).log(Level.SEVERE, null, ex);

        }
    }

jsp page is as follows:

 <form action="AServlet" method="POST"
              enctype="multipart/form-data">
                            <input type="file" name="Content" />
                            Description : <input  type="text" name="Description" />
                        <input type="submit" value="Submit" />

        </form>

Best,

解决方案

In my web.xml file. There was a filter

    <filter>
        <filter-name>resourceUploadServlet</filter-name>
        <filter-class>org.mortbay.servlet.MultiPartFilter</filter-class>
        <init-param>
            <param-name>maxSize</param-name>
            <param-value>2147483648</param-value>
        </init-param>
    </filter>

When i remove the filter the problem is solved...

这篇关于Apache commons fileupload FileItemIterator hasNext()返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆