package com.winsun.utils;

import com.winsun.auth.core.util.IOUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @Author xuede
 * @Date 2020/3/6 10:29
 * @Version 1.0
 */
@Slf4j
public class FileUtil {

    //写到相应路径
    public  static boolean makefile(String path, MultipartFile file,String filename){
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File savePath = new File(dir, filename);
        OutputStream os = null;
        try {
            os = new FileOutputStream(savePath);
            os.write(file.getBytes());
            os.flush();
        } catch (IOException e) {
            log.error("上传文件失败：" + e.getMessage(), e);
            return false;
        } finally {
            IOUtils.closeQuite(os);
        }
        return true;
    }

    public  static boolean makefile1(String path, MultipartFile file,String filename){
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File savePath = new File(dir, filename);
        OutputStream os = null;
        try {
            os = new FileOutputStream(savePath);
            os.write(file.getBytes());
            os.flush();
        } catch (IOException e) {
            log.error("上传文件失败：" + e.getMessage(), e);
            return false;
        } finally {
            IOUtils.closeQuite(os);
        }
        return true;
    }
}
