[英]Tomcat 6.0 Large File Upload (> 2 GB)
我在使用大於2GB的文件上傳HTTP文件時遇到問題。 服務器和客戶端都是64位,因此從系統角度來看,必須沒有2GB的限制。 我做了以下事情:
我正在使用apache commons文件上傳。 我還嘗試使用ServerFileUpload setMaxFileSize方法設置最大文件大小。
我能夠上傳小於2GB的文件(我成功嘗試了1.88GB文件)。 請指導我,我在這里錯過了什么?
更具體的說,ServletFileUpload.parseRequest方法在上傳大文件時返回0 FileItems
這是代碼片段:
if (isMultipartForm()) {
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(SIZE_THRESHOLD);//SIZE_THRESHOLD = 4MB
ServletFileUpload upload = new ServletFileUpload(factory);
//upload.setFileSizeMax(3000000000L); Tried this too
upload.setProgressListener(progressListener);
items = upload.parseRequest(request);
if(items != null && items.size() == 0)
return new CommandResultSet(false, "NoItemsFoundInRequest");
return new CommandResultSet(true, "" + ( (items!=null) ? items.size() : ""));
} catch(FileUploadException e) {
e.printStackTrace();
System.out.println("Exception in MultipartFormManager. Can not parse request.");
return new CommandResultSet(false, e.getMessage());
}
}
如果您使用的是common-fileupload 1.1或者舊版本,則無法上傳(1.9888)~2GB。 這里的問題是這個jar正在調用一個名為getContentLength的方法,該方法是int類型,因此您的請求只能處理大小為Integer.MAX_VALUE的大小。 在像1.3這樣的common-fileupload的最新版本中,這已經解決了。 希望這可以幫助。
我當然可能出現新的錯誤,但我還沒有發現即使64位瀏覽器也能處理大於2GB的上傳。 問題不是服務器而是瀏覽器。 您會發現奇怪的是,大多數現代瀏覽器都會很樂意從標准服務器下載大於2GB的文件,無需特殊配置。
Jsp code :
<script src="../lib/app/configurator.data.ajax.js" type="text/javascript"></script>
<script src="../lib/ui/jquery.fileupload.js"></script>
<html>
<script language="Javascript">
var varb = '';
var test = 0;
var count = 1;
$(function () {
var maxChunkSize = 30000000; //SET CHUNK SIZE HERE
var your_global_var_for_form_submit = '';
var params = {
year: threeDSelectedYear,
series: threeDSelectedSeries
};
/*File upload bind with form, 'fileupload' is my form id. As sumit triggers
for file ulaod will submit my form*/
/* replaceFileInput: true, */
$('#fileupload').fileupload({
maxChunkSize: maxChunkSize,
url: efccustom.getEfcContext()+'upload/uploadZip?year='+threeDSelectedYear+'&series='+threeDSelectedSeries, //URL WHERE FILE TO BE UPLOADED
error: function (jqXHR, textStatus, errorThrown) {
// Called for each failed chunk upload
$(".fileupload-loading").html("");
$('.ANALYZE_DIALOG').dialog("close");
},
success: function (data, textStatus, jqXHR) {
/*This event will be called on success of upload*/
count = parseInt($('#counter').val()) + 1;
$('#counter').val(count);
$('#ttk').val(count);
data.ttk = 7;
console.log(" count ",count , data);
},
submit: function (e, data) {
/*This event will be called on submit here i am
adding variable to form*/
//console.log($('#zip_uploaded_file').val());
//console.log(data.originalFiles[0].name);
test = data.originalFiles[0];
$('#fname').val(data.originalFiles[0].name);
$('#trequests').val(Math.ceil(data.originalFiles[0].size/maxChunkSize));
$('#counter').val('1');
},
progress: function (e, data) {
/*PROGRESS BAR CODE WILL BE HERE */
$(".fileupload-loading").html('<img src="../public/themeroller/images/throbber.gif" alt="Uploading Please Wait..." title="Uploading Please Wait...." />');
},
add: function (e, data) {
$('.browsedFileName').html(data.originalFiles[0].name);
your_global_var_for_form_submit = data;
},
done: function (e, data) {
ajaxcall.Data._get('upload/extractZipNCreateJson',params,function(data2) {
alert("file upload success ");
$(".fileupload-loading").html("");
$('.ANALYZE_DIALOG').dialog("close");
});
}
});
/*This is my button click event on which i am submitting my form*/
$('#button').click(function(){
your_global_var_for_form_submit.submit();
});
});
</script>
<html>
<body>
<form id="fileupload" enctype="multipart/form-data">
<div class="row fileupload-buttonbar">
<div class="span7">
<!--<input type="file" name="files" id="file" /> -->
<input type="file" id="zip_uploaded_file" name="zip_uploaded_file" />
<input type="hidden" name="counter" id="counter" value="1" />
<input type="hidden" name="fname" id="fname" value="" />
<input type="hidden" name="trequests" id="trequests" value="1" />
<input type="hidden" name="ttk" id="ttk" value="1" />
<input type="button" id="button" name="button" value="submit" />
<span class='browsedFileName'></span>
</div>
</div>
<!-- The loading indicator is shown during file processing -->
<div class="fileupload-loading"></div>
</form>
</body>
Controller COde :
@RequestMapping(value = "/uploadZip", method = RequestMethod.POST, consumes = "multipart/form-data")
@ResponseBody
public ResponseEntity<String>
uploadZip(HttpServletRequest request, HttpServletResponse response)
throws IOException, IllegalArgumentException {
try {
String year = request.getParameter("year");
String series = request.getParameter("series");
log.info(" year " + year + " series " + series);
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = upload.parseRequest(request);
Iterator iterator = items.iterator();
HttpSession session = request.getSession();
UserContext userContext = (UserContext) session
.getAttribute(ApplicationConstant.USER_CONTEXT);
String userName = userContext.getUser();
String workSpaceInUse = userContext.getCurrentWorkSpace();
FileItem item = null;
boolean fileNotExistFlag;
String fileExtension;
while (iterator.hasNext()) {
item = (FileItem) iterator.next();
String content = item.getContentType();
log.info(" content "+content);
log.info(" File Type Getting Uploaded :"+content);
if (!item.isFormField()) {
/* Here in IOUtils the Third Parameter true tells that the small chunks of data that comes need to be added to the same File */
IOUtils.copy(fileItem.getInputStream(), new FileOutputStream(new File(threeDPath+"/"+year+"/"+series+"/"+uploadZipFileName),true));
}
}
return null;
}
}
catch(Exception e) {
return handleError(new RuntimeException("Unexpected error while uploading ZIP", e));
}
return null;
}
如果您希望上傳這些尺寸的文件,我不會依賴直接的瀏覽器上傳。 Java applet甚至可能是Flash文件(不確定,如果可能,不是Flash人員)將是我的建議,因此您可以將文件分成塊。 如果上傳中斷,您可以從上次停止的位置繼續。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.