1、导入资源
2、JSP代码
选择图片
或将照片拖到这里,单次最多可选300张
0%
开始上传
3、Js代码
注意:
4、controller代码
@RequestMapping(value = "/upload" ) public String invoiceUpload(String proName,Long id,Mapmap){ map.put("proName", proName); projectService.getProject(id); map.put("projectId", id); return "project/invoiceUpload"; } @ResponseBody @RequestMapping(value = "/news_uploder", method ={RequestMethod.POST,RequestMethod.GET}, produces = "application/json; charset=utf-8") public JSonObject uploder(@RequestParam MultipartFile[] file,Long projectId,HttpServletRequest request,HttpServletResponse response){ String flag=null; JSonObject jsonObject = new JSonObject(); try { WebuploaderUtil webuploaderUtil=new WebuploaderUtil(); webuploaderUtil.upload(file[0], "upload/invoice/", request); flag=webuploaderUtil.getFileName(); jsonObject.put("filePath", flag); jsonObject.put("status", "success"); } catch (Exception e) { e.printStackTrace(); } return jsonObject; } 5、工具类 package com.softjx.util; import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.http.HttpServletRequest; import org.springframework.web.multipart.MultipartFile; public class WebuploaderUtil { private String allowSuffix = "jpg,png,gif,jpeg";//允许文件格式 private long allowSize = 2L;//允许文件大小 private String fileName; private String[] fileNames; public String getAllowSuffix() { return allowSuffix; } public void setAllowSuffix(String allowSuffix) { this.allowSuffix = allowSuffix; } public long getAllowSize() { return allowSize*1024*1024; } public void setAllowSize(long allowSize) { this.allowSize = allowSize; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String[] getFileNames() { return fileNames; } public void setFileNames(String[] fileNames) { this.fileNames = fileNames; } private String getFileNameNew(){ SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS"); return fmt.format(new Date()); } public void uploads(MultipartFile[] files, String destDir,HttpServletRequest request) throws Exception { String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path; try { fileNames = new String[files.length]; int index = 0; for (MultipartFile file : files) { String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1); int length = getAllowSuffix().indexOf(suffix); if(length == -1){ throw new Exception("请上传允许格式的文件"); } if(file.getSize() > getAllowSize()){ throw new Exception("您上传的文件大小已经超出范围"); } String realPath = request.getSession().getServletContext().getRealPath("/"); File destFile = new File(realPath+destDir); if(!destFile.exists()){ destFile.mkdirs(); } String fileNameNew = getFileNameNew()+"."+suffix;// File f = new File(destFile.getAbsoluteFile()+"\"+fileNameNew); file.transferTo(f); f.createNewFile(); fileNames[index++] =basePath+destDir+fileNameNew; } } catch (Exception e) { throw e; } } public void upload(MultipartFile file, String destDir,HttpServletRequest request) throws Exception { String path = request.getContextPath(); //http://localhost:8088/huahang String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path; try { String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1); int length = getAllowSuffix().indexOf(suffix); if(length == -1){ throw new Exception("请上传允许格式的文件"); } if(file.getSize() > getAllowSize()){ throw new Exception("您上传的文件大小已经超出范围"); } String realPath = request.getSession().getServletContext().getRealPath("/")+"/"; File destFile = new File(realPath+destDir); if(!destFile.exists()){ destFile.mkdirs(); } String fileNameNew = getFileNameNew()+"."+suffix; File f = new File(destFile.getAbsoluteFile()+"/"+fileNameNew); file.transferTo(f); f.createNewFile(); //包含网站的全路径http://localhost:8080/QDLIMAP/upload/user/20170414104142667.png //fileName = basePath+destDir+fileNameNew; //返回相对路径upload/user/20170414104142667.png fileName = destDir+fileNameNew; } catch (Exception e) { throw e; } } }
总结
以上所述是小编给大家介绍的webuploader 实现图片批量上传功能附实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!



