FileUtils.java 1.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
package cn.wisenergy.common.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * Created by m1991 on 2021/2/26 18:56
 */
public class FileUtils {
        /**
         * @param file     文件
         * @param path     文件存放路径
         * @param fileName 保存的文件名
         * @return
         */
        public static boolean upload(MultipartFile file, String path, String fileName) {

            //确定上传的文件名
21
            String realPath = path + fileName;
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
            System.out.println("上传文件:" + realPath);

            File dest = new File(realPath);

            //判断文件父目录是否存在
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdir();
            }

            try {
                //保存文件
                file.transferTo(dest);
                return true;
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            }

        }

}