前台:
导入一下文件
swfobject.js
jquery-1.7.2.min.js
jquery.uploadify.v2.1.4.min.js
uploadify.css
给你一个容器按钮
<div><input type="file" name="uploadify" id="uploadify" value="00" class="uploadify-button"/></div>
$(document).ready(function () {
$("#uploadify").uploadify({
'uploader' : 'uploadify.swf',
'script' : '${pageContext.request.contextPath}/enclosure/upload',
'cancelImg' : 'img/cancel.png',
'folder' : 'upload',
'queueID' : 'fileQueue',
'auto' : false,
'multi' : true,
'simUploadLimit' : 2,
'buttonImg':'img/scfj.png',
'scriptData':{'applyMainId':$("#applyMainId").val()},
'onComplete' : function(event, ID,fileObj, response, data) {
location.href='${pageContext.request.contextPath}/enclosure/list?applyMainId='+${applyMainId};
}
});
})
<div id="fileQueue"></div>
cancel.png为自己本地删除图片,官方下载可以获得
后台:
@RequestMapping(value="/upload",method=RequestMethod.POST)
@ResponseBody
public String uploadify(@RequestParam("Filedata")MultipartFile multiFile,HttpServletRequest request, HttpServletResponse response,Integer applyMainId) throwsIOException, FileUploadException{
System.out.println("name:"+multiFile.getOriginalFilename());
System.out.println("inputstream"+multiFile.getInputStream());
ServletContext sc = request.getSession().getServletContext();
String dir = sc.getRealPath("/upload");//附件存放服务器的路径
System.out.println(dir);
File file = new File(dir);
if(!file.exists()){
file.mkdirs();
}
String filename = multiFile.getOriginalFilename();
//防止文件被覆盖,以纳秒生成文件
Long _l = System.nanoTime();
String _extfilename = filename.substring(filename.lastIndexOf("."));
filename = _l+_extfilename;
String uploadPath = request.getContextPath()+""+dir+"/"+filename;
String responseStr = "";
try {
FileUtils.writeByteArrayToFile(new File(dir, filename), multiFile.getBytes());
responseStr = "上传成功!";
TblEnclosure e = new TblEnclosure();
e.setUserId(1);
e.setCreateTime(new Date());
e.setName(filename);
e.setYname(multiFile.getOriginalFilename());
e.setSizes(String.valueOf(multiFile.getSize()));
e.setType(_extfilename);
e.setApplyMainId(applyMainId);
encloseureService.save(e);
} catch (Exception e) {
e.printStackTrace();
responseStr = "fail";
System.out.println("上传失败!");
return "";
}
return "1";
}