springmvc(7) 文件上传

2020-12-13 02:32

阅读:565

标签:get   return   upload   path   turn   实现   byte   span   文件   

1.通过commons-fileupload来实现:导入jar包commons-fileupload和commons-io

2.配置springmvc:配置解析器:

   "multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        "defaultEncoding" value="utf-8"/>
        "maxUploadSize" value="10485760000"/>
        "maxInMemorySize" value="40960"/>
    

3.编写上传处理器代码:

    @RequestMapping("/upload")
    public String fileupload(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {
        //获取文件名
//        file.getOriginalFilename();
        String path ="/fileupload";
        InputStream is = file.getInputStream();
        OutputStream os = new FileOutputStream(new File(path,file.getOriginalFilename()));
        int len=0;
        byte[] buffer = new byte[400];
        while ((len = is.read(buffer)) != -1){
            os.write(buffer,0,len);
        }
        os.close();
        is.close();
        return "index.jsp";
    }

4.jsp页面:

  
"upload" method="post" enctype="multipart/form-data"> "file" name="file" value=""/>
"submit" value="submit">

 

springmvc(7) 文件上传

标签:get   return   upload   path   turn   实现   byte   span   文件   

原文地址:https://www.cnblogs.com/yuby/p/11042364.html


评论


亲,登录后才可以留言!