快速解决commons-fileupload组件无法处理自定义head信息的bug

Jakarta commons fileupload组件可以处理HTTP请求及响应,很多时候被用来处理文件上传,但是近期发现,当我们自定义文件上传、自己组装mime信息、文件上传时加入自定义head节点时,fileupload组件无法获得自定义的head节点,仔细分析了fileupload组件源代码后,发现核心方法在FileUploadBase文件的findNextItem方法中,问题在于fileupload组件解析完自定义的head节点后,却忘记传递到FileItemStreamImpl中了,稍作修订,即可修正该bug。


代码如下:

/**解析文件列表
         * Called for finding the nex item, if any.
         * @return True, if an next item was found, otherwise false.
         * @throws IOException An I/O error occurred.
         */
        private boolean findNextItem() throws IOException {
            if (eof) {
                return false;
            }
            if (currentItem != null) {
                currentItem.close();
                currentItem = null;
            }
            for (;;) {
                boolean nextPart;
                if (skipPreamble) {
                    nextPart = multi.skipPreamble();
                } else {
                    nextPart = multi.readBoundary();
                }
                if (!nextPart) {
                    if (currentFieldName == null) {
                        // Outer multipart terminated -> No more data
                        eof = true;
                        return false;
                    }
                    // Inner multipart terminated -> Return to parsing the outer
                    multi.setBoundary(boundary);
                    currentFieldName = null;
                    continue;
                }
                FileItemHeaders headers = getParsedHeaders(multi.readHeaders());
                if (currentFieldName == null) {
                    // We're parsing the outer multipart
                    String fieldName = getFieldName(headers);
                    if (fieldName != null) {
                        String subContentType = headers.getHeader(CONTENT_TYPE);
                        if (subContentType != null
                                &&  subContentType.toLowerCase()
                                        .startsWith(MULTIPART_MIXED)) {
                            currentFieldName = fieldName;
                            // Multiple files associated with this field name
                            byte[] subBoundary = getBoundary(subContentType);
                            multi.setBoundary(subBoundary);
                            skipPreamble = true;
                            continue;
                        }
                        String fileName = getFileName(headers);
                        currentItem = new FileItemStreamImpl(fileName,
                                fieldName, headers.getHeader(CONTENT_TYPE),
                                fileName == null, getContentLength(headers));
                        notifier.noteItem();
                        itemValid = true;
                        return true;
                    }
                } else {
                    String fileName = getFileName(headers);
                    if (fileName != null) {
                             //这里代码要修订
                        //这是原来的代码,没有传入header
                        //currentItem = new FileItemStreamImpl(fileName, currentFieldName,headers.getHeader(CONTENT_TYPE),false, getContentLength(headers));
                        //这是新的代码,我们要传入header
                        currentItem = new FileItemStreamImpl(fileName, currentFieldName,headers.getHeader(CONTENT_TYPE),false, getContentLength(headers),headers);
                        notifier.noteItem();
                        itemValid = true;
                        return true;
                    }
                }
                multi.discardBodyData();
            }
        }

/**原始代码,存在丢失FileItemHeaders信息的bug
             * Creates a new instance.
             * @param pName The items file name, or null.
             * @param pFieldName The items field name.
             * @param pContentType The items content type, or null.
             * @param pFormField Whether the item is a form field.
             * @param pContentLength The items content length, if known, or -1
             * @throws IOException Creating the file item failed.
             */
            FileItemStreamImpl(String pName, String pFieldName,
                    String pContentType, boolean pFormField,
                    long pContentLength) throws IOException {
                name = pName;
                fieldName = pFieldName;
                contentType = pContentType;
                formField = pFormField;
                final ItemInputStream itemStream = multi.newInputStream();
                InputStream istream = itemStream;
                if (fileSizeMax != -1) {
                    if (pContentLength != -1
                            &&  pContentLength > fileSizeMax) {
                        FileUploadException e =
                            new FileSizeLimitExceededException(
                                "The field " + fieldName
                                + " exceeds its maximum permitted "
                                + " size of " + fileSizeMax
                                + " characters.",
                                pContentLength, fileSizeMax);
                        throw new FileUploadIOException(e);
                    }
                    istream = new LimitedInputStream(istream, fileSizeMax) {
                        protected void raiseError(long pSizeMax, long pCount)
                                throws IOException {
                            itemStream.close(true);
                            FileUploadException e =
                                new FileSizeLimitExceededException(
                                    "The field " + fieldName
                                    + " exceeds its maximum permitted "
                                    + " size of " + pSizeMax
                                    + " characters.",
                                    pCount, pSizeMax);
                            throw new FileUploadIOException(e);
                        }
                    };
                }
                stream = istream;
            }

/**创建FileItem,修订后的代码,解决丢失FileItemHeaders信息的bug
             * @param pName
             * @param pFieldName
             * @param pContentType
             * @param pFormField
             * @param pContentLength
             * @param headers
             * @throws IOException
             */
            FileItemStreamImpl(String pName, String pFieldName,
                    String pContentType, boolean pFormField,
                    long pContentLength,FileItemHeaders headers) throws IOException {
                name = pName;
                fieldName = pFieldName;
                contentType = pContentType;
                formField = pFormField;
                if(headers!=null){
                        this.headers = headers;
                }
                final ItemInputStream itemStream = multi.newInputStream();
                InputStream istream = itemStream;
                if (fileSizeMax != -1) {
                    if (pContentLength != -1
                            &&  pContentLength > fileSizeMax) {
                        FileUploadException e =
                            new FileSizeLimitExceededException(
                                "The field " + fieldName
                                + " exceeds its maximum permitted "
                                + " size of " + fileSizeMax
                                + " characters.",
                                pContentLength, fileSizeMax);
                        throw new FileUploadIOException(e);
                    }
                    istream = new LimitedInputStream(istream, fileSizeMax) {
                        protected void raiseError(long pSizeMax, long pCount)
                                throws IOException {
                            itemStream.close(true);
                            FileUploadException e =
                                new FileSizeLimitExceededException(
                                    "The field " + fieldName
                                    + " exceeds its maximum permitted "
                                    + " size of " + pSizeMax
                                    + " characters.",
                                    pCount, pSizeMax);
                            throw new FileUploadIOException(e);
                        }
                    };
                }
                stream = istream;
            }

(0)

相关推荐

  • Apache commons fileupload文件上传实例讲解

    文件上传的方法主要目前有两个常用的,一个是SmartUpload,一个是Apache的Commons fileupload. 我们这里主要介绍下第二个的用法,首先要上传文件,注意几个问题: 1 form表单内,要添加空间<input type="file" name="myfile"> 2 form表单的内容格式要定义成multipart/form-data格式 3 需要类库:1 commons-io.jar 2commons-fileupload-1.

  • Java组件commons fileupload实现文件上传功能

    Apache提供的commons-fileupload jar包实现文件上传确实很简单,最近要用Servlet/JSP做一个图片上传功能,在网上找了很多资料,大多是基于struts框架介绍的,还有些虽然也介绍common-fileupload的上传,但是那些例子比较老,有些类现在都废弃了. 通过研究学习总结,终于完成了这个上传功能,下面与大家分享一下. 案例场景 一个图书馆后台管理界面,需要提供上传图书图片的功能并且最终显示在页面中. 实现效果 进入添加书籍页面,默认显示一个图片"暂无突破&qu

  • JavaEE组件commons-fileupload实现文件上传、下载

    一.文件上传概述 实现Web开发中的文件上传功能,需要两步操作: 1.在Web页面中添加上传输入项 <form action="#" method="post" enctype="multipart/form-data"> <input type="file" name="filename1"/><br> <input type="file" n

  • java组件commons-fileupload文件上传示例

    文件上传在Web应用中非常普遍,要在Java Web环境中实现文件上传功能非常容易,因为网上已经有许多用Java开发的组件用于文件上传,本文以使用最普遍的commons-fileupload组件为例,演示如何为Java Web应用添加文件上传功能. commons-fileupload组件是Apache的一个开源项目之一,可以从http://commons.apache.org/fileupload/下载.该组件简单易用,可实现一次上传一个或多个文件,并可限制文件大小. 下载后解压zip包,将c

  • java组件commons-fileupload实现文件上传、下载、在线打开

    最近做了一个文件上传.下载.与在线打开文件的功能,刚开始对文件上传的界面中含有其它表单(例如输入框.密码等)在上传的过程中遇到了许多问题,下面我写了一个同时实现文件上传.下载.在线打开文件的测试程序. 首先请看效果图: 核心代码: package com.jefry; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.URL; import java.u

  • Apache Commons fileUpload实现文件上传之一

    废话不多说了,直奔主题了. 需要两个jar包: commons-fileupload.jar Commons IO的jar包(本文使用commons-io-2.4.jar) 利用Servlet来实现文件上传. package web.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.

  • java组件commons-fileupload实现文件上传

    一.所需要的包: 1.commons-fileupload-1.2.1.jar: 下载地址 http://commons.apache.org/downloads/download_fileupload.cgi 2.commons-io-1.4.jar: 下载地址 http://commons.apache.org/downloads/download_io.cgi 二.注意事项: form表单里面要加上enctype="multipart/form-data" 三.代码示例  1.j

  • JSP组件commons-fileupload实现文件上传

    本文实例为大家分享了JSP使用commons-fileupload实现文件上传代码,供大家参考,具体内容如下 1.准备: 将commons-fileupload-1.1.zip和commons-io-1.1.zip复制到"\WEB-INF\lib"目录下 2.首先是Servlet:FileUpload.java package servlet; import java.io.File; import java.io.IOException; import java.io.PrintWr

  • Apache Commons fileUpload文件上传多个示例分享

    本文通过实例来介绍如何使用commons-fileupload.jar,Apache的commons-fileupload.jar可方便的实现文件的上传功能,具体内容如下 将Apache的commons-fileupload.jar放在应用程序的WEB-INF\lib下,即可使用.下面举例介绍如何使用它的文件上传功能. 所使用的fileUpload版本为1.2,环境为Eclipse3.3+MyEclipse6.0.FileUpload 是基于 Commons IO的,所以在进入项目前先确定Com

  • commons fileupload实现文件上传的实例代码

    一.文件上传的原理分析 1.文件上传的必要前提 a.表单的method必须是post b.表单的enctype属性必须是multipart/form-data类型的. enctype默认值:application/x-www-form-urlencoded 作用:告知服务器,请求正文的MIME类型 application/x-www-form-urlencoded : username=abc&password=123 ServletRequest.getParameter(String nam

随机推荐