Base64.java 1.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
package cn.timer.api.utils.baidu;

import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

/**
 * @author wuqingjun
 * @email 284718418@qq.com
 * @date 2022/3/29
 */
public class Base64 {

    private static HttpURLConnection httpUrl = null;

    /**
     * 转化成Base64字符串
     */
    public static String getStrFromPath(MultipartFile file) {

        InputStream in = null;
        byte[] data = null;
        try {
            in = file.getInputStream();
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        // 返回Base64编码过再URLEncode的字节数组字符串
        return encoder.encode(data);
    }
}