Apache Commons Fileupload / Tomcat不能应付out.flush() [英] Apache Commons Fileupload / Tomcat cant cope with out.flush()

查看:151
本文介绍了Apache Commons Fileupload / Tomcat不能应付out.flush()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道一个解决方法,如果你想刷新servlet输出流,apache commons fileupload会引发下面的异常?

  FileUploadException:处理multipart / form-data请求失败。流意外地结束
基本上我有代码,循环使用apache commons fileupload上传每个文件,然后是试图 out.flush()关于每个文件的一些统计信息。即:

  FileItemFactory factory = new DiskFileItemFactory(); 
ServletFileUpload upload = new ServletFileUpload(factory);
列表< FileItem> items = upload.parseRequest(request); (FileItem field:items){
if(!field.isFormField()& field.getName().length()> 0&& field.b
$ b。 getName()。getSize()> 0){
ArticleImport helper = new ArticleImport(new ArticleImportResponder(user,out));
//此辅助对象使用out.flush()向用户提供反馈。
helper.process(field.getInputStream(),user);





$ b

这个问题不会发生在Apache Tomcat 6.0.20,但它确实发生在早期版本中。

解决方案

不幸的是,这是一个已知的bug,除了确保你的使用至少6.0.20。



您可以检测到哪个版本的tomcat正在运行,并使用它来确定是否可以使用out.flush()使用以下调用:

  javax.servlet.ServletContext.getServerInfo(); 


Does anyone know of a workaround whereby if you are trying to flush the servlet output stream, apache commons fileupload throws the following exception?

FileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly

Basically I have code that loops through each file uploaded using apache commons fileupload, and then am trying to out.flush() some stats about each file. ie:

    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    List<FileItem> items = upload.parseRequest(request);

    for(FileItem field : items) {
        if (!field.isFormField() && field.getName().length()>0 && field.getName().getSize()>0) {
            ArticleImport helper = new ArticleImport(new ArticleImportResponder(user,out));
            // This helper object uses out.flush() to provide feedback to the user.
            helper.process(field.getInputStream(), user);
            }
        }
    }

The problem does not occur in Apache Tomcat 6.0.20, but it does occur in earlier versions.

解决方案

Unfortunately this is a known bug, there is nothing you can do about it except ensure that your using at least 6.0.20.

You could detect which version of tomcat is running and use that to determine if you can use out.flush() using the following call:

javax.servlet.ServletContext.getServerInfo();

这篇关于Apache Commons Fileupload / Tomcat不能应付out.flush()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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