1,编写后台common io/common upload多文件上传实现
@SuppressWarnings("rawtypes")
@RequestMapping("batchUpload")
public void batchFileUpload(HttpServletRequest request,
HttpServletResponse response) throws Exception {
DiskFileItemFactory factory = new DiskFileItemFactory();
PrintWriter out = response.getWriter();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");// 解决上传中文文件名乱码
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
List files = upload.parseRequest(request);
Iterator item = files.iterator();
String path = "";
String filename = "";
if (isMultipart) {
while (item.hasNext()) {
FileItem fileitem = (FileItem) item.next();
if (!fileitem.isFormField()) {
String username = request.getSession()
.getAttribute("username").toString();
filename = fileitem.getName();
FileListBean fileListBean = new FileListBean();
fileListBean.setFilename(filename);
fileListBean.setCreate_person(username);
fileListBean.setCreate_date(new Date());
String appPath = request.getSession().getServletContext()
.getRealPath("/");
appPath = appPath + "upload\\";
path = appPath + filename;
File file = new File(path);
if (!file.exists()) {
file.createNewFile();
}
fileitem.write(file);
this.batchFileuploadService.addFileList(fileListBean);
}
}
}
out.flush();
out.close();
}
2,配置前台页面plupload实现
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>batchFileUpload.html</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3" />
<meta http-equiv="description" content="This is my page" />
<link rel="stylesheet"
href="/dbzyAna/javascript/themes/base/jquery-ui.css"
type="text/css" />
<link rel="stylesheet"
href="/dbzyAna/javascript/js/jquery.ui.plupload/css/jquery.ui.plupload.css"
type="text/css" />
<script src="/dbzyAna/javascript/jquery.min.js"></script>
<script type="text/javascript"
src="/dbzyAna/javascript/ui/minified/jquery-ui.min.js"></script>
<!-- production -->
<script type="text/javascript" src="/dbzyAna/javascript/js/plupload.full.min.js"></script>
<script type="text/javascript"
src="/dbzyAna/javascript/js/jquery.ui.plupload/jquery.ui.plupload.js"></script>
<script type="text/javascript">
// Initialize the widget when the DOM is ready
$(function() {
$("#uploader").plupload({
// General settings
runtimes : 'html5,flash,silverlight,html4',
url : '/dbzyAna/batchUpload',
// User can upload no more then 20 files in one go (sets multiple_queues to false)
max_file_count : 20,
//此属性用于将文件切割
//chunk_size : '10mb',
// Resize images on clientside if we can
resize : {
width : 200,
height : 200,
quality : 90,
crop : true
// crop to exact dimensions
},
filters : {
// Maximum file size
max_file_size : '100mb',
// Specify what files to browse for
mime_types : [ {
title : "Excel files",
extensions : "xlsx"
}, {
title : "Zip files",
extensions : "zip"
} ]
},
// Rename files by clicking on their titles
rename : true,
// Sort files
sortable : true,
// Enable ability to drag'n'drop files onto the widget (currently only HTML5 supports that)
dragdrop : true,
// Views to activate
views : {
list : true,
thumbs : true, // Show thumbs
active : 'thumbs'
},
// Flash settings
flash_swf_url : '/dbzyAna/javascript/js/Moxie.swf',
// Silverlight settings
silverlight_xap_url : '/dbzyAna/javascript/js/Moxie.xap'
});
});
</script>
</head>
<body>
<form id="form1">
<div id="uploader">
<p>You browser doesn't have Flash, Silverlight, Gears,
BrowserPlus or HTML5 support.</p>
</div>
</form>
</body>
</html>
java配置plupload
最新推荐文章于 2025-04-15 16:29:47 发布