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.BASE64DecoderrJDK內部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;
        }

    }


}

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM