博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java以Blob形式存储,读取图片并在jsp页面显示图片流
阅读量:5950 次
发布时间:2019-06-19

本文共 4086 字,大约阅读时间需要 13 分钟。

hot3.png

      废话少说代码伺候:

封装好的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

你可能感兴趣的文章
有序的双链表
查看>>
程序员全国不同地区,微信(面试 招聘)群。
查看>>
【干货】界面控件DevExtreme视频教程大汇总!
查看>>
闭包 !if(){}.call()
查看>>
python MySQLdb安装和使用
查看>>
Java小细节
查看>>
poj - 1860 Currency Exchange
查看>>
chgrp命令
查看>>
Java集合框架GS Collections具体解释
查看>>
洛谷 P2486 BZOJ 2243 [SDOI2011]染色
查看>>
linux 笔记本的温度提示
查看>>
数值积分中的辛普森方法及其误差估计
查看>>
Web service (一) 原理和项目开发实战
查看>>
跑带宽度多少合适_跑步机选购跑带要多宽,你的身体早就告诉你了
查看>>
广平县北方计算机第一届PS设计大赛
查看>>
深入理解Java的接口和抽象类
查看>>
java与xml
查看>>
Javascript异步数据的同步处理方法
查看>>
iis6 zencart1.39 伪静态规则
查看>>
SQL Server代理(3/12):代理警报和操作员
查看>>