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;
}
相关推荐:
优化,让生活与工作更高效-从个人到企业的全面升级,广州百度seo费用
GPT优化:让你的工作与生活更高效、更智能,Ai__79
线上AI写作免费一键生成,轻松提升写作效率,解放创作思维
优化网站的秘诀:提高网站速度与用户体验,助力业务腾飞,旅游网站建设步骤
seo软文有什么作用,seo文案是什么 ,斐乐Ai测评问题
seo需要了解什么,seo需要学些什么内容 ,ovo ai
如何用AI写公众号文章?让创作更高效、更轻松
ChatGPT出现错误503?你需要知道的解决方案和应对策略,ai拉伸字效
《收录情况:数字时代的网络信息检索与价值体现》,山东全域营销推广软件客服电话
用AI征文工具,轻松创作出精彩文章!
SEO精通:让你的内容在搜索引擎中脱颖而出,跨境电商 推广营销
AI生成网页模板,轻松打造专业网站,ai网格画法
SEO与SEM策略:提升网站流量与品牌曝光的双剑合璧,ai补图
SEO针对中小企业的增长潜力:如何通过精准优化抢占市场先机,网络推广和营销就选y火10星评价
SEO建议:如何通过优化提升网站流量,赢得市场竞争,自媒体网站免费推广平台
SEO优化与SEM广告:提升品牌曝光与流量的双重利器,ai接回头
ChatGPT不能用了?了解这一背后的真相及解决方法,ai恐怖头像
互联网资源的无限潜力:如何利用数字世界为个人和企业创造价值,信阳网站建设正规公司
SEO总计:如何通过SEO优化实现网站流量和排名的飞跃,三元桥网站优化
什么是seo伪原创,seo就业前景伪原创怎么写 ,头像ai画怎么弄
seo链接有什么用,seo外链是什么意思 ,one ai写作
seo竞价做的什么工作,seo 竞价 ,office智能ai
SEO搜索关键词是什么意思?让你轻松网站流量的秘密!,ai颜色不对
SEO这种营销方式,改变你网站流量的秘密武器,佛山网站设计建设
SEO抢权:如何在竞争激烈的市场中占得先机,正规网站建设口碑好
seo链接锚是什么,什么是锚链接,如何设置锚链接 ,保山智能AI
“新热度”:引领潮流的力量,如何趋势的脉搏,浙江通用网站建设特点
ChatGPT当前不可用?如何应对AI服务中断的挑战,ai文章免费写作app
seo适用于什么领域,seo适用于什么领域中 ,ai智能翻译写作机器人v1.0
ChatGPT5.0为什么一直没出来?背后的技术与战略,元宵节ai趣赏月
seo网络上什么意思,seo表示什么 ,如何避免今日头条ai写作检测
为什么选择SEO会员服务,让您的网站流量飞速增长,靖边专业网站建设公司
Chato1免费么?揭开这款AI聊天机器人的神秘面纱,交个朋友ai写作
GoogleSEO打满分自然流量会高吗?揭开SEO优化的真相,猪插画ai
什么是“神马关键词”?如何用它提升你的网络营销效果?,新闻发布网站怎么做推广
为什么做酒店seo,为什么做酒店 ,ai uehara下载
SEO找出网站流量提升的终极策略,带你走向搜索引擎巅峰!,朝阳模板网站建设价格
seo需要什么能力,seo需要哪些技术 ,ai长条弯曲
用AI写文,开启创作新时代
AI代写文章:高效创作的新风尚
SEO优化排-打造高效的网络营销利器,精神AI
GoogleGPT-引领未来智能对话的革命,灵境ai画质
seo矩阵运营中心是什么,seo矩阵运营中心是什么意思啊 ,北京ai特效
AI网页版本:开启智能时代的新篇章,拼音标调ai
ChatGPTO1免费:突破智能聊天的极限,体验AI无限可能,糯米ai唱歌
打破创作边界,无限可能无限制生成文章的AI
SEO全站优化:打造强大网站排名的必备利器,AI论文写作的优点
ChatGPT的梯子:突破网络壁垒,畅享智能对话的全新体验,ai的音标1001ai的音标
SEO优化引导新官网,助力企业迈向成功之路,宋轶ai换
企业关键字-助力企业成功的隐形动力,闽侯县企业网站建设