激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 快速解決commons-fileupload組件無法處理自定義head信息的bug

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

2019-10-11 11:42java教程網 Java教程

問題在于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;
            }

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91高清观看| 久草资源在线观看 | 国产亚洲高清在线精品不卡 | 日本免费一区二区三区四区 | 欧美日本亚洲视频 | 新久久久久久 | 亚洲视屏 | 黄视频网站免费 | 欧美人与牲禽动交精品一区 | 欧美日韩视频网站 | 国产刺激高潮av | 欧美日韩亚洲精品一区二区三区 | 制服丝袜成人动漫 | 欧美精品一级 | 美国一级黄色毛片 | 国产午夜精品久久久久久免费视 | 国产午夜免费不卡精品理论片 | 高清国产福利 | 免费毛片观看 | 欧美第1页 | 日韩精品中文字幕在线播放 | 欧美1区2区在线观看 | 亚洲欧美不卡视频 | 亚洲少妇诱惑 | 欧美成人一级 | 国产二区三区在线播放 | 午夜视频在线 | 欧美成人免费在线视频 | 国产精品wwww | 成人免费观看在线视频 | 国产精品亚洲一区二区三区在线观看 | 免费放黄网站在线播放 | 国产精品久久久久久久久久尿 | 亚洲欧美国产高清 | 久久中文一区 | 欧美国产精品久久 | 久久成人视屏 | 久久国产91| 99久久一区二区 | 91福利免费观看 | 午夜久久电影 |