快速解決commons-fileupload組件無(wú)法處理自定義head信息的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) {
//這里代碼要修訂
//這是原來(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;
}
- java組件commons-fileupload實(shí)現(xiàn)文件上傳、下載、在線打開
- Java組件commons fileupload實(shí)現(xiàn)文件上傳功能
- JavaEE組件commons-fileupload實(shí)現(xiàn)文件上傳、下載
- JSP組件commons-fileupload實(shí)現(xiàn)文件上傳
- java組件commons-fileupload文件上傳示例
- Apache Commons fileUpload文件上傳多個(gè)示例分享
- java組件commons-fileupload實(shí)現(xiàn)文件上傳
- commons fileupload實(shí)現(xiàn)文件上傳的實(shí)例代碼
- Apache Commons fileUpload實(shí)現(xiàn)文件上傳之一
- Apache commons fileupload文件上傳實(shí)例講解
相關(guān)文章
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-10Java后端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)裝配原理,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工具類),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11解決SpringMVC使用@RequestBody注解報(bào)400錯(cuò)誤的問(wèn)題
這篇文章主要介紹了解決SpringMVC使用@RequestBody注解報(bào)400錯(cuò)誤的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09SpringBoot中自定義首頁(yè)(默認(rèn)頁(yè))及favicon的方法
這篇文章主要介紹了SpringBoot中如何自定義首頁(yè)(默認(rèn)頁(yè))及favicon,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08spring boot2.0圖片上傳至本地或服務(wù)器并配置虛擬路徑的方法
最近寫了關(guān)于圖片上傳至本地文件夾或服務(wù)器,上傳路徑到數(shù)據(jù)庫(kù),并在上傳時(shí)預(yù)覽圖片。本文通過(guò)實(shí)例代碼給大家分享spring boot2.0圖片上傳至本地或服務(wù)器并配置虛擬路徑的方法,需要的朋友參考下2018-12-12Java進(jìn)程內(nèi)緩存框架EhCache詳解
這篇文章主要介紹了Java進(jìn)程內(nèi)緩存框架EhCache,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2021-12-12IntelliJ 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