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中

猜你喜欢

转载自www.cnblogs.com/dbutil/p/9441073.html