问题描述

最近需要使用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. NOIP 2005 校门外的树

    #include<iostream> #include<cstring> using namespace std; int a[10005]; int main() { mem ...

  2. 学习笔记之PHP

    学习 PHP,第 1 部分: 注册帐户.上传需要批准的文件.并查看和下载已批准的文件 https://www.ibm.com/developerworks/cn/opensource/tutorial ...

  3. 网页中显示pdf的方法

    非常好的在网页中显示pdf的方法 今天有一需求,要在网页中显示pdf,于是立马开始搜索解决方案,无意中发现一个非常好的解决方法,详见http://blogs.adobe.com/pdfdevjunki ...

  4. Selenium2+python自动化62-jenkins持续集成环境搭建

    前言 selenium脚本写完之后,一般是集成到jenkins环境了,方便一键执行. 一.环境准备 小编环境: 1.win10 64位 2.JDK 1.8.0_66 3.tomcat 9.0.0.M4 ...

  5. Hyberledger-Fabric 1.00 RPC学习(1)

    参考:http://hyperledger-fabric.readthedocs.io/en/latest/write_first_app.html 本文的目的就是基于Hyperledger Fabr ...

  6. 个人tools封装

    /** * jQuery 扩展 */(function ($) { $.fn.extend({ /** * 目标为任意对象元素 * 同时绑定单击和双击事件 */ bindClick: function ...

  7. 一劳永逸解决VLC播放中文字幕乱码问题

    VLC对于Mac/Ubuntu用户来说算得上是必备软件.其相当于PC机上的“暴风影音”,但Mac/Ubuntu的新手使用VLC播放avi时都会碰 到字幕乱码的问题.avi字幕的格式有多种,这里假设你使 ...

  8. Visual Studio 使用Web Deploy发布项目

    工具:Web Deploy 3.6 点击下载 (强烈推荐使用独立的Web Deploy 安装包安装) 使用 Web Platform Installer 安装 Web Deploy(3.5,3.6都安 ...

  9. sql日期查询

    select getdate() ,getdate()) ,getdate()) ) ,getdate())) ,getdate())) Select datename(weekday, getdat ...

  10. python模块的打包

    python模块的打包方法: http://blog.csdn.net/five3/article/details/7847551