java base64 图片

package com.xx.xx.xx;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * @Author: fy 
 * @Date: 2019/4/25 13:42
 * @Description:  base64 和图片的转化
 */
public class MyBase64PictureUtils {


    /**
     * 将图片转成base64  这种base64没有头信息,如 data:image/png;base64,
     * @param picturePath  图片的绝对路径
     * @return  base64
     */
    public  static String picture2Base64(String picturePath){
        InputStream in = null;
        byte[] data = null;
        try {
            in = new FileInputStream(picturePath);
            data = new byte[in.available()];
            in.read(data);
            BASE64Encoder encoder = new BASE64Encoder();
            return encoder.encode(data);
        } catch (Exception e) {
            e.printStackTrace();
            throw  new RuntimeException(e);
        }finally {
            IOUtils.closeQuietly(in);
        }
    }


    /**
     * 将base64转成图片,如果 base64以头信息开头如 data:image/png;base64,*****,需要将 data:image/png;base64,去掉才能转成图片
     * @param base64
     */
    public static  void createPictureByBase64(String base64,String saveFilePath) throws  Exception{

        if(base64.startsWith("data:")){
            base64 = base64.substring(base64.indexOf(",")+1);
        }
        BASE64Decoder decoder =new BASE64Decoder();
        byte[] bytes = decoder.decodeBuffer(base64);
        FileUtils.writeByteArrayToFile(new File(saveFilePath),bytes);
    }


}

 

 

io包我用了 apache common-io里面工具。

java生成base64或者 生成图片是不需要data:image/png;base64 这样的头信息的。但是有时候前端传过来的图片base64包含这样的信息。所以如果要保存为图片,需要去掉这样的头信息。

   毕竟浏览器直接使用base64资源,肯定是需要知道数据的类型的。

 

base64保存为图片的时候如果要求不高可是直接后缀为  

C:\\***\\**\\**\\**.png ,否则就要去读取head信息里面的类型如 data:image/png;base64    ,或 data:image/jpeg;base64
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的Base64图片处理是通过将图片转换为Base64编码的字符串来实现的。使用JavaBase64类,可以将图片文件转换为Base64编码的字符串,也可以将Base64编码的字符串转换为图片文件。 下面是一个使用Java处理图片实现Base64编码转换的示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Base64; public class Base64Image { public static void main(String[] args) { String imagePath = "path/to/image.jpg"; // 将图片文件转换为Base64编码的字符串 String base64Image = encodeImage(imagePath); System.out.println("Base64编码的图片字符串: " + base64Image); // 将Base64编码的字符串转换为图片文件 String decodedImagePath = "path/to/decoded_image.jpg"; decodeImage(base64Image, decodedImagePath); System.out.println("已将Base64编码的字符串转换为图片文件: " + decodedImagePath); } private static String encodeImage(String imagePath) { File imageFile = new File(imagePath); try (FileInputStream fis = new FileInputStream(imageFile)) { byte[] imageBytes = new byte[(int) imageFile.length()]; fis.read(imageBytes); return Base64.getEncoder().encodeToString(imageBytes); } catch (IOException e) { e.printStackTrace(); } return null; } private static void decodeImage(String base64Image, String decodedImagePath) { byte[] imageBytes = Base64.getDecoder().decode(base64Image); try { FileUtils.writeByteArrayToFile(new File(decodedImagePath), imageBytes); } catch (IOException e) { e.printStackTrace(); } } } ``` 以上代码中的`encodeImage`方法将图片文件转换为Base64编码的字符串,而`decodeImage`方法则将Base64编码的字符串转换为图片文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值