前端:
$('#updateFiles').uploadifive({
'auto': true, //是否自动上传,默认true
'multi': true, //是否多文件
'uploadScript' : '<%=request.getContextPath()%>/fileUpload/uploadFilesWithRealName', //地址
'buttonText' : '请选择文件',
'dnd' : false,//是否允许拖动上传
'removeCompleted' : false,//上传成功后会自动删除页面上的文件
'queueID' : false,
'fileType':['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel'],//文件类型
'fileObjName':'uploadify',//文件名称
'fileSizeLimit':'20MB',//文件大小
'onError' : function(errorType) {//异常处理
console.log('The error was: ' + errorType);
},
'onFallback': function () { //如果浏览器没有兼容的HTML5文件API功能,则在初始化期间触发
alert('浏览器不支持HTML5,无法上传!');
},
'onProgress' : function(file, e) {//上传进度条和大小
if (e.lengthComputable) {
var percent = Math.round((e.loaded / e.total) * 100);
}
file.queueItem.find('.fileinfo').html(' - ' + percent + '%');
file.queueItem.find('.progress-bar').css('width', percent + '%');
},
'onUploadComplete' : function(file, data) {//成功处理
if(data){
var json=data;
$('#savePath').val(json);
$('#fileName').val(file.name);
}else{
console.info("失败");
}
}
});
后台
/**
* @desc 多文件上传
* @return
*/
@RequestMapping(value="/uploadFilesWithRealName")
@ResponseBody
public String uploadFilesWithRealName(MultipartHttpServletRequest request, @MatrixVariable(required=false) String jsessionid) {
List<String> filePaths = new ArrayList<>();
try {
List<MultipartFile> files = request.getFiles("uploadify");
for (int i = 0; i < files.size(); i++) {
MultipartFile file = files.get(i);
// 把文件上传到临时文件夹
String filePath = FileUtil.saveFileToTempDir(file.getInputStream(), file.getOriginalFilename(), true);
String aesPath = URLEncoder.encode(AESUtil.getInstance().encrypt(filePath), Constants.CHAR_ENCODING_UTF8);
filePaths.add(aesPath);
}
return StringUtils.join(filePaths, ",");
// 回传数据失败的时候
} catch (Exception e) {
e.printStackTrace();
return null;
}
}