Base64.java 1.03 KB
Newer Older
1 2
package cn.timer.api.utils.baidu;

3
import org.springframework.web.multipart.MultipartFile;
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
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字符串
     */
25
    public static String getStrFromPath(MultipartFile file) {
26 27 28 29

        InputStream in = null;
        byte[] data = null;
        try {
30
            in = file.getInputStream();
31 32 33 34 35 36 37 38 39
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        // 返回Base64编码过再URLEncode的字节数组字符串
40
        return encoder.encode(data);
41 42
    }
}