com.sun.org.apache.xml.internal.security.utils Base64不存在,找不到符号!(Deprecated and restricted API)

配置服务器中com.sun.org.apache.xml.internal.security.utils Base64不存在

问题描述:
在配置部署项目时,build failure,程序中显示某个核心包不存在!
我是使用了Base64来给图片转码,出现以下问题:
在这里插入图片描述
问题:com.sun.org.apache.xml.internal.security.utils.Base64不存在
原因: 自己写的代码不建议用com.sun包,他属于“Deprecated and restricted API”,可能在发布未来版本中删除不存在。
解决方法: 这些API出现了许多替代品,可以自己添加替代品,工具类来使用,
这里我自己添加了一个BaseUtils工具类来进行转码↓↓↓↓↓↓↓↓

package cn.com.nistone.webapi.utils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

public class Base64Utils {
	
	/**
	 * 图片转化成base64字符串
	 * @param fileAbsolutePath 文件绝对路径
	 * @return 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
	 */
	public static String GetImageStr(String fileAbsolutePath) {
		InputStream in = null;
		byte[] data = null;
		// 读取图片字节数组
		try {
			in = new FileInputStream(fileAbsolutePath);
			data = new byte[in.available()];
			in.read(data);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 对字节数组Base64编码
		return Base64.encodeBase64String(data);// 返回Base64编码过的字节数组字符串
	}

	
	/**
	 * 对字节数组字符串进行Base64解码并生成图片
	 * @param imgStr base64字符串
	 * @param fileAbsolutePath 存放文件绝对路径
	 * @return 操作成功 : true , 操作失败  : false 
	 */
	public static boolean GenerateImage(String imgStr, String fileAbsolutePath) {
		if (imgStr == null || StringUtils.isEmpty(imgStr)) // 图像数据为空
			return false;
		try {
			// Base64解码
			byte[] b = Base64.decodeBase64(imgStr);
			for (int i = 0; i < b.length; ++i) {
				if (b[i] < 0) {// 调整异常数据
					b[i] += 256;
				}
			}
			// 生成jpeg图片
			// 新生成的图片
			OutputStream out = new FileOutputStream(fileAbsolutePath);
			out.write(b);
			out.flush();
			out.close();
			return true;
		} catch (Exception e) {
			 e.printStackTrace();
			 return false;
		}
	}
	
	
	private final static String encoding = "utf-8";
	private final static String key = "*~<>?|&#%$@9X0!.";
	private final static byte[] keyIV = { 18, 52, 86, 120, (byte) 144,
			(byte) 171, (byte) 205, (byte) 239, (byte) 239, 18, (byte) 205, 52,
			(byte) 171, 86, 120, (byte) 239 };
	
	/**
	 * 加密
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public static String encode(String data) throws Exception{
		IvParameterSpec zeroIv = new IvParameterSpec(keyIV);
		Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
		SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
		cipher.init(Cipher.ENCRYPT_MODE, keyspec, zeroIv);
		byte[] encrypted = cipher.doFinal(data.getBytes());
		return Base64.encodeBase64String(encrypted);
	}
	
	/**
	 * 解密
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public static String decode(String data) throws Exception {
		IvParameterSpec zeroIv = new IvParameterSpec(keyIV);
		byte[] encrypted1 = Base64.decodeBase64(data.getBytes());
		Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
		SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
		cipher.init(Cipher.DECRYPT_MODE, keyspec, zeroIv);
		byte[] original = cipher.doFinal(encrypted1);
		return new String(original, encoding);
	}
	
	
	/**
	 * 解密
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public static  byte[] decode1(String data) throws Exception {
		IvParameterSpec zeroIv = new IvParameterSpec(keyIV);
		byte[] encrypted1 = Base64.decodeBase64(data.getBytes());
		Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
		SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
		cipher.init(Cipher.DECRYPT_MODE, keyspec, zeroIv);
		byte[] original = cipher.doFinal(encrypted1);
		return  original;
	}

}

成功转码生成图片! 这里有小伙伴需要使用到Base64转成图片的可以直接复制,调用GenerateImage方法传参即可!!

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值