解决spring MVC上传配置冲突。保留springmvc的配置,修改controller中的代码。
controller
@Controller
public class UploadController {
@RequestMapping("/toUpload")
public String page(){
return "upload";
}
@RequestMapping("/upload")
@ResponseBody
public String uploadifyUpload(@RequestParam(value = "file") MultipartFile[] files, String key, Integer num) throws IOException {
String savePath = "D:\\snsoft-upload";
savePath = savePath + "\\uploads\\";
File f1 = new File(savePath);
if (!f1.exists()) {
f1.mkdirs();
}
String name = "";
String extName = "";
for (MultipartFile mf : files) {
if(!mf.isEmpty()){
name = mf.getOriginalFilename();
long size = mf.getSize();
String type = mf.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 = System.currentTimeMillis()+"";
file = new File(savePath + name + extName);
} while (file.exists());
File saveFile = new File(savePath + name + extName);
try {
mf.transferTo(saveFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return name + extName;
}
}
jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"
+request.getServerName()+":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<meta http-equiv="keywords" content="Pess">
<meta http-equiv="description" content="Pess">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="renderer" content="webkit">
<title>上传</title>
<script type="text/javascript" src="static/js/jquery-1.7.2.min.js"></script>
<link href="plug-in/uploadify/uploadify.css" rel="stylesheet" type="text/css"/>
<script src="plug-in/uploadify/jquery.uploadify.js" type="text/javascript"></script>
<script src="plug-in/uploadify/jquery.uploadify.min.js" type="text/javascript"></script>
<style type="text/css">
</style>
</head>
<body>
<div id="fileQueue"></div>
<a id="uploadify">开始上传</a>
<a href="javascript:$('#uploadify').uploadify('cancel')">取消上传</a>
<input type="text">
</body>
<script type="text/javascript">
$(function () {
$("#uploadify").uploadify({
'swf' : 'plug-in/img/uploadify.swf',
'uploader' : '/upload',
'fileObjName' : 'file',
'formData' : {'key':'value','num':2},
'fileTypeExts' : '*.gif; *.jpg; *.png',
'fileSizeLimit' : '20kb',
'progressData' : 'percentage',
'fileTypeDesc': 'Image Files',
'buttonText' : '上传文件',
'buttonClass' :'btn',
'height': 30,
'width': 120,
'cancel': 'plug-in/img/uploadify-cancel.png',
'auto': true,
'multi': true,
'onUploadComplete' : function(file) {
alert('上传完成');
},
'onUploadSuccess' : function (file, data, response) {
alert(file.name+"|"+data);
},
'onUploadError' : function(file, errorCode, errorMsg, errorString) {
alert('The file' + file.name + ' could not be uploaded: ' + errorString);
},
'onCancel' : function(file) {
alert('取消上传');
}
});
});
</script>
</html>
谢谢支持!