• java BASE64流 输出图片。


    亲测3个请求都可用,没有测试性能问题。仅供参考
    BASE64Decoder  Eclipsse 类可能引用不了
    解决方案链接:http://blog.csdn.net/JBxiaozi/article/details/7351768


    3 import java.awt.image.BufferedImage; 4 import java.io.FileInputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.io.OutputStream; 8 import javax.imageio.ImageIO; 9 import javax.servlet.http.HttpServletResponse; 10 import org.springframework.stereotype.Controller; 11 import org.springframework.web.bind.annotation.RequestMapping; 12 import sun.misc.BASE64Decoder; 13 import sun.misc.BASE64Encoder; 14 15 @Controller 16 @RequestMapping("/imgs/") 17 public class test { 18 19 @RequestMapping("test") 20 public void test(HttpServletResponse response) throws IOException { 21 //写给浏览器 22 response.setContentType("image/jpeg"); 23 //浏览器不要缓存 24 response.setDateHeader("expries", -1); 25 response.setHeader("Cache-Control", "no-cache"); 26 response.setHeader("Pragma", "no-cache"); 27 BufferedImage buffImg = ImageIO.read(new FileInputStream("g://timg.jpg")); 28 ImageIO.write(buffImg, "jpg", response.getOutputStream()); 29 } 30 @RequestMapping("test1") 31 public void test1(HttpServletResponse response) throws IOException { 32 try { 33 response.setContentType("image/jpeg"); 34 OutputStream toClient = response.getOutputStream(); 35 String xmlImg = GetImageStr("g://timg.jpg"); 36 GenerateImage(xmlImg, toClient); 37 } catch (Exception ex) { 38 System.out.println(ex.toString()); 39 } 40 } 41 42 @RequestMapping("test2") 43 public void test2(HttpServletResponse response) throws IOException { 44 try { 45 response.setContentType("text/html"); 46 String xmlImg = GetImageStr("g://timg.jpg"); 47 System.out.println(xmlImg); 48 response.getWriter().write("<html><body><img src='data:image/jpeg;base64,"+xmlImg+"'/></body></html>"); 49 } catch (Exception ex) { 50 System.out.println(ex.toString()); 51 } 52 } 53 54 public static boolean GenerateImage(String imgStr, OutputStream out) { 55 if (imgStr == null) // 图像数据为空 56 return false; 57 BASE64Decoder decoder = new BASE64Decoder(); 58 try { 59 // Base64解码 60 byte[] b = decoder.decodeBuffer(imgStr); 61 for (int i = 0; i < b.length; ++i) { 62 if (b[i] < 0) { 63 b[i] += 256; 64 } 65 } 66 67 out.write(b); 68 out.flush(); 69 out.close(); 70 return true; 71 } catch (Exception e) { 72 return false; 73 } 74 } 75 76 public static String GetImageStr(String imgFilePath) { 77 // 将图片文件转化为字节数组字符串,并对其进行Base64编码处理 78 byte[] data = null; 79 80 // 读取图片字节数组 81 try { 82 InputStream in = new FileInputStream(imgFilePath); 83 data = new byte[in.available()]; 84 in.read(data); 85 in.close(); 86 } catch (IOException e) { 87 e.printStackTrace(); 88 } 89 90 // 对字节数组Base64编码 91 BASE64Encoder encoder = new BASE64Encoder(); 92 return encoder.encode(data);//返回字符串 93 } 94 }
  • 相关阅读:
    sitecore系统教程之体验编辑器
    Sitecore安装(手动方式)
    Sitecore详细安装(包含sitecore安装过程截图)
    logstash快速入门实战指南-Logstash简介
    Elasticsearch从入门到精通之Elasticsearch基本概念
    arcgis api 3.x for js 解决 textSymbol 文本换行显示(附源码下载)
    openlayers4 入门开发系列之前端动态渲染克里金插值 kriging 篇(附源码下载)
    arcgis api 4.x for js 结合 react 入门开发系列react全家桶实现加载天地图(附源码下载)
    arcgis api 4.x for js 结合 react 入门开发系列"esri-loader"篇(附源码下载)
    arcgis api 4.x for js 结合 react 入门开发系列初探篇(附源码下载)
  • 原文地址:https://www.cnblogs.com/1-Admin/p/8029949.html
Copyright © 2020-2023  润新知