java字节数组转base64串_实例详解Java实现图片与base64字符串之间的转换

废话不多说了,直接给大家贴java实现图片与base84字符串之间的转换代码了,具体代码如下所示:

package cn.com;

import Java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

public class Base64Test

{

public static void main(String[] args)

{

String strImg = GetImageStr();

System.out.println(strImg);

GenerateImage(strImg);

}

//图片转化成base64字符串

public static String GetImageStr()

{//将图片文件转化为字节数组字符串,并对其进行Base64编码处理

String imgFile = "d://test.jpg";//待处理的图片

InputStream in = null;

byte[] data = null;

//读取图片字节数组

try

{

in = new FileInputStream(imgFile);

data = new byte[in.available()];

in.read(data);

in.close();

}

catch (IOException e)

{

e.printStackTrace();

}

//对字节数组Base64编码

BASE64Encoder encoder = new BASE64Encoder();

return encoder.encode(data);//返回Base64编码过的字节数组字符串

}

//base64字符串转化成图片

public static boolean GenerateImage(String imgStr)

{ //对字节数组字符串进行Base64解码并生成图片

if (imgStr == null) //图像数据为空

return false;

BASE64Decoder decoder = new BASE64Decoder();

try

{

//Base64解码

byte[] b = decoder.decodeBuffer(imgStr);

for(int i=0;i

{

if(b[i]<0)

{//调整异常数据

b[i]+=256;

}

}

//生成jpeg图片

String imgFilePath = "d://222.jpg";//新生成的图片

OutputStream out = new FileOutputStream(imgFilePath);

out.write(b);

out.flush();

out.close();

return true;

}

catch (Exception e)

{

return false;

}

}

}

以上所述是小编给大家介绍的Java实现图片与base64字符串之间的转换,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你解答这个问题。 在 Java 中,我们可以使用 javax.crypto 库中的 Cipher 类进行 AES 加密和解密操作。具体步骤如下: 1. 创建 Cipher 对象 ```java Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); ``` 2. 创建密钥 ```java SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES"); ``` 其中,keyBytes 是一个 byte 数组,表示密钥的内容。 3. 初始化 Cipher 对象 ```java cipher.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(ivBytes)); ``` 其中,ivBytes 是一个 byte 数组,表示初始化向量。 4. 加密或解密数据 ```java byte[] result = cipher.doFinal(dataBytes); ``` 其中,dataBytes 是一个 byte 数组,表示要加密或解密的数据。 5. 将加密或解密后的结果转换字符串字节数组 如果需要将加密或解密后的结果转换字符串,可以使用 Base64 编码。具体代码如下: ```java String resultStr = Base64.getEncoder().encodeToString(result); ``` 如果需要将加密或解密后的结果转换字节数组,可以直接使用得到的 result 数组。 完整的示例代码如下: ```java import java.util.Base64; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class AesUtils { public static byte[] encrypt(byte[] keyBytes, byte[] ivBytes, byte[] dataBytes) throws Exception { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(ivBytes)); return cipher.doFinal(dataBytes); } public static byte[] decrypt(byte[] keyBytes, byte[] ivBytes, byte[] dataBytes) throws Exception { Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES"); cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(ivBytes)); return cipher.doFinal(dataBytes); } public static void main(String[] args) throws Exception { String dataStr = "hello, world!"; byte[] keyBytes = "0123456789abcdef".getBytes(); byte[] ivBytes = "0123456789abcdef".getBytes(); byte[] dataBytes = dataStr.getBytes(); // 加密 byte[] encryptedBytes = encrypt(keyBytes, ivBytes, dataBytes); String encryptedStr = Base64.getEncoder().encodeToString(encryptedBytes); System.out.println(encryptedStr); // 解密 byte[] decryptedBytes = decrypt(keyBytes, ivBytes, encryptedBytes); String decryptedStr = new String(decryptedBytes); System.out.println(decryptedStr); } } ``` 希望这个答案能够帮助到你!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值