spring mvc 多文件上传

来源:互联网 发布:淘宝装修做满页面 编辑:程序博客网 时间:2024/05/11 19:56

@Controller:

    //文件上传
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public void handleRequestInternal(      
       MultipartHttpServletRequest request) throws Exception {       
       List<MultipartFile> files=request.getFiles("file");      
        //将文件存入硬盘
        String uploadpath = request.getSession().getServletContext().getRealPath("/");
        System.out.println(uploadpath);
        System.out.println(files.isEmpty());
        for (MultipartFile file:files) {    
            if (file.isEmpty()) continue;
            System.out.println("ok");
            System.out.println(file.getOriginalFilename());
            FileOutputStream fileOS = new FileOutputStream(uploadpath + "/attached/" + file.getOriginalFilename());
            fileOS.write(file.getBytes());
            System.out.println(fileOS);
            fileOS.close();
        }
    }   

页面:

<input type="file" name="file"/>  
 <input type="file" name="file"/> 






原创粉丝点击