chenjiahe
2023-09-04 399ee057f3bef4ca0465995b6486df761e913d73
src/main/java/com/hx/util/FileConvertTool.java
@@ -1,11 +1,14 @@
package com.hx.util;
import com.hx.exception.TipsException;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.io.IOUtils;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.*;
import java.net.URL;
@@ -184,59 +187,6 @@
    }
    /**
     * 根据byte数组,生成文件
     */
    public static File getFile(String baseIn, String filePath, String fileName) {
        if(baseIn == null){
            return null;
        }
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            InputStream in = parse_inputStream(baseIn);
            if(in == null){
                return null;
            }
            File dir = new File(filePath);
            //判断文件目录是否存在
            if(!dir.exists()){
                dir.mkdirs();
            }
            file = new File(filePath+"/"+fileName);
            fos = new FileOutputStream(file);
            byte[] b = new byte[BUFFER_SIZE];
//            while ((in.read(b)) != -1) {
//                fos.write(b); // 写入数据
//            }
            int len;
            while ((len = in.read(b)) != -1){
                fos.write(b, 0, len);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return file;
    }
    /**
     * 获得指定文件的byte数组
     */
    private static byte[] getBytes(String filePath){
@@ -350,4 +300,36 @@
            return false;
        }
    }
    public static String encodeBase64File(File file) throws Exception {
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] bytes = new byte[fileInputStream.available()];
        // 读取到 byte 里面
        fileInputStream.read(bytes);
        fileInputStream.close();
        BASE64Encoder base64Encoder = new BASE64Encoder();
        // 得到文件 之后转成beye 然后使用base64转码
        // 转码
        String encode = base64Encoder.encode(bytes);
         return encode;
    }
    public static String encodeBase64File(MultipartFile multipartFile) throws Exception {
        if (multipartFile == null) {
            throw new TipsException("未检查到上传的文件!");
        }
        String imageBaseStr = null;
        try {
            String contentType = multipartFile.getContentType();
            byte[] imageBytes = multipartFile.getBytes();
            BASE64Encoder base64Encoder = new BASE64Encoder();
//            imageBaseStr = "data:" + contentType + ";base64," + base64Encoder.encode(imageBytes);
            imageBaseStr = base64Encoder.encode(imageBytes);
            imageBaseStr = imageBaseStr.replaceAll("[\\s*\t\n\r]", "");
        } catch (IOException e) {
            throw new TipsException("文件转换base64异常");
        }
        //返回生成的编码
        return imageBaseStr;
    }
}