使用Springmvc框架实现多文件上传(二)
上一个小demo中,我们使用的是传入数组的方式,接下来我们可以使用单个入参的方式也是可以的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | /*//多文件上传 @RequestMapping(value="/useraddsave.html",method=RequestMethod.POST) public String addUserSave(User user,HttpSession session,HttpServletRequest request, @RequestParam(value ="a_idPicPath", required = false) MultipartFile idPicFile, @RequestParam(value ="a_workPicPath", required = false) MultipartFile workPicFile){ String idPicPath = null; String workPicPath = null; String path = request.getSession().getServletContext().getRealPath("statics"+File.separator+"uploadfiles"); logger.info("uploadFile path ============== > "+path); //判断文件是否为空(证件照) if(!idPicFile.isEmpty()){ String oldFileName = idPicFile.getOriginalFilename();//原文件名 logger.info("uploadFile oldFileName ============== > "+oldFileName); String prefix=FilenameUtils.getExtension(oldFileName);//原文件后缀 logger.debug("uploadFile prefix============> " + prefix); int filesize = 500000; logger.debug("uploadFile size============> " + idPicFile.getSize()); if(idPicFile.getSize() > filesize){//上传大小不得超过 500k request.setAttribute("uploadFileError", " * 上传大小不得超过 500k"); return "useradd"; }else if(prefix.equalsIgnoreCase("jpg") || prefix.equalsIgnoreCase("png") || prefix.equalsIgnoreCase("jpeg") || prefix.equalsIgnoreCase("pneg")){//上传图片格式不正确 String fileName = System.currentTimeMillis()+RandomUtils.nextInt(1000000)+"_Personal.jpg"; logger.debug("new fileName======== " + idPicFile.getName()); File targetFile = new File(path, fileName); if(!targetFile.exists()){ targetFile.mkdirs(); } //保存 try { idPicFile.transferTo(targetFile); } catch (Exception e) { e.printStackTrace(); request.setAttribute("uploadFileError", " * 上传失败!"); return "useradd"; } idPicPath = path+File.separator+fileName; }else{ request.setAttribute("uploadFileError", " * 上传图片格式不正确"); return "useradd"; } } //判断文件是否为空(工作证照) if(!workPicFile.isEmpty()){ String oldFileName = workPicFile.getOriginalFilename();//原文件名 logger.info("uploadFile oldFileName ============== > "+oldFileName); String prefix=FilenameUtils.getExtension(oldFileName);//原文件后缀 logger.debug("uploadFile prefix============> " + prefix); int filesize = 500000; logger.debug("uploadFile size============> " + workPicFile.getSize()); if(workPicFile.getSize() > filesize){//上传大小不得超过 500k request.setAttribute("uploadWpError", " * 上传大小不得超过 500k"); return "useradd"; }else if(prefix.equalsIgnoreCase("jpg") || prefix.equalsIgnoreCase("png") || prefix.equalsIgnoreCase("jpeg") || prefix.equalsIgnoreCase("pneg")){//上传图片格式不正确 String fileName = System.currentTimeMillis()+RandomUtils.nextInt(1000000)+"_Personal.jpg"; logger.debug("new fileName======== " + workPicFile.getName()); File targetFile = new File(path, fileName); if(!targetFile.exists()){ targetFile.mkdirs(); } //保存 try { workPicFile.transferTo(targetFile); } catch (Exception e) { e.printStackTrace(); request.setAttribute("uploadWpError", " * 上传失败!"); return "useradd"; } workPicPath = path+File.separator+fileName; }else{ request.setAttribute("uploadWpError", " * 上传图片格式不正确"); return "useradd"; } } user.setCreatedBy(((User)session.getAttribute(Constants.USER_SESSION)).getId()); user.setCreationDate(new Date()); user.setIdPicPath(idPicPath); user.setWorkPicPath(workPicPath); if(userService.add(user)){ return "redirect:/user/userlist.html"; } return "useradd"; }*/ |
分类:
SpringMVC
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY