Base64编码

Base 64 Encoding有什么用?举个简单的例子,你使用SMTP协议 (Simple Mail Transfer Protocol 简单邮件传输协议)来发送邮件。因为这个协议是基于文本的协议,所以如果邮件中包含一幅图片,我们知道图片的存储格式是二进制数据(binary data),而非文本格式,我们必须将二进制的数据编码成文本格式,这时候Base 64 Encoding就派上用场了。


    /**
* 下载图片
*/
@RequestMapping("/imageShow")
public String imageShow(HttpServletResponse response, Integer id)
throws IOException {
//根据id,获取图片地址
String imgPath = reportService.getImgPath(id);
OutputStream os = response.getOutputStream();
File file = new File(imgPath);
String[] list = file.list();
String path = imgPath + list[0];
String imageStr = FileUtils.getImageStr(path);
return imageStr;

}
 

附工具类

package com.hl.bluetooth.util;

import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;
import java.util.Objects;

/**
 * @DateL 2021/11/16 15:09
 * @ClassName: FileUtils
 **/
@Component
public class FileUtils {


    public static File multipartFileToFile(MultipartFile multipartFile) {
        File file = new File(Objects.requireNonNull(multipartFile.getOriginalFilename()));
        try {
            InputStream ins = null;
            ins = multipartFile.getInputStream();
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }

    /**
     * 图片转化成base64字符串
     *
     */
    public static String getImageStr(String imgPath) {
        InputStream in = null;
        byte[] data = null;
        String encode = null;
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        try {
            // 读取图片字节数组
            in = new FileInputStream(imgPath);
            data = new byte[in.available()];
            in.read(data);
            encode = encoder.encode(data);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return encode;
    }

    /**
     * base64字符串转化成图片
     *
     * @param imgData     图片编码
     */
    public static String generateImage(String imgData, String fileName)  {
        if (imgData == null) {
            // 图像数据为空
            return "null";
        }
        ApplicationHome applicationHome = new ApplicationHome(FileUtils.class);
        File source = applicationHome.getSource();
        String dirPath = source.getParentFile().toString() + "/upload";
        BASE64Decoder decoder = new BASE64Decoder();

        File dir = new File(dirPath);
        if (!dir.exists()){
            dir.mkdirs();
        }
        File file = new File(dirPath+"/"+fileName);
        if (file.exists()){
            file.delete();
        }
        try {
            // Base64解码
            byte[] b = decoder.decodeBuffer(imgData);
            for (int i = 0; i < b.length; ++i) {
                // 调整异常数据
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            OutputStream out = new FileOutputStream(dirPath+"\\"+fileName);
            out.write(b);
            out.flush();
            out.close();
            return dirPath+"\\"+fileName;
        } catch (Exception e) {
            e.printStackTrace();
            return "null";
        }
    }




}

 

posted @ 2022-07-18 14:31  jiuchengi  阅读(50)  评论(0编辑  收藏  举报