BASE64码转图片

 @Override
    public String baseToPhoto(String base64Code) {
    //保存图片路径
        String url = "D:/file";
//        base64Code = "data:image/jpeg;base64,"+base64Code;
        //
        if (base64Code == null) {
            return null;
        }
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            //Base64解码
            byte[] b = decoder.decodeBuffer(base64Code);
            // 处理数据
            for (int i = 0; i < b.length; ++i) {
                //调整异常数据
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            //文件夹不存在则自动创建
            File tempFile = new File(localFilePath+"/"+url);
            if (!tempFile.getParentFile().exists()) {
                tempFile.getParentFile().mkdirs();
            }
            //生成jpg图片
            OutputStream out = new FileOutputStream(tempFile);
            out.write(b);
            out.flush();
            out.close();
            return url;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
可以使用Java自带的Base64类和IO类来实现将base64码转换为图片并下载。具体步骤如下: 1. 将base64码转换为字节数组 2. 将字节数组写入到文件 3. 下载文件 以下是示例代码: ```java import java.io.*; import java.util.Base64; public class Base64ToImage { public static void main(String[] args) { String base64String = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABa0lEQVR4nL2Tz0sDQRSGv7+7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7v+H5vX5vX5vX5vX5vX5vX5vX5vX5vX5vX5vX5vX5vX5vX5vX5vX5vX5vX5vX5vX5vX5vX5vX5vX5vX5vX5vX5vX5vX5vX4P8f8P8f8P8f8P8f8P8f8P8f8P8f8P8f8P8f8P8f8P8f8P8f8P8f8P8f8P8f8P+H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4H6/4P8f8P8f8P8f8P8f8P8f8P8f8P8f8P8f8P8f8P8f8P8f8P8f8P8f8P8f8P+D9vX9vX9vX9vX9vX9vX9vX9vX9vX9vX9vX9vX9vX9vX9vX9vX9vX9vX9vX9vX9vX9vX9vX9vX9vX9vX9vX9vX9vX9vX9vX9vX5P+D7AAAAAElFTkSuQmCC"; String fileName = "test.png"; byte[] imageBytes = Base64.getDecoder().decode(base64String); try (OutputStream outputStream = new FileOutputStream(fileName)) { outputStream.write(imageBytes); } catch (IOException e) { e.printStackTrace(); } // 下载文件 File file = new File(fileName); if (file.exists()) { // 设置响应头,控制浏览器下载该文件 response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); // 读取要下载的文件,保存到文件输入流 FileInputStream in = new FileInputStream(file); // 创建输出流 OutputStream out = response.getOutputStream(); // 创建缓冲区 byte buffer[] = new byte[1024]; int len = 0; // 循环将输入流的内容读取到缓冲区当 while ((len = in.read(buffer)) > 0) { // 输出缓冲区的内容到浏览器,实现文件下载 out.write(buffer, 0, len); } // 关闭文件输入流 in.close(); // 关闭输出流 out.close(); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阳&光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值