疾速處理commons-fileupload組件沒法處置自界說head信息的bug。本站提示廣大學習愛好者:(疾速處理commons-fileupload組件沒法處置自界說head信息的bug)文章只能為提供參考,不一定能成為您想要的結果。以下是疾速處理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;
}