2019独角兽企业重金招聘Python工程师标准>>>

废话少说代码伺候:

封装好的ImageUtil类:目的读取本地的图片文件并存入数据库,然后读出数据库中以Blob形式存储的图片保存到指定目录。

1 package org.blog.util;
2 import java.io.File;
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7   public class ImageUtil {
8 private static File file = null ;
9 /* *
10 * 读取图像的二进制流
11 *
12 * @param infile
13 * @return
14 */
15 public static FileInputStream getByteImage(String infile) {
16 FileInputStream inputImage = null ;
17 file = new File(infile);
18 try {
19 inputImage = new FileInputStream(file);
20 } catch (FileNotFoundException e) {
21 e.printStackTrace();
22 }
23 return inputImage;
24 }
25 /* *
26 * 输出图片
27 * @param inputStream
28 * @param path
29 */
30 public static void readBlob(FileInputStream inputStream, String path) {
31 try {
32 FileOutputStream fileOutputStream = new FileOutputStream(path);
33 byte [] buf = new byte [ 1024 ];
34 int len = 0 ;
35 while ((len = inputStream.read(buf)) != - 1 ) {
36 fileOutputStream.write(buf, 0 , len); // 写
37   }
38 inputStream.close();
39 fileOutputStream.close();
40 } catch (FileNotFoundException e) {
41 e.printStackTrace();
42 } catch (IOException e) {
43 e.printStackTrace();
44 }
45 }
46 }

从数据库中读出二进制流显示到jsp页面:

servlet源码:

1 package servlet;
2 import java.io.ByteArrayInputStream;
3 import java.io.FileInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.OutputStream;
7 import java.io.PrintWriter;
8 import java.sql.Blob;
9 import javax.servlet.ServletException;
10 import javax.servlet.ServletOutputStream;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14 import org.blog.util.ImageUtil;
15 import org.hibernate.Hibernate;
16   public class Image extends HttpServlet {
17 private static final long serialVersionUID = 1L ;
18 @Override
19 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
20 throws ServletException, IOException {
21 this .doPost(req, resp);
22 }
23 @Override
24 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
25 throws ServletException, IOException {
26 try {
27 FileInputStream in = ImageUtil.getByteImage( " D:\\me.jpg " );
28 Blob blob = Hibernate.createBlob( in );
29 InputStream inputStream = blob.getBinaryStream(); // IO流
30 int length = ( int ) blob.length();
31 byte [] b = new byte [length];
32 inputStream.read(b, 0 , length);
33 PrintWriter out = resp.getWriter();
34 InputStream is = new ByteArrayInputStream(b);
35 int a = is .read();
36 while (a != - 1 ) {
37 out .print(( char ) a);
38 a = is .read();
39 }
40 out .flush();
41 out .close();
42 /* OutputStream outputStream = resp.getOutputStream();// 从response中获取getOutputStream
43 outputStream.write(b);// 写
44 inputStream.close();
45 outputStream.close(); */
46 } catch (Exception e) {
47 System. out .println( " error " );
48 }
49 }
50 }

jsp源码:

1 <% @ page language = " java " import = " java.util.* " pageEncoding = " UTF-8 " %>
2 <%
3 String path = request.getContextPath();
4 String basePath = request.getScheme() + " :// " + request.getServerName() + " : " + request.getServerPort() + path + " / " ;
5 %>
6 <! DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.01 Transitional//EN " >
7 < html >
8 < head >
9 < base href = " <%=basePath%> " >
10
11 < title > My JSP ' image.jsp ' starting page </ title >
12
13 < meta http - equiv = " pragma " content = " no-cache " >
14 < meta http - equiv = " cache-control " content = " no-cache " >
15 < meta http - equiv = " expires " content = " 0 " >
16 < meta http - equiv = " keywords " content = " keyword1,keyword2,keyword3 " >
17 < meta http - equiv = " description " content = " This is my page " >
18 <!--
19 < link rel = " stylesheet " type = " text/css " href = " styles.css " mce_href = " styles.css " >
20 -->
21 </ head >
22
23 < body >
24 < div style = " border: solid red ; " mce_style = " border: solid red ; " > < img src = " image.do " mce_src = " image.do " ></ div >
25 </ body >
26 </ html >

转载于:https://my.oschina.net/u/197668/blog/361218

java以Blob形式存储,读取图片并在jsp页面显示图片流相关推荐

  1. mysql blob 读取 图片_mysql中以blob形式存储的图片文件 通过ajax方式传输 在js中设置成img控件的src...

    第一步,读取blob数据, 第二步,将blob数据转换成byte数组 第三步,将byte数据进行base64加密转换成字符串并回传 第四步,接收字符串 第五步,将img控件的src设置成"d ...

  2. 在jsp中通过I/O流方式读取图片并展示到页面

    之前在做一个项目时用到了图片上传并立即展示到页面浏览,而且图片存放在硬盘上的一个文件夹中而非在工程与数据库中,这就会出现一个问题,如果不是在开发程序环境中访问图片页面,则会出现图片不能展示情况,原因很 ...

  3. java exif 语言编码_Java读取图片EXIF信息的代码

    Java读取图片EXIF信息的代码 本文实例讲述了Java读取图片EXIF信息的方法.分享给大家供大家参考.具体分析如下: 首先介绍一下什么是EXIF,EXIF是Exchangeable Image ...

  4. 从oracle中读取图片,从oracle数据库中读取图片,在jsp?

    servlet:(jsp调用servlet显示图片,servlet如下) public void doGet(HttpServletRequest request, HttpServletRespon ...

  5. springmvc后台获取图片,到jsp页面显示

    1,jsp页面,img的url动态设定 <img id='bomimg' alt="" > $("#bomimg").attr("src& ...

  6. java里的文件显示繁体_关于在JSP页面上同时显示简体和繁体的问题(字符编码)...

    编写过JSP页面的朋友大概对于中文乱码问题已经是司空见惯了,网上对于这方面问题的解决办法也很多,在此我就不多赘述了. 但是最近我遇到了一个问题,就是我编写过的页面,有时候会显示繁体字:而这个是通常的G ...

  7. java 把jsp 保存成图片_将jsp页面转化为图片或pdf(一)(qq:1324981084)

    public class Urlimage extendsJPanel {/*** jsp转jpg*/ private static final long serialVersionUID = 1L; ...

  8. servlet和struts2一起使用,实现绝对路径下的图片输出到jsp页面

    如果我们在web.xml中配置的struts2的接收请求的路径为: <filter-mapping> <filter-name>struts2</filter-name& ...

  9. 将文件中的图片显示到jsp页面

    声明:自己在实践也遇到这个问题,解决后给大家分享一下.有误导或者不足之处,欢迎指出.小编也是小白. 不啰嗦,直奔主题 在tomcat目录下找到service.xml文件 -- tomcat安装目录\c ...

最新文章

  1. 浏览器渲染原理及可能出现的bug
  2. 为什么VS(visual studio)中会建议宏 #define 转换为constexpr?
  3. 宝鸡文理学院c语言试题,宝鸡文理学院试题电子电磁场与电磁波A
  4. Rapidmind计算库性能测试
  5. 熊猫分发_熊猫新手:第一部分
  6. JAVA打印变量类型
  7. 回溯算法高效解标准数独(MarkDown)
  8. Bounds和Frame简介
  9. happen-before原则
  10. 菜鸟知识-五大智能手机操作系统
  11. Eclipse 创建JavaWeb工程
  12. 家有妙招:教你一个在淘宝买东西打折的好方法
  13. 用css给我画一朵花
  14. 阿里巴巴B2B主要股东
  15. 基于java的SFTP工具类
  16. Excel2007版的常用功能(24):宏表函数:
  17. 区块恋 l 世上最浪漫的事是和你一起看BTC步步高升
  18. 【笔记】路由器 - 软硬件构成
  19. 前端面试高频精讲(二)Vue篇
  20. 小程序开发与公众号用户关联推送消息

热门文章

  1. python:小心类实例的属性动态绑定机制
  2. 查看安卓APK源码破解
  3. 开启AngularJS 1.X的学习之路(1)
  4. 推导坐标旋转公式(转)
  5. 回顾2012;展望2013
  6. python怎么模拟浏览器交互_干货分享:python爬虫模拟浏览器的两种方法实例分析(赶紧收藏)...
  7. python之_init_函数的简介
  8. C#将unix时间戳转换成.net的DateTime类型的代码
  9. supervisord的安装使用
  10. Ubuntu防火墙:ufw