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

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

 

package org.blog.util;

 

import java.io.File;

 

import java.io.FileInputStream;

 

import java.io.FileNotFoundException;

 

import java.io.FileOutputStream;

 

import java.io.IOException;

 

 public class ImageUtil {

 

    private static File file = null;

 

    /**

 

     * 读取图像的二进制流

 

     * 

 

     * @param infile

 

     * @return

 

     */

 

    public static FileInputStream getByteImage(String infile) {

 

        FileInputStream inputImage = null;

 

        file = new File(infile);

 

        try {

 

            inputImage = new FileInputStream(file);

 

        } catch (FileNotFoundException e) {

 

            e.printStackTrace();

 

        }

 

        return inputImage;

 

    }

 

    /**

 

     * 输出图片

 

     * @param inputStream

 

     * @param path

 

     */

 

    public static void readBlob(FileInputStream inputStream, String path) {

 

        try {

 

            FileOutputStream fileOutputStream = new FileOutputStream(path);

 

            byte[] buf = new byte[1024];

 

            int len = 0;

 

            while ((len = inputStream.read(buf)) != -1) {

 

                fileOutputStream.write(buf, 0, len);// 写

 

             }

 

            inputStream.close();

 

            fileOutputStream.close();

 

        } catch (FileNotFoundException e) {

 

            e.printStackTrace();

 

        } catch (IOException e) {

 

            e.printStackTrace();

 

        }

 

    }

 

}

 

 

 

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

servlet源码:

 

package servlet;

 

import java.io.ByteArrayInputStream;

 

import java.io.FileInputStream;

 

import java.io.IOException;

 

import java.io.InputStream;

 

import java.io.OutputStream;

 

import java.io.PrintWriter;

 

import java.sql.Blob;

 

import javax.servlet.ServletException;

 

import javax.servlet.ServletOutputStream;

 

import javax.servlet.http.HttpServlet;

 

import javax.servlet.http.HttpServletRequest;

 

import javax.servlet.http.HttpServletResponse;

 

import org.blog.util.ImageUtil;

 

import org.hibernate.Hibernate;

 

 public class Image extends HttpServlet {

 

    private static final long serialVersionUID = 1L;

 

    @Override

 

    protected void doGet(HttpServletRequest req, HttpServletResponse resp)

 

            throws ServletException, IOException {

 

        this.doPost(req, resp);

 

    }

 

    @Override

 

    protected void doPost(HttpServletRequest req, HttpServletResponse resp)

 

            throws ServletException, IOException {

 

        try {

 

            FileInputStream in = ImageUtil.getByteImage("D:\\me.jpg");

 

            Blob blob = Hibernate.createBlob(in);

 

            InputStream inputStream = blob.getBinaryStream();// IO流

 

            int length = (int) blob.length();

 

            byte[] b = new byte[length];

 

            inputStream.read(b, 0, length);

 

            PrintWriter out = resp.getWriter();

 

            InputStream is = new ByteArrayInputStream(b);

 

            int a = is.read();

 

            while (a != -1) {

 

                out.print((char) a);

 

                a = is.read();

 

            }

 

            out.flush();

 

            out.close();

 

            /*OutputStream outputStream = resp.getOutputStream();// 从response中获取getOutputStream

 

            outputStream.write(b);// 写

 

            inputStream.close();

 

            outputStream.close();*/

 

        } catch (Exception e) {

 

            System.out.println("error");

 

        }

 

    }

 

}

 

jsp源码:

 

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

 

<%

 

String path = request.getContextPath();

 

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

 

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

 

<html>

 

  <head>

 

    <base href="<%=basePath%>">

 

    

 

    <title>My JSP 'image.jsp' starting page</title>

 

    

 

    <meta http-equiv="pragma" content="no-cache">

 

    <meta http-equiv="cache-control" content="no-cache">

 

    <meta http-equiv="expires" content="0">    

 

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

 

    <meta http-equiv="description" content="This is my page">

 

    <!--

 

    <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">

 

    -->

 

  </head>

 

  

 

  <body>

 

    <div style="border: solid red ;" mce_style="border: solid red ;"> <img src="image.do" mce_src="image.do"></div>

 

  </body>

 

</html>

 

 

 

jsp从数据库中读取二进制图片显示在页面上,要求图片在规定的长宽中显示,大的缩写,小的还是原图大小代码

<img id="lg_png" onload="if(this.width>200)this.width=200;if(this.height>80)this.height=80" src='GetPhoServlet.do?objectFlag=2&photo_no=<%= ctVecs.elementAt(2)%>' />

 

 

 

你可能感兴趣的:(java)