从数据库提取图片文件显示到前端页面的一种方法

前言:上一篇写了如何从前端页面上传图片文件到mysql数据库,这篇写的就是如何将图片文件从数据库取出来,显示到前端页面

一、图片是以blob的类型存到数据库的,所以在取的时候可以利用rs.getBytes()的方法来提取,这里的 rs 是jdbc从数据库查询出来结果集。下面是通过传来bookid,来将图片显示到一个servlet的方法

1>dao层方法

package com.bookstore.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.bookstore.model.Book;
import com.bookstore.util.DBUtil;

public class BookDaoImpl implements IBookDao {
	Connection conn = null;
	PreparedStatement ps = null;
	ResultSet rs = null;
	static DBUtil dbu = null;
	
	public BookDaoImpl(){
		dbu = DBUtil.getDBUtil();
	}
	/**
	 * 通过书籍id查询书籍图片
	 */
	public byte[] queryBookPicture(int bookId) {
		// TODO Auto-generated method stub
		String sql = "select bookPicture from book where bookId = ? ";
		byte[] bookPicture = null;
		try{
			conn = dbu.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, bookId);
			rs = ps.executeQuery();
			if(rs.next()){
				bookPicture = rs.getBytes(1);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			dbu.close(ps);
			dbu.close(conn);
		}
		
		return bookPicture;
	}
}

2>service层方法

package com.bookstore.service;

import com.bookstore.dao.BookDaoImpl;

public class FindBookPictureImpl implements IFindBookPicture {
	private BookDaoImpl bdi = new BookDaoImpl();
	public byte[] findBookPicture(int bookId) {
		// TODO Auto-generated method stub		
		//调用dao方法
		return bdi.queryBookPicture(bookId);
	}

}
3>servlcet
package com.bookstore.servlet;

import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.bookstore.service.FindBookPictureImpl;

/**
 * Servlet implementation class FindBookPictureServlet
 */
@WebServlet("/bookPicture")
public class FindBookPictureServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private FindBookPictureImpl fbpi = new FindBookPictureImpl(); //调用service
 
    public FindBookPictureServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String bookId = request.getParameter("bookId"); //前端传来的图片id
		byte[] bookPicture = fbpi.findBookPicture(Integer.parseInt(bookId)); //获得bookPicture
		response.setContentType("image/jpeg");  //设置图片格式
		OutputStream out = response.getOutputStream(); //打开输出流
		out.write(bookPicture);  //输出图片
		out.flush();	//输出
		out.close();  //关闭输出
	}

}


三、在jsp页面调用图片的方法,

, 将img标签的src设置为该servlet的访问名字并加上你想传到这个servlet即你想访问的图片id,你就 可以将你想访问的图片显示到该jsp,并且通过编写其的css来改变其外观(长宽,颜色等)

PS: 实际开发中并不将图片存到数据库,因为这是一种很浪费数据库资源的做法。

你可能感兴趣的:(从数据库提取图片文件显示到前端页面的一种方法)