一次性上传多个文件到服务器端(一)
页面代码:
<form name="form1" action="uploadFileJunior.html" method="post" ENCTYPE="multipart/form-data"> <input id="File1" name="fileupload" accept="file/*.*" multiple="multiple" type="file" value="" /> <input id="" type="submit" value="上传" /> </form>
上面实现多选的关键是multiple="multiple"一句。
后台处理代码(后台是SpringMVC的Controller):
@RequestMapping("/uploadFileJunior") public String uploadFileJunior(@RequestParam("fileupload") MultipartFile[] files,HttpServletRequest request,HttpServletResponse response){ try { int count=files.length; for(int i=0;i<count;i++){ MultipartFile file=files[i]; InputStream is=file.getInputStream(); String fileName=file.getOriginalFilename(); System.out.println("File"+(i+1)+":"+fileName); String newFilepath=fileService.upload2Server(is,fileName); logger.info(i+":"+file.getOriginalFilename()+" "+" newFilepath="+newFilepath); } return "/pages/sample/index.jsp"; } catch (Exception e) { e.printStackTrace(); logger.error(e); request.setAttribute("error", e.getClass()); request.setAttribute("reason", e.getMessage()); StackTraceElement[] arr=e.getStackTrace(); request.setAttribute("stackTraceElements", arr); return "pages/error/index.jsp"; } }
注意好上面两个fileupload的映射关系,后台这边对应的变量数组是files,之后针对files进行处理就行了。
参考资料:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)