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

本文介绍了一个关于commons-fileupload组件处理自定义head信息时出现的问题及其解决方案。通过修改源代码,在创建FileItemStreamImpl实例时传递FileItemHeaders参数,成功解决了组件忽略自定义head节点的问题。

解决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; } 

 

转载于:https://www.cnblogs.com/hehe520/archive/2010/02/25/6330191.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值