sun.misc.BASE64Decoder的风险

问题描述

最近需要使用Base64上传图片,但是返现sun.misc.BASE64Decoder 为已经过期的包,此包为以前sun公司的内部包,可以下载此包,但是不利于现在Maven方式构建,可能会在未来发行版中删除。

需要注意sun.misc包中的内容是JDK内部api,项目直接引用存在风险,在执行install命令进行项目编译过程中,日志中会出现警告。

警告内容为sun.misc.BASE64Decoder是内部专用 API, 可能会在未来发行版中删除。

 

 

修复办法

 

使用JDK 1.8提供的java.util.Base64.Decoder的公共API,可代替sun.misc.BASE64DecoderrJDK内部API。代码如下:

 

 转码全部代码:

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
 
import org.springframework.web.multipart.MultipartFile;
/**
 * base64的上传文件的实现类
 * @author zhanghl
 *
 */
public class BASE64DecodedMultipartFile implements MultipartFile {
 
    private final byte[] imgContent;
    private final String header;
 
    public BASE64DecodedMultipartFile(byte[] imgContent, String header) {
        this.imgContent = imgContent;
        this.header = header.split(";")[0];
    }
 
    @Override
    public String getName() {
        // TODO - implementation depends on your requirements
        return System.currentTimeMillis() + Math.random() + "." + header.split("/")[1];
    }
 
    @Override
    public String getOriginalFilename() {
        // TODO - implementation depends on your requirements
        return System.currentTimeMillis() + (int)Math.random() * 10000 + "." + header.split("/")[1];
    }
 
    @Override
    public String getContentType() {
        // TODO - implementation depends on your requirements
        return header.split(":")[1];
    }
 
    @Override
    public boolean isEmpty() {
        return imgContent == null || imgContent.length == 0;
    }
 
    @Override
    public long getSize() {
        return imgContent.length;
    }
 
    @Override
    public byte[] getBytes() throws IOException {
        return imgContent;
    }
 
    @Override
    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(imgContent);
    }
 
    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {
        new FileOutputStream(dest).write(imgContent);
    }
}

  

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import org.springframework.web.multipart.MultipartFile;
 
import java.util.Base64;
 
 
/**
 * @author zhanghl
 */
public class Base64ToMultipart {
    /**
     * 使用sun.misc.BASE64Decoder和sun.misc.BASE64Encoder的JDK内部API(不建议使用)
     * base64转Multipartfile
     *
     * @param base64
     * @return
     */
//    public static MultipartFile base64ToMultipart(String base64) {
//        try {
//            String[] baseStrs = base64.split(",");
//
//            BASE64Decoder decoder = new BASE64Decoder();
//            byte[] b = new byte[0];
//            b = decoder.decodeBuffer(baseStrs[1]);
//
//            for (int i = 0; i < b.length; ++i) {
//                if (b[i] < 0) {
//                    b[i] += 256;
//                }
//            }
//
//            return new BASE64DecodedMultipartFile(b, baseStrs[0]);
//        } catch (IOException e) {
//            e.printStackTrace();
//            return null;
//        }
//    }
 
    /**
     * 使用 java.util.Base64.Decoder和java.util.Base64.Encoder的JDK公共API(推荐)
     * base64转Multipartfile
     * @param base64
     * @return
     */
    public static MultipartFile base64ToMultipart(String base64) {
        try {
            String[] baseStrs = base64.split(",");
            Base64.Decoder decoder = Base64.getDecoder();
            byte[] b = decoder.decode(baseStrs[1]);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            return new BASE64DecodedMultipartFile(b, baseStrs[0]);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
 
    }
 
 
}

  

 

posted @   zhangheliang  阅读(5806)  评论(1编辑  收藏  举报
编辑推荐:
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
阅读排行:
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法
点击右上角即可分享
微信分享提示