麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Java > 正文

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

2019-11-26 15:59:51
字體:
來源:轉載
供稿:網友
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;
            }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 精品三区视频 | 成年片在线观看 | 免费看真人a一级毛片 | 国产精品久久久久久久久久大牛 | 韩国一大片a毛片 | 久久国产在线观看 | 久久国产精品久久久久 | 色妞妞视频| 天天骑夜夜操 | 免费视频一区 | 精品国产专区 | 色999中文字幕 | 免费的毛片 | 久久看免费视频 | 亚洲生活片 | 成av在线| 嫩草91在线 | 久久国产成人精品国产成人亚洲 | 激情综合视频 | 青青草在线免费观看 | 日日操夜 | 欧产日产国产精品99 | 午夜国产福利 | 免费一级欧美大片视频 | 日韩a毛片免费观看 | 2021免费日韩视频网 | 四季久久免费一区二区三区四区 | 精品国产乱码久久久久久丨区2区 | 国产精品久久久久久久久久尿 | 久久久精彩 | 精品久久久久久久久久中文字幕 | 欧美一级特黄aaaaaaa什 | 中国杭州少妇xxxx做受 | 在线免费日本 | 成人综合在线观看 | 中文字幕一二三区芒果 | 热99re久久免费视精品频软件 | 久久夜夜视频 | www.48xx.com | 国产成人精品午夜视频' | 国产在线精品一区二区三区不卡 |