一次性上传多个文件到服务器端(一)

页面代码:

<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进行处理就行了。

参考资料:

http://www.cnblogs.com/jingch/p/5036686.html

posted @   逆火狂飙  阅读(779)  评论(0编辑  收藏  举报
编辑推荐:
· 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)
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东
点击右上角即可分享
微信分享提示