问题描述

最近需要使用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的风险的更多相关文章

  1. sun.misc.BASE64Decoder 限制取消

    sun.misc.BASE64Decoder Windows -> Preferences -> Java -> Compiler -> Errors/Warnings -&g ...

  2. eclipse 中 import sun.misc.BASE64Decoder; 报错

    from://http://blog.sina.com.cn/s/blog_48964b120101ahrf.html 在android做3DES加密功能时 eclipse 中 import sun. ...

  3. 使用sun.misc.BASE64Decoder出错解决方案

    Access restriction: The type BASE64Decoder is not accessible due to restriction on required library ...

  4. sun.misc.BASE64Decoder导入异常及处理思路

    Java后台保存base64图片数据 使用byte[] bytes = new BASE64Decoder().decodeBuffer(str);需要引入sun.misc.BASE64Decoder ...

  5. 解决报错:import sun.misc.BASE64Decoder无法找到

    解决报错:import sun.misc.BASE64Decoder无法找到 2017年09月29日 16:03:26 chaoyu168 阅读数:2116 标签: sun.misc.BASE64De ...

  6. JDK从1.8升级到9.0.1后sun.misc.BASE64Decoder和sun.misc.BASE64Encoder不可用

    目录 描述 原因分析 处理办法 参考 描述 最近研究把项目的JDK升级从1.8升级到9.0.1,在eclipse上配置好JDK为9后,发现项目有错,查看发现sun.misc.BASE64Decoder ...

  7. sun.misc.BASE64Decoder 替代

    加密解密经常用到sun.misc.BASE64Decoder处理,编译时会提示: sun.misc.BASE64Decoder是内部专用 API, 可能会在未来发行版中删除 解决办法: Java8以后 ...

  8. Ant 警告:sun.misc.BASE64Decoder 是 Sun 的专用 API,可能会在未来版本中删除

    如果你用Ant编译项目,而且在项目中用了SUN的专用API,你会得到警告信息,然后Ant会报告编译失败: 这当然是不合理的,javac只是警告而已,ant凭什么就直接报失败呢? 其实最好的解决办法是避 ...

  9. 解决import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder;报错的问题

    在项目中用到这两个Jar包,但是程序报错. Access restriction: The type BASE64Decoder is not accessible due to restrictio ...

随机推荐

  1. 自定义linux命令

    ​ 方法一.修改/etc/bashrc文件 在文件底部加入 alias zone="cd /usr/local/webserver" 在命令行输入zone,则会直接进入到制定目录 ...

  2. Shell实现Unix进程间信息交换的几种方法

    本文将介绍在SCO OpenServer5.0.5系统中使用shell语言来实现进程间信息交换的几种方法: 使用命名管道实现进程间信息交换 使用kill命令和trap语句实现进程间信息交换 使用点命令 ...

  3. laravel Hash密码 校对

    laravel加密 是使用hash不可逆的,但是可以对加密后的密码进行校对 $data = $r->all();$id = $data['id'];$user_password = bcrypt ...

  4. coding github 配置ssl 免密拉取代码

    详细介绍: https://www.cnblogs.com/superGG1990/p/6844952.html 注:其中检验过程与下述不同,可以先在对应git库使用 git pull 一次,选择信任 ...

  5. HDU 2199 Can you solve this equation?(二分精度)

    HDU 2199 Can you solve this equation?     Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == ...

  6. 10-28质量监控ELK

    监控业务范围 app崩溃监控(Bugly) 应用性能监控(APM) 业务监控(TalkingData.友盟) 质量监控(缺位) 质量监控平台ELK elk官网 数据构造 线上错误状态分布 故障影响范围 ...

  7. F5 源地址保持

    Virtusl Server: Web Portal 建议选择成Source_Addr(源地址保持).因为Web Portal提供WEB服务给用户访问,压力比较大,而源地址保持的方式处理速度比Cook ...

  8. POJ 2777-题解

    一.题意 给你一排N个格子,M种颜色,P个操作.有两种操作:(1)C A B D:把[A, B]区间内的所有格子涂成颜色D.(2)P A B:输出[A, B]区间内的颜色的种类数.注意,初始颜色为1. ...

  9. Web Api HelpPage

    为了方面APP开发人员,服务端的接口都应当提供详尽的API说明.但每次有修改,既要维护代码,又要维护文档,一旦开发进度紧张,很容易导致代码与文档不一致. Web API有一个Help Page插件,可 ...

  10. OpenCL 使用函数 clCreateProgramWithBinary 来创建程序

    ▶ 函数 clCreateProgramWithSource 接收 OpenCL 代码(设备无关)来创建程序,而函数 clCreateProgramWithBinary 接收已经经过函数 clBuil ...