zoukankan      html  css  js  c++  java
  • 一次性上传多个文件到服务器端(一)

    页面代码:

    <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

  • 相关阅读:
    集群资源队列监控:Grafana
    1
    3月9号
    jmx
    日常笔记
    nsenter命令简介
    一天2小时
    postgresql Centos7部署
    笔记5
    1
  • 原文地址:https://www.cnblogs.com/heyang78/p/6257585.html
Copyright © 2011-2022 走看看