欢迎来到[小脾气]的博客

做人要像铜钱,内方外圆

Stringboot MultiPartFile 转 File

1.首先在pom中引入包

<dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
</dependency>

2.然后上代码

@RequestMapping("/upload")
    public void file(@RequestParam("file")MultipartFile file,HttpServletRequest request) throws IOException{
        if(!file.isEmpty()){
            String filePath = request.getSession().getServletContext().getRealPath("/")+"upload/";
            System.err.println("filePath:"+filePath);
            File dir = new File(filePath);
            if(!dir.exists()){
                dir.mkdir();
            }
            String path = filePath + file.getOriginalFilename();
            System.err.println("path:"+path);
            File temFile = null;
            try {
                temFile = new File(path);
                FileUtils.copyInputStreamToFile(file.getInputStream(), temFile);
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
    }

最后就转到了tenFile中

posted on 2018-08-08 09:59  嚯嚯,小脾气  阅读(210)  评论(0编辑  收藏  举报

导航