java BASE64流 输出图片。
发布日期:2022-03-29 14:04:56
浏览次数:43
分类:博客文章
本文共 3192 字,大约阅读时间需要 10 分钟。
亲测3个请求都可用,没有测试性能问题。仅供参考
BASE64Decoder Eclipsse 类可能引用不了 解决方案链接:
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 @Controller16 @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("");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 }
转载地址:https://www.cnblogs.com/1-Admin/p/8029949.html 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过,博主的博客真漂亮。。
[***.116.15.85]2024年03月06日 21时09分09秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
java学习手册下载_Java学习手册
2019-04-21
axios delete有请求体吗_关于axios请求——delete方法
2019-04-21
mysql函数大全 pdf,MySQL函数大全
2019-04-21
php 常用文件系统函数,php 文件系统函数整理介绍
2019-04-21
oracle所需的环境,转:面对一个全新的oracle环境,首先应该了解什么?
2019-04-21
linux 小数四则运行,shell四则运算(整数及浮点数)的方法介绍
2019-04-21
linux系统分区后进入紧急模式,Linux系统的救援模式应用详解
2019-04-21
linux创建硬盘分区lvm,LVM创建及分区调整、更换LVM硬盘
2019-04-21
FreeBSD可以安装Linux软件吗,在Linux服务器上面通过网络安装FreeBSD
2019-04-21
南昌工程学院c语言答案,南昌工程学院C语言程序设计基础课件第3讲运算符和表达式...
2019-04-21