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

 更新时间:2013年08月30日 09:23:21   作者:  
问题在于fileupload组件解析完自定义的head节点后,却忘记传递到FileItemStreamImpl中了,稍作修订,即可修正该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;
            }

相关文章

  • Java数组与字符串深入探索使用方法

    Java数组与字符串深入探索使用方法

    在今天的文章中,我将为你详细讲述Java学习中重要的一节 [ 数组与字符串 ] ,带你深入了解Java语言中数组的声明、创建和初始化方法,字符串的定义以及常用到的操作方法
    2022-07-07
  • Java自动化测试中多数据源的切换(实例讲解)

    Java自动化测试中多数据源的切换(实例讲解)

    下面小编就为大家带来一篇Java自动化测试中多数据源的切换(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10
  • Java后端SSM框架图片上传功能实现方法解析

    Java后端SSM框架图片上传功能实现方法解析

    这篇文章主要介绍了Java后端SSM框架图片上传功能实现方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • 最新springboot中必须要了解的自动装配原理

    最新springboot中必须要了解的自动装配原理

    本文给大家介绍springboot中必须要了解的自动装配原理,spring-boot-dependencies:核心依赖都在父工程中,这个里面主要是管理项目的资源过滤及插件,本文对springboot自动装配原理给大家介绍的非常详细,需要的朋友参考下吧
    2022-05-05
  • 使用JPA自定义VO类型转换(EntityUtils工具类)

    使用JPA自定义VO类型转换(EntityUtils工具类)

    这篇文章主要介绍了使用JPA自定义VO类型转换(EntityUtils工具类),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • 解决SpringMVC使用@RequestBody注解报400错误的问题

    解决SpringMVC使用@RequestBody注解报400错误的问题

    这篇文章主要介绍了解决SpringMVC使用@RequestBody注解报400错误的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • SpringBoot中自定义首页(默认页)及favicon的方法

    SpringBoot中自定义首页(默认页)及favicon的方法

    这篇文章主要介绍了SpringBoot中如何自定义首页(默认页)及favicon,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-08-08
  • spring boot2.0图片上传至本地或服务器并配置虚拟路径的方法

    spring boot2.0图片上传至本地或服务器并配置虚拟路径的方法

    最近写了关于图片上传至本地文件夹或服务器,上传路径到数据库,并在上传时预览图片。本文通过实例代码给大家分享spring boot2.0图片上传至本地或服务器并配置虚拟路径的方法,需要的朋友参考下
    2018-12-12
  • Java进程内缓存框架EhCache详解

    Java进程内缓存框架EhCache详解

    这篇文章主要介绍了Java进程内缓存框架EhCache,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2021-12-12
  • IntelliJ IDEA cmd和idea Terminal查看java版本不一致的解决方案

    IntelliJ IDEA cmd和idea Terminal查看java版本不一致的解决

    原来win10电脑上安装的是jdk8的版本,因某些原因,现在想换成jdk7的版本,修改环境变量后,在cmd中执行 [java -version]命令,显示的是7的版本,遇到这样的问题如何解决呢?下面小编给大家分享IntelliJ IDEA cmd和idea Terminal查看java版本不一致的解决方案,一起看看吧
    2023-09-09

最新评论