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

    package com.hswg.jstxb.issued.common.FileUtil;/**
     * Created by TongGuoBo on 2019/6/19.
     */
     
    import org.springframework.web.multipart.MultipartFile;
     
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.OutputStream;
     
    /**
     * @ClassName MultipartFileToFile
     * @Description MultipartFile转fie
     * @Author TongGuoBo
     * @Date 2019/6/19 13:48
     **/
    public class MultipartFileToFile {
     
        /**
         * MultipartFile 转 File
         *
         * @param file
         * @throws Exception
         */
        public static File multipartFileToFile(MultipartFile file) throws Exception {
     
            File toFile = null;
            if (file.equals("") || file.getSize() <= 0) {
                file = null;
            } else {
                InputStream ins = null;
                ins = file.getInputStream();
                toFile = new File(file.getOriginalFilename());
                inputStreamToFile(ins, toFile);
                ins.close();
            }
            return toFile;
        }
     
        //获取流文件
        private static void inputStreamToFile(InputStream ins, File file) {
            try {
                OutputStream os = new FileOutputStream(file);
                int bytesRead = 0;
                byte[] buffer = new byte[8192];
                while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                    os.write(buffer, 0, bytesRead);
                }
                os.close();
                ins.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
     
        /**
         * 删除本地临时文件
         * @param file
         */
        public static void delteTempFile(File file) {
        if (file != null) {
            File del = new File(file.toURI());
            del.delete();
        }
    }
    }
    

      

  • 相关阅读:
    Maven的作用
    redis持久化的几种方式
    3.持续交付实战用户管理服务
    MySQL 一些概念
    Git学习笔记--定制
    Git学习笔记--标签
    Git学习笔记--分支
    Git学习笔记--远程仓库
    Git学习笔记--版本控制
    Git学习笔记--init、add、commit
  • 原文地址:https://www.cnblogs.com/xiadongqing/p/15633011.html
Copyright © 2011-2022 走看看