最近在做文件上传的一个项目,由于上传的文件为镜像文件,体积很大,一般的插件无法满足,采用SWFUpload插件则可以实现大文件的上传,经过测试单个文件3.5g大小可以实现上传,22g的文件则上传失败。
SWFUpload插件相对于其他插件有以下优点:1.单个文件可以很大 2.可以同时上传多个文件到队列中 3.拓展性很强。
以下是基于jfinal的一个简单实例。
1.html部分
<html>
<head>
<link href="css/default.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="css/button.css" type="text/css" />
<script type="text/javascript" src="js/swfupload/swfupload.js"></script>
<!-- <script type="text/javascript" src="js/swfupload/swfupload.queue.js"></script> -->
<script type="text/javascript" src="js/swfupload/handlers.js"></script>
<script type="text/javascript" src="../../js/mirror/myswfupload.js"></script>
</head>
<body style="background-color: #C0D1E3; padding: 2px;">
<div id="content">
<form>
<div
style="display: inline; border: solid 1px #7FAAFF; background-color: #C5D9FF; padding: 2px;">
<span id="spanButtonPlaceholder"></span>
<input id="btnUpload" type="button" value="上 传"
οnclick="startUploadFile();" class="btn3_mouseout"
/>
<input id="btnCancel" type="button" value="取消所有上传"
οnclick="cancelUpload();" disabled="disabled" class="btn3_mouseout"
/>
</div>
</form>
<div id="divFileProgressContainer"></div>
<div id="thumbnails">
<table id="infoTable" border="0" width="530" style="display: inline; border: solid 1px #7FAAFF; background-color: #C5D9FF; padding: 2px;margin-top:8px;">
</table>
</div>
</div>
</body>
</html>
2.核心js部分
(function($, $w){
var swfu ;var file_opts;
$(document).ready(function(){
createNewSWFVo();
});
function createNewSWFVo(){
file_opts = initOpstion();
swfu = new SWFUpload(file_opts);
}
function initOpstion(){
return {
upload_url: "/cloud-manage/mirror/ swfupload",
post_params: {"name" : "zwm"},
use_query_string:true,
// File Upload Settings
file_size_limit : "10 GB", // 文件大小控制
file_types : "*.*",
file_types_description : "All Files",
file_upload_limit : "0",
file_queue_error_handler : fileQueueError,
file_dialog_complete_handler : fileDialogComplete,//选择好文件后提交
file_queued_handler : fileQueued,
upload_progress_handler : uploadProgress,
upload_error_handler : uploadError,
upload_success_handler : uploadSuccess,
upload_complete_handler : uploadComplete,
button_placeholder_id : "spanButtonPlaceholder",
button_width: 89,
button_height:29,
button_text : '<span class="button"> 选择文件 </span>',
button_text_style : '.button { font-family:"Microsoft YaHei", Arial, sans-serif; font-size: 14px; color:#333}.button:hover{color:#0895da}',
button_text_top_padding: 0,
button_text_left_padding: 0,
button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
button_cursor: SWFUpload.CURSOR.HAND,
// Flash Settings
flash_url : "/cloud-manage/js/swfupload/swfupload.swf",
custom_settings : {
upload_target : "divFileProgressContainer"
},
// Debug Settings
debug: false //是否显示调试窗口
};
}
function startUploadFile(){
$(".progressContainer.blue").show();
swfu.startUpload();
}
function clearLastFileUploadInfo(){
$(".progressName").text("");
$(".progressBarStatus").find("font").text("");
$(".progressContainer.blue").hide();
}
$w.upload = $w.upload || {};
$w.upload.startUploadFile = startUploadFile;
// $w.upload.openUploadListWin = openUploadListWin;
})(jQuery, window);
3.controller部分(upload_url请求上传的地址)
/*注意要引入的是这几个包,否则容易出错*/
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
*文件上传
*/
public void swfupload(){
DiskFileItemFactory factory = new DiskFileItemFactory();
// 设置内存缓冲区,超过后写入临时文件
factory.setSizeThreshold(10240000);
// 设置临时文件存储位置
//String base = "/files/xk";
String base = "/swfuploaFile";
File file = new File(base);
if(!file.exists())
file.mkdirs();
factory.setRepository(file);
ServletFileUpload upload = new ServletFileUpload(factory);
// 设置单个文件的最大上传值
upload.setFileSizeMax(10002400000l);
// 设置整个request的最大值
upload.setSizeMax(10002400000l);
upload.setHeaderEncoding("UTF-8");
try {
HttpServletRequest request =getRequest();
List<?> items = upload.parseRequest(request);
FileItem item = null;
String fileName = null;
for (int i = 0 ;i < items.size(); i++){
item = (FileItem) items.get(i);
fileName = base + File.separator + item.getName();
// 保存文件
if (!item.isFormField() && item.getName().length() > 0) {
item.write(new File(fileName));
}
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
renderJson(new ResultJson(ResultCode.SUCCESS));
}
未完,待续。。。