工具类:
package com.hongfang.employment_platform.until;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.CharsetUtil;
import com.hongfang.employment_platform.constant.FileType;
import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.StandardCharsets;
/**
- Base64的工具类
- @author: WeiXin
- @date: Created in 2022/4/21 17:38
- @version: 1.0
*/
public class Base64Utils {
/**
* Base64的前置附加固定值
*/
public static String Base64Salt = "hfep";
/**
* 对原始路径进行base64加密
* @param originalUrl
* @return
*/
public static String encryptCodeByBase64(String originalUrl){
if(StringUtils.isBlank(originalUrl)){
throw new NullPointerException("原始Url为空");
}
String base64Url = Base64Salt + Base64.encode(originalUrl);
return base64Url;
}
/**
* sun公司自带的base64加密
* @param key
* @return
* @throws Exception
*/
public static String encryptBASE64(byte[] key){
if(key == null || key.length == 0 ){
throw new NullPointerException("加密key为空");
}
String base64Key = Base64Salt + (new BASE64Encoder()).encodeBuffer(key);
return base64Key;
}
/**
* sun公司自带的base64解密
* @param key
* @return
* @throws Exception
*/
public static String decryptBASE64(String key){
if(StringUtils.isBlank(key)){
throw new NullPointerException("解密key为空");
}
String result = null;
key = key.substring(Base64Salt.length());
try {
byte[] bytes = (new BASE64Decoder()).decodeBuffer(key);
result = new String(bytes, "UTF-8");
}catch (IOException var1){
var1.printStackTrace();
}
return result;
}
/**
* 对base64路径进行解密
* @param base64Url
* @return
*/
public static String decipheringCodeByBase64(String base64Url){
if(StringUtils.isBlank(base64Url)){
throw new NullPointerException("加密Url为空");
}
String sub = base64Url.substring(Base64Salt.length());
return Base64.decodeStr(sub);
}
/**
* java8自带的工具类Base64加密
* @param base64Url
* @return
*/
public static String encryptBASE64ByOrigin(String base64Url){
if(StringUtils.isBlank(base64Url)){
throw new NullPointerException("加密Url为空");
}
return Base64Salt + java.util.Base64.getEncoder().encodeToString(base64Url.getBytes());
}
/**
* java8自带的工具类Base64解密
* @param targetUrl
* @return
*/
public static String decryptBASE64ByOrigin(String targetUrl){
if(StringUtils.isBlank(targetUrl)){
throw new NullPointerException("解密Url为空");
}
String result = null;
targetUrl = targetUrl.substring(Base64Salt.length());
try {
byte[] bytes = java.util.Base64.getDecoder().decode(targetUrl.replaceAll(" ", "+"));
result = new String(bytes, "UTF-8");
}catch (UnsupportedEncodingException var1){
var1.printStackTrace();
}
return result;
}
/**
* ppt文件的base64路径编码转成pdf文件的base64路径编码
* @param base64Url
* @return
*/
public static String generatePDFCodeByBase64(String base64Url){
String pptFilePath = decipheringCodeByBase64(base64Url);
String subPPTFilePath = pptFilePath.substring(0, pptFilePath.lastIndexOf(".")) + FileType.FILE_TYPE_PDF;
return encryptCodeByBase64(subPPTFilePath);
}
/**
* ppt文件的base64路径编码转成pdf文件的base64路径编码
* @param base64Url
* @return
*/
public static String generatePDFCodeBySunBase64(String base64Url){
String pptFilePath = decryptBASE64(base64Url);
String pdfFilePath = null;
String subPPTFilePath = pptFilePath.substring(0, pptFilePath.lastIndexOf(".")) + FileType.FILE_TYPE_PDF;
try{
pdfFilePath = encryptBASE64(subPPTFilePath.getBytes("UTF-8"));
}catch (UnsupportedEncodingException var1){
var1.printStackTrace();
}
return pdfFilePath;
}
public static void main(String[] args) {
String str = "hfepQzovVXBsb2FkRmlsZXMvMjAyMi0wNi0wMS/lhbPkuo7kuL7lip4yMDIy5bm05rGf6KW/55yB5aSn5a2m55Sf6IGM5Lia55Sf5rav6KeE5YiS5aSn6LWb55qE6YCa55+lXzIwMjItMDYtMDEucGRm";
System.out.println("java8 : " + decryptBASE64ByOrigin(str));
System.out.println("sun : " + decryptBASE64(str));
System.out.println("hutool : " + decipheringCodeByBase64(str));
}
}
service层:
controller:
前端接口: