springMVC+uplodify实现异步上传
最近在做课程设计的时候准备实现一个异步上传的功能
在网上查找jQuery的上传插件,找到了这个uploadify插件
简单的看了下上传时候的demo感觉样式不错,于是决定拿来用一下。
首先来说目前uploadfiy插件分为HTML5版本(收费)和flash版本
此处谈论flash版本
下面是插件的简单用法
$(‘#file_upload’).uploadify({
‘swf’:’resources/js/uploadify.swf’,
‘uploader’:’picUpload’
});
以上两个是必须的参数
第一个是指定的动画文件,后面为动画的路径
第二个是指定的上传的路径(服务器接收路径)
如果需要更详尽的参数可以去官网查找。官网doc地址
在使用springMVC接收参数时有问题,request接收到的参数,始终在转换为MultipartHttpServletRequest的时候有问题,查原因,一般在同步接收的时候,都是提交form表单的,需要在表单中红增加ENCTYPE=”multipart/form-data “这么个东西。然后查了下uploadify文档没有找到相应的参数设置,于是放弃,使用传统的servlet的方式。
在此贴出解决后的代码,所需jar(commons-fileupload-1.2.1.jar,commons-io-1.3.2.jar)
@RequestMapping(value=”/{picUpload}”, method=RequestMethod.POST)
public String picUpload(HttpServletRequest request,ModelMap model, HttpSession session) throws IOException {
DiskFileItemFactory fac = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(fac);
upload.setHeaderEncoding(“utf-8”);
List fileList = null;
try {
fileList = upload.parseRequest(request);
} catch (FileUploadException ex) {
}
Iterator<FileItem> it = fileList.iterator();
String name = “”;
String extName = “”;
while (it.hasNext()) {
FileItem item = it.next();
if (!item.isFormField()) {
name = item.getName();
long size = item.getSize();
String type = item.getContentType();
//System.out.println(size + ” ” + type);
if (name == null || name.trim().equals(“”)) {
continue;
}
// 扩展名格式:
if (name.lastIndexOf(“.”) >= 0) {
extName = name.substring(name.lastIndexOf(“.”));
}
File file = null;
do {
// 生成文件名:
name = UUID.randomUUID().toString();
file = new File(“D://” + name + extName);
//System.out.println(savePath + name + extName);
} while (file.exists());
File saveFile = new File(“D://” + name + extName);
try {
item.write(saveFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return “”;
}
如果有哪位高手,使用uploadify插件能够用spring的MultipartHttpServletRequest来接收的,请不吝赐教。