- 场景:前端base64图片内容传到后端,通过BASE64Decoder将图片Base64码转成上传流,上传成功后。更新jenkins时,更新失败。
- 问题:Java 9版本之后Base64Encoder和Base64Decoder无法继续使用?使用jenkins打包更新时无法使用BASE64Decoder的包(sun.misc.Base64Encoder和sun.misc.Base64Decoder无法使用)?
- 原因:JDK中的/lib/tool.jar和/lib/rt.jar已经从Java SE 9中删除。
- 改前:
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; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; /** * 图片上传工具类 */ public class Base64 implements MultipartFile { private final byte[] imgContent; private final String header; public Base64(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); } /** * 将图片Base64 码转成上传流 */ 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 Base64(b, baseStrs[0]); } catch (IOException e) { e.printStackTrace(); return null; } } }
- 改后:
import org.springframework.web.multipart.MultipartFile; import java.util.Base64; import java.util.Base64.Decoder; import java.io.*; /** * 图片上传工具类 */ public class Base64Util implements MultipartFile { private final byte[] imgContent; private final String header; public Base64Util(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); } /** * 将图片Base64 码转成上传流 */ public static MultipartFile base64ToMultipart(String base64) { String[] baseStrs = base64.split(","); String imgBase64 = base64.replaceAll("data:image/png;base64,",""); Decoder decoder = Base64.getDecoder(); byte[] result = decoder.decode(imgBase64); return new Base64Util(result, baseStrs[0]); } }
- 参考:https://blog.csdn.net/xie_sining/article/details/80777164