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()));

  

文件流和文件名称转File

    public static File inputStreamToFile(InputStream inputStream, String fileName, String bh) throws Exception {
        if (inputStream == null) {
            return 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);

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

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

        //为文件添加流信息
        OutputStream os = new FileOutputStream(toFile);
        IOUtils.copy(inputStream, os);
        return toFile;
    }

  

 

相关文章:

  • 2022-12-23
  • 2021-12-03
  • 2021-09-14
  • 2021-05-30
  • 2022-12-23
  • 2021-06-13
  • 2021-11-14
  • 2022-12-23
猜你喜欢
  • 2021-12-27
  • 2022-02-14
  • 2022-12-23
  • 2021-06-14
  • 2022-12-23
相关资源
相似解决方案