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 ...
随机推荐
- CenOS中的yum配置文件CentOS-Base.repo里面的参数都是何含义? souhu CentOS-Base.repo
souhu yum服务器CentOS-Base.repo 将$releasever替换为操作系统版本号 # CentOS-Base.repo # # The mirror system uses t ...
- git hooks 的学习使用
示例网址: https://my.oschina.net/u/3477605/blog/1806238 https://www.jianshu.com/p/935409ce4c9a https://w ...
- Quick guide for converting from JAGS or BUGS to NIMBLE
Converting to NIMBLE from JAGS, OpenBUGS or WinBUGS NIMBLE is a hierarchical modeling package that u ...
- Ubuntu下VIM使用指南
基本命令: Esc:VIM中的万能功能键之一,基本上任何时候按这个键,都可以返回VIM的普通状态. i:在普通状态下按i可以进入“插入”编辑状态,这个时候按方向键移动光标,在想要输入的地方输入字符,用 ...
- 《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #9 RT Group Scheduling 与RT Throttling
HACK #9 RT Group Scheduling 与RT Throttling 本节介绍对实时进程所使用的CPU时间进行限制的功能RT Group Scheduling和RT Throttlin ...
- Linux下分析某个进程CPU占用率高的原因
Linux下分析某个进程CPU占用率高的原因 通过top命令找出消耗资源高的线程id,利用strace命令查看该线程所有系统调用 1.top 查到占用cpu高的进程pid 2.查看该pid的线程 ...
- Spring 3.1 entityManagerFactory java.lang.NoSuchFieldError: NULL Error
This means there is a version mismatch--most likely with spring classes. So make sure all your sprin ...
- LUA表的引用理解
--lua中引用类型都是分配在堆上的 --因此,我们在使用LUA的table时,可尽可能的使用表的引用,而不需要拷贝表里的元素 --比如,通过RPC协议传来一个表A,我们想要缓存这个表,只需要保存该表 ...
- 编程语言分类和Python解释器介绍
主要内容: 一.编程语言分类和介绍 二.Python解释器分类和介绍 一.编程语言分类和介绍 1.编程语言分类 编程语言的发展依次经历了机器语言.汇编语言和高级语言. 2.编程语言的介绍 2.1 ...
- java.lang.VerifyError: Inconsistent stackmap frames at branch target 81
java项目中有如下代码: @RequestMapping(value = "/getMxList") @ResponseBody public Map<String, Ob ...