MultipartFile 转 File

风流意气都作罢 提交于 2019-12-02 08:12:36
public static File multipartFileToFile(MultipartFile file, String bh) throws Exception {
        if (file.getSize() <= 0) {
            return null;
        }
        File toFile = null;
        // 用户主目录
        String userHome = System.getProperties().getProperty("user.home");
        StringBuilder filepath = new StringBuilder();
        filepath.append(userHome).append(File.separator).append("files").append(File.separator).append(bh).append(File.separator);

        //创建文件夹
        toFile = new File(filepath.toString());
        FileUtils.forceMkdir(toFile);

        //创建文件,此时文件为空
        filepath.append(file.getOriginalFilename());
        toFile = new File(filepath.toString());

        //为文件添加流信息
        file.transferTo(toFile);
        return toFile;
    }

  

删除file

//删除文件夹String bh = "";
String userHome = System.getProperties().getProperty("user.home");
StringBuilder filepath = new StringBuilder();
filepath.append(userHome).append(File.separator).append("files").append(File.separator).append(bh);
FileUtils.deleteDirectory(new File(filepath.toString()));

  

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!