javabase64图片转文件流输出

package com.wisesoft.cform.web.controller;

import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

@Controller
@RequestMapping("/imgs/")
public class MyImg {

    @RequestMapping("test")
    public void test(HttpServletResponse response) throws IOException {
        //写给浏览器
        response.setContentType("image/jpeg");
        //浏览器不要缓存
        response.setDateHeader("expries", -1);
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        BufferedImage buffImg = ImageIO.read(new FileInputStream("g://timg.jpg"));
        ImageIO.write(buffImg, "jpg", response.getOutputStream());
    }
    
    @RequestMapping("test1")
    public void test1(HttpServletResponse response) throws IOException {
        try {
            response.setContentType("image/jpeg");
            OutputStream toClient = response.getOutputStream();
            String xmlImg = GetImageStr("C:\\Users\\zack\\Pictures\\186892ce0d5e478eb3896eb1cb4a1208.jpg");
            xmlImg = xmlImg.replace("data:image/gif;base64,", "");
            xmlImg = xmlImg.replace("data:image/jpg;base64,", "");
            System.out.println(xmlImg);
            GenerateImage(xmlImg, toClient);
        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
    }
    
    @RequestMapping("test2")
    public void test2(HttpServletResponse response) throws IOException {
        try {
            response.setContentType("text/html");
            String xmlImg = GetImageStr("g://timg.jpg");
            System.out.println(xmlImg);
            response.getWriter().write("");
        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
    }

    public static boolean GenerateImage(String imgStr, OutputStream out) {
        if (imgStr == null) // 图像数据为空
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static String GetImageStr(String imgFilePath) {
        // 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
        byte[] data = null;

        // 读取图片字节数组
        try {
            InputStream in = new FileInputStream(imgFilePath);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);//返回字符串
    }
}

你可能感兴趣的:(javabase64图片转文件流输出)