package com.ankaibei.workFlow.webadmin.ankaibei.utils;

import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author lwq
 * @version 1.0
 * @description:
 * @date 2022/9/22 11:20
 */
public class FileUtis {

    //服务器地址
    final static String baseUrl = "http://124.70.58.209:8077";
    //获取编译文件夹下的static路径
    static String basePath = "/ankaibei/upload";


    private final ServletContext context;

    public FileUtis(ServletContext context) {
        this.context = context;
    }

    /**
     * 上传文件
     *
     * @param upload
     * @param
     * @return
     * @throws IOException
     */
    public static String UploadFile(MultipartFile upload) throws IOException {
        String path = ResourceUtils.getURL("classpath:").getPath() + "static/upload";
        System.out.println(path);
        //判断该路径是否存在
        File file = new File(path);
        if (!file.exists()) {
            //如果这个文件夹不存在的话,就创建这个文件
            file.mkdirs();
        }
        //获取上传文件名称
        String filename = upload.getOriginalFilename();
        System.out.println(filename);
        //把文件名称设置成唯一值 uuid 以防止文件名相同覆盖
        String uuid = UUID.randomUUID().toString().replace("-", "");
        //新文件名
        filename = uuid + "_" + filename;
        System.out.println(filename);
        //完成文件上传
        upload.transferTo(new File(path, filename));
        String filePath = "upload/" + filename;
        System.out.println(filePath);
        return filePath;
    }

    /**
     * 单图片上传
     *
     * @param
     * @return
     */
    public static String uploadOne(MultipartFile voFile) throws FileNotFoundException {
        String filePath = ResourceUtils.getURL("classpath:").getPath() + "static/upload";
        File file = new File(filePath);
        if (!file.exists()) {//文件夹不存在
            file.mkdirs();//创建文件夹
        }
        //文件输出流
        FileOutputStream fileOutputStream = null;
        //缓冲输出流
        BufferedOutputStream bufferedOutputStream = null;
        //前端限制好，图片为空不让上传
        //获取前端的图片文件
//		MultipartFile voFile=vo.getImg();
        //要上传的图片文件
        File imgFile = null;
        //要上传的图片名
        String imgName = null;
        //上传后图片的url
        String imgUrl = null;
        try {
            //获取图片二进制数据
            byte[] bytes = voFile.getBytes();
            imgName = UUID.randomUUID() + ".jpg";
            imgFile = new File(file, imgName);
            //文件输出流对象
            fileOutputStream = new FileOutputStream(imgFile);
            //缓冲流对象
            bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
            //将图片数据写入文件
            bufferedOutputStream.write(bytes);
            //数据推出管道流
            bufferedOutputStream.flush();
            //关闭输出流
            bufferedOutputStream.close();
            //将图片的url写到类中
            imgUrl = baseUrl + filePath + "/" + imgName;
            System.out.println(imgUrl);
            return imgUrl;
        } catch (IOException e) {//发生IO异常
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 多图片上传
     *
     * @param
     * @return
     */
    public static List<String> uploadList(List<MultipartFile> voFileList) {
        String filePath = basePath;
        File file = new File(filePath);
        if (!file.exists()) {//文件夹不存在
            file.mkdirs();//创建文件夹
        }
        //文件输出流
        FileOutputStream fileOutputStream = null;
        //缓冲输出流
        BufferedOutputStream bufferedOutputStream = null;
        //前端限制好，图片为空不让上传
        //获取前端的图片文件
//		MultipartFile voFile=vo.getImg();
        //要上传的图片文件
        File imgFile = null;
        //要上传的图片名
        String imgName = null;
        //上传后图片的url
        String imgUrl = null;
        //单个文件
        MultipartFile voFile = null;
        //图片地址列表
        List<String> imgUrlList = new ArrayList<>();
        try {
            for (int i = 0; i < voFileList.size(); i++) {
                voFile = voFileList.get(i);
                //获取图片二进制数据
                byte[] bytes = voFile.getBytes();
                imgName = UUID.randomUUID() + ".jpg";
                imgFile = new File(file, imgName);
                //文件输出流对象
                fileOutputStream = new FileOutputStream(imgFile);
                //缓冲流对象
                bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
                //将图片数据写入文件
                bufferedOutputStream.write(bytes);
                //数据推出管道流
                bufferedOutputStream.flush();
                //关闭输出流
                bufferedOutputStream.close();
                //将图片的url写到类中
                imgUrl = baseUrl + filePath + "/" + imgName;
                imgUrlList.add(imgUrl);
            }
            return imgUrlList;
        } catch (IOException e) {//发生IO异常
            e.printStackTrace();
            return null;
        }
    }


}
