zoukankan      html  css  js  c++  java
  • Base64Encoder和Base64Decoder问题解决

    • 场景:前端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
  • 相关阅读:
    day 34
    day 33 线程锁
    day 32 操作系统、线程和进程(GIL锁)
    day 31 网络基础的补充
    day 30 多线程 socketserver模块补充
    python自学笔记 2019/07/01
    类与对象的概念
    递归及三种二分法
    好看的颜色
    zend 汉化
  • 原文地址:https://www.cnblogs.com/LJing21/p/12156142.html
Copyright © 2011-2022 走看看