zoukankan      html  css  js  c++  java
  • MultipartFile 转 File

    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;
        }
    

      

  • 相关阅读:
    Stop介绍
    django建站基本步骤
    Js 数组排序函数sort()
    前端面试基础-html篇之CSS3新特性
    前端面试基础-html篇之H5新特性
    2018年前端程序猿最好用的编辑器:visual studio code 常见配置
    适合手机端页面的轮播图,无插件,支持自动循环,一套轮播图可以适应所有的屏幕
    纯css3实现圆点围绕圆圈匀速无限旋转
    xpath 踩坑笔记01 通过a 文本内容标签定位元素
    python3学习记录3
  • 原文地址:https://www.cnblogs.com/jiehanshi/p/11736901.html
Copyright © 2011-2022 走看看