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.BASE64Decoderr的JDK内部API。代码如下:

转码全部代码:
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);
}
}
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;
} } }
sun.misc.BASE64Decoder的风险的更多相关文章
- sun.misc.BASE64Decoder 限制取消
sun.misc.BASE64Decoder Windows -> Preferences -> Java -> Compiler -> Errors/Warnings -&g ...
- eclipse 中 import sun.misc.BASE64Decoder; 报错
from://http://blog.sina.com.cn/s/blog_48964b120101ahrf.html 在android做3DES加密功能时 eclipse 中 import sun. ...
- 使用sun.misc.BASE64Decoder出错解决方案
Access restriction: The type BASE64Decoder is not accessible due to restriction on required library ...
- sun.misc.BASE64Decoder导入异常及处理思路
Java后台保存base64图片数据 使用byte[] bytes = new BASE64Decoder().decodeBuffer(str);需要引入sun.misc.BASE64Decoder ...
- 解决报错:import sun.misc.BASE64Decoder无法找到
解决报错:import sun.misc.BASE64Decoder无法找到 2017年09月29日 16:03:26 chaoyu168 阅读数:2116 标签: sun.misc.BASE64De ...
- JDK从1.8升级到9.0.1后sun.misc.BASE64Decoder和sun.misc.BASE64Encoder不可用
目录 描述 原因分析 处理办法 参考 描述 最近研究把项目的JDK升级从1.8升级到9.0.1,在eclipse上配置好JDK为9后,发现项目有错,查看发现sun.misc.BASE64Decoder ...
- sun.misc.BASE64Decoder 替代
加密解密经常用到sun.misc.BASE64Decoder处理,编译时会提示: sun.misc.BASE64Decoder是内部专用 API, 可能会在未来发行版中删除 解决办法: Java8以后 ...
- Ant 警告:sun.misc.BASE64Decoder 是 Sun 的专用 API,可能会在未来版本中删除
如果你用Ant编译项目,而且在项目中用了SUN的专用API,你会得到警告信息,然后Ant会报告编译失败: 这当然是不合理的,javac只是警告而已,ant凭什么就直接报失败呢? 其实最好的解决办法是避 ...
- 解决import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder;报错的问题
在项目中用到这两个Jar包,但是程序报错. Access restriction: The type BASE64Decoder is not accessible due to restrictio ...
随机推荐
- redhat 连接mysql数据库Can't get hostname for your address
redhat 连接mysql数据库Can't get hostname for your address Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQ ...
- vim配置之powerline
vimConfig/plugin/vim-powerline-setting.vim let g:Powerline_symbols = 'fancy'
- bzoj4403 两个串
Description 兔子们在玩两个串的游戏.给定两个字符串S和T,兔子们想知道T在S中出现了几次, 分别在哪些位置出现.注意T中可能有“?”字符,这个字符可以匹配任何字符. Input 两行两个字 ...
- 二.jQuery源码解析之构建jQuery之构建函数jQuery的7种用法
一:$(selectorStr[,限制范围]),接受一个选择器(符合jQuery规范的字符串),返回一个jQuery对象; 二:$(htmlStr[,文档对象]),$(html[,json对象])传入 ...
- 关于在github上 下载源码 clone 非 master 分支的代码
https://blog.csdn.net/u012302552/article/details/80680497
- vs2010 出现“未能将 ProteusDebugEngine 调试器附加到计算机”
vs2010 打开项目时出现如下图错误,解决方法: 1.查看C:\Progream Files下的Internet Explorer文件夹还在不在,不在则会出现此问题: 2.可以右键项目属性-调试-勾 ...
- python类静态变量
python的类静态变量直接定义在类中即可,不需要修饰符,如: 1 class Test: stc_attr = 1 def __init__(self,attr1,attr2): self.attr ...
- leetcode374
// Forward declaration of guess API. // @param num, your guess // @return -1 if my number is lower, ...
- Java 堆栈,内存分配理解
Java虚拟机的堆.栈.堆栈 https://www.zhihu.com/question/29833675
- javax.persistence.RollbackException: Error while committing the transaction
the valid jpa update entity code gives the exception below in the case of wrong dependency( org.hib ...