欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

快速解決commons-fileupload組件無(wú)法處理自定義head信息的bug

 更新時(shí)間:2013年08月30日 09:23:21   作者:  
問(wèn)題在于fileupload組件解析完自定義的head節(jié)點(diǎn)后,卻忘記傳遞到FileItemStreamImpl中了,稍作修訂,即可修正該bug
Jakarta commons fileupload組件可以處理HTTP請(qǐng)求及響應(yīng),很多時(shí)候被用來(lái)處理文件上傳,但是近期發(fā)現(xiàn),當(dāng)我們自定義文件上傳、自己組裝mime信息、文件上傳時(shí)加入自定義head節(jié)點(diǎn)時(shí),fileupload組件無(wú)法獲得自定義的head節(jié)點(diǎn),仔細(xì)分析了fileupload組件源代碼后,發(fā)現(xiàn)核心方法在FileUploadBase文件的findNextItem方法中,問(wèn)題在于fileupload組件解析完自定義的head節(jié)點(diǎn)后,卻忘記傳遞到FileItemStreamImpl中了,稍作修訂,即可修正該bug。
復(fù)制代碼 代碼如下:

/**解析文件列表
         * 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) {
                             //這里代碼要修訂
                        //這是原來(lái)的代碼,沒(méi)有傳入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;
            }

 

            /**創(chuàng)建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;
            }

相關(guān)文章

  • Java數(shù)組與字符串深入探索使用方法

    Java數(shù)組與字符串深入探索使用方法

    在今天的文章中,我將為你詳細(xì)講述Java學(xué)習(xí)中重要的一節(jié) [ 數(shù)組與字符串 ] ,帶你深入了解Java語(yǔ)言中數(shù)組的聲明、創(chuàng)建和初始化方法,字符串的定義以及常用到的操作方法
    2022-07-07
  • Java自動(dòng)化測(cè)試中多數(shù)據(jù)源的切換(實(shí)例講解)

    Java自動(dòng)化測(cè)試中多數(shù)據(jù)源的切換(實(shí)例講解)

    下面小編就為大家?guī)?lái)一篇Java自動(dòng)化測(cè)試中多數(shù)據(jù)源的切換(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-10-10
  • Java后端SSM框架圖片上傳功能實(shí)現(xiàn)方法解析

    Java后端SSM框架圖片上傳功能實(shí)現(xiàn)方法解析

    這篇文章主要介紹了Java后端SSM框架圖片上傳功能實(shí)現(xiàn)方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • 最新springboot中必須要了解的自動(dòng)裝配原理

    最新springboot中必須要了解的自動(dòng)裝配原理

    本文給大家介紹springboot中必須要了解的自動(dòng)裝配原理,spring-boot-dependencies:核心依賴都在父工程中,這個(gè)里面主要是管理項(xiàng)目的資源過(guò)濾及插件,本文對(duì)springboot自動(dòng)裝配原理給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2022-05-05
  • 使用JPA自定義VO類型轉(zhuǎn)換(EntityUtils工具類)

    使用JPA自定義VO類型轉(zhuǎn)換(EntityUtils工具類)

    這篇文章主要介紹了使用JPA自定義VO類型轉(zhuǎn)換(EntityUtils工具類),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • 解決SpringMVC使用@RequestBody注解報(bào)400錯(cuò)誤的問(wèn)題

    解決SpringMVC使用@RequestBody注解報(bào)400錯(cuò)誤的問(wèn)題

    這篇文章主要介紹了解決SpringMVC使用@RequestBody注解報(bào)400錯(cuò)誤的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-09-09
  • SpringBoot中自定義首頁(yè)(默認(rèn)頁(yè))及favicon的方法

    SpringBoot中自定義首頁(yè)(默認(rèn)頁(yè))及favicon的方法

    這篇文章主要介紹了SpringBoot中如何自定義首頁(yè)(默認(rèn)頁(yè))及favicon,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08
  • spring boot2.0圖片上傳至本地或服務(wù)器并配置虛擬路徑的方法

    spring boot2.0圖片上傳至本地或服務(wù)器并配置虛擬路徑的方法

    最近寫了關(guān)于圖片上傳至本地文件夾或服務(wù)器,上傳路徑到數(shù)據(jù)庫(kù),并在上傳時(shí)預(yù)覽圖片。本文通過(guò)實(shí)例代碼給大家分享spring boot2.0圖片上傳至本地或服務(wù)器并配置虛擬路徑的方法,需要的朋友參考下
    2018-12-12
  • Java進(jìn)程內(nèi)緩存框架EhCache詳解

    Java進(jìn)程內(nèi)緩存框架EhCache詳解

    這篇文章主要介紹了Java進(jìn)程內(nèi)緩存框架EhCache,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2021-12-12
  • IntelliJ IDEA cmd和idea Terminal查看java版本不一致的解決方案

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

    原來(lái)win10電腦上安裝的是jdk8的版本,因某些原因,現(xiàn)在想換成jdk7的版本,修改環(huán)境變量后,在cmd中執(zhí)行 [java -version]命令,顯示的是7的版本,遇到這樣的問(wèn)題如何解決呢?下面小編給大家分享IntelliJ IDEA cmd和idea Terminal查看java版本不一致的解決方案,一起看看吧
    2023-09-09

最新評(píng)論