1、JSP页面:
<input type="file" name="file_upload" id="file_upload1"/>
2、JS :
// 初始化上传附件按钮
init_upload();
/**
* 营业执照图片上传
*/
function init_upload(){
$("#file_upload1").uploadify({
'swf': basePath + 'resources/uploadify/uploadify.swf',
'uploader': basePath + 'basSupplierInfo/uploadFiles.do',
'cancelImg': basePath + 'resources/uploadify/uploadify-cancel.png',
//'langFile':basePath + 'resources/uploadify/lang.js',
'preventCaching': true, // 加随机数到URL后,防止缓存
// 'folder': 'upload',
'buttonText' : '选择文件',
'progressData ': 'percentage',//显示上传进度方式
//'queueID' : 'custom-queue',
//'queueSizeLimit' : 99, // 队列长度
// 'method' : "post",
'fileTypeExts' : '*.jpg;*.png;',
'removeCompleted' : false,// 上传完成后是否删除队列中的对应元素
//'removeTimeout' : 10,//上传完成后多少秒后删除队列中的进度条,
//'requeueErrors' : true, // 上传失败后重新加入队列
// 'buttonCursor': 'hand', // 上传按钮Hover时的鼠标形状
'auto':true, //是否自动上传
'multi':false, //多文件上传
'uploadLimit' : 1, //最多上传文件数量
'fileObjName':'file_upload',
//'fileSizeLimit':2000,// 文件大小限制, 200 KB
'onUploadSuccess' : function(file, data, response) { //上传成功操作后
$("#licensePicName").val(file.name);
$("#licensePicUrl").val($.parseJSON(data).fileNameOfUUID); //String转成json串
var cancel=$("#"+file.id + " .cancel a");
if (cancel) {
cancel.on('click',function () {
//通过uploadify的settings方式重置上传限制数量
$('#file_upload1').uploadify('settings','uploadLimit', ++uploadLimit1);
//防止手快多点几次x,把x隐藏掉
$(this).hide();
});
}
},
'onUploadError' : function(file, errorCode, errorMsg, errorString){
//alert(errorMsg);
}
});
}
3、后台方法:
/**
* 处理文件上传
* @param response
* @param request
* @return
* @throws IOException
*/
@RequestMapping(value="/uploadFiles",method=RequestMethod.POST)
@ResponseBody
public Map<String, Object> upload(HttpServletResponse response,HttpServletRequest request) throws IOException{
Map<String, Object> resultMap = new HashMap<String, Object>();
String responseStr="";
MultipartHttpServletRequest multipartRequest =(MultipartHttpServletRequest) request;
Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
//String savePath = this.getServletConfig().getServletContext().getRealPath("");
//savePath = savePath + "/uploads/";
// 文件保存路径 ctxPath本地路径
/*String ctxPath=request.getSession().getServletContext().getRealPath("/")+File.separator+"uploadFiles";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
String ymd = sdf.format(new Date());
ctxPath += File.separator + ymd + File.separator;
System.out.println("ctxpath="+ctxPath);
// 创建文件夹
File file = new File(ctxPath);
if (!file.exists()) {
file.mkdirs();
}
*/
//指定文件上传目录
String ctxPath = PropertiesUtil.readValue("params.properties", "supplierRegisterFile.path");
System.out.println("ctxpath="+ctxPath);
String fileName = null;
String fileNameOfUUID = null;
for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
// 上传文件
MultipartFile mf = entity.getValue();
fileName = mf.getOriginalFilename();//获取原文件名
System.out.println("filename="+fileName);
String uuid = UUID.randomUUID().toString().toUpperCase();
fileNameOfUUID = uuid+fileName.substring(fileName.lastIndexOf("."));
//fileNameOfUUID=fileName.substring(0,fileName.lastIndexOf("."))+uuid+fileName.substring(fileName.lastIndexOf("."));
System.out.println("fileNameOfUUID="+fileNameOfUUID);
File uploadFile = new File(ctxPath + File.separator + fileNameOfUUID);
try {
FileCopyUtils.copy(mf.getBytes(), uploadFile);
responseStr="上传成功";
} catch (IOException e) {
responseStr="上传失败";
e.printStackTrace();
}
}
resultMap.put("fileName", fileName);
resultMap.put("fileNameOfUUID", fileNameOfUUID);
return resultMap;
}