P显示数据库图片详解 (jsp获取数据库图片并且显示)

随着互联网的发展,图片已经成为网站设计中不可或缺的元素之一。在许多Web应用程序中,需要从数据库中检索图像并在网页上显示它们。 P是一种能够实现此需求的强大工具,我们可以通过其提供的内置函数来轻松地从数据库中检索和显示图片。

本篇文章将详细介绍如何使用P来从数据库中检索图像并在网页上显示它们。

之一步:创建图像表

在这个例子中,我们将创建一个图像表,其中包含两个字段:id和image。其中id字段是自增的主键,而image字段是BLOB类型,我们将在其中存储图片数据。

如下所示,我们使用MySQL数据库来创建此表:

“`

CREATE TABLE images (

id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,

image BLOB NOT NULL

);

“`

现在我们已经创建了一张新的图像表,接下来我们需要将一些图片数据存储到其中。

第二步:存储图片数据

我们需要在数据库中存储一些图片数据,以便在接下来的步骤中使用。这里我们将使用JDBC来连接数据库并将图片数据插入到表中。

如下所示,我们将存储三张图片:

“`

Connection conn = null;

PreparedStatement stmt = null;

try {

// 获取数据库连接

conn = DriverManager.getConnection(“jdbc:mysql://localhost/mydatabase”,”root”,””);

// 插入之一张图片

File imageFile1 = new File(“D:/image1.jpg”);

FileInputStream fis1 = new FileInputStream(imageFile1);

stmt = conn.prepareStatement(“INSERT INTO images(image) VALUES(?)”);

stmt.setBinaryStream(1, fis1, (int) imageFile1.length());

stmt.executeUpdate();

// 插入第二张图片

File imageFile2 = new File(“D:/image2.jpg”);

FileInputStream fis2 = new FileInputStream(imageFile2);

stmt.setBinaryStream(1, fis2, (int) imageFile2.length());

stmt.executeUpdate();

// 插入第三张图片

File imageFile3 = new File(“D:/image3.jpg”);

FileInputStream fis3 = new FileInputStream(imageFile3);

stmt.setBinaryStream(1, fis3, (int) imageFile3.length());

stmt.executeUpdate();

} catch (SQLException | FileNotFoundException ex) {

ex.printStackTrace();

} finally {

if (stmt != null) {

stmt.close();

}

if (conn != null) {

conn.close();

}

}

“`

在这个例子中,我们使用了FileInputStream类来读取图片文件,并使用PreparedStatement对象的setBinaryStream()方法将图片数据存储到数据库中。

现在我们已经将图片数据存储到了数据库中,接下来我们需要使用P页面来检索和显示它们。

第三步:从数据库中检索图片数据

P页面是Java Servlet技术的扩展,因此它可以很容易地与Java进行交互。在这里,我们将使用一个P页面来检索并显示数据库中存储的图片数据。

如下所示,我们将使用P脚本和Java代码来从数据库中检索图片数据:

“`

<%

Connection conn = null;

PreparedStatement stmt = null;

ResultSet rs = null;

try {

// 获取数据库连接

conn = DriverManager.getConnection(“jdbc:mysql://localhost/mydatabase”,”root”,””);

// 查询图像列表

stmt = conn.prepareStatement(“SELECT * FROM images”);

rs = stmt.executeQuery();

// 创建一个缓冲输出流

ByteArrayOutputStream outStream = new ByteArrayOutputStream();

// 迭代检索到的记录

while (rs.next()) {

// 读取图像数据

InputStream in = rs.getBinaryStream(“image”);

byte[] buffer = new byte[4096];

int n = 0;

while ((n = in.read(buffer)) != -1) {

outStream.write(buffer, 0, n);

}

// 将图像数据写入响应输出流

response.setContentType(“image/jpeg”);

response.setContentLength(outStream.size());

response.getOutputStream().write(outStream.toByteArray());

outStream.reset();

}

} catch (SQLException | IOException ex) {

ex.printStackTrace();

} finally {

if (rs != null) {

rs.close();

}

if (stmt != null) {

stmt.close();

}

if (conn != null) {

conn.close();

}

}

%>

“`

在这个例子中,我们使用Java代码从数据库中检索图像数据。我们首先使用PreparedStatement对象执行一个Select查询,然后我们使用ResultSet对象来遍历每一行数据。我们使用InputStream对象读取BLOB类型的数据,并将它们写入缓冲流中。我们将图像数据写入到响应输出流中,以便在P页面上显示。

现在我们已经使用P页面成功地从数据库中检索图像数据并在网页上成功地显示它们。

结论

本篇文章详细介绍了如何使用P来从数据库中检索图像并在网页上显示它们。我们首先创建了一个包含图片数据的图像表,然后使用JDBC将图片数据存储到其中。我们最后使用P页面通过Java代码从数据库中检索图像数据,并使用响应输出流将它们显示在网页上。

相关问题拓展阅读:

jsp 如何从数据库中读取二进制文件(图片)并显示

提供的jsp页面绝敏代码:

上传照片并皮枝

function addInfo(){

if(document.PhotosForm.explain.value==””){

alert(“请填写说明内容!”);

return false;

}

if(document.PhotosForm.schoolPhoto.value==””){

alert(“请选择照片文件!”);

return false;

}

if((document.PhotosForm.schoolPhoto.value.indexOf(“.jpg”))>0||(document.PhotosForm.schoolPhoto.value.indexOf(“.bmp”)>0)||(document.PhotosForm.schoolPhoto.value.indexOf(“.gif”)>0)||(document.PhotosForm.schoolPhoto.value.indexOf(“.JPG”))>0||(document.PhotosForm.schoolPhoto.value.indexOf(“.BMP”)>0)||(document.PhotosForm.schoolPhoto.value.indexOf(“.GIF”)>0)){

return true;

}else{

alert(“请选择照片格式(.bmp或.jpg或.gif文件)!”);

return false;

}

}

上传照片

  师大首页–>

校园老照片–>上传老照片”);

else out.print(“校园新貌–>上传新照片”);

%>

照 片:

“/>

注释内容:

上传

重填

主要的java类:

package com.toowell.schoolPhoto.action;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.Action;

import org.apache.struts.upload.FormFile;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import java.sql.ResultSet;

import java.util.Vector;

import java.io.*;

import com.toowell.common.DbBean;

import com.toowell.common.Util;

import com.toowell.common.Upload;

import com.toowell.common.UserUtil;

import com.toowell.common.page.PageBean;

import com.toowell.schoolPhoto.form.PhotosForm;

import com.toowell.schoolPhoto.model.PhotosBean;

import com.toowell.schoolPhoto.model.PhotosVo;

/**

* Created by IntelliJ IDEA.

* User: Administrator

* Date:

* Time: 9:11:05

* To change this template use Options | File Templates.

*/

public class PhotoShowAction extends Action {

public ActionForward execute(ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response) throws Exception{

String parameter= mapping.getParameter();

boolean isAdmin=UserUtil.isAdmin(request);

boolean isAudit=UserUtil.isAudit(request);

//查询老照片

if(“old”.equalsIgnoreCase(parameter)) {

String sql=”select * from schoolPhoto where state=0 and audit=1 order by id desc”;

if(isAdmin){

sql=null;

sql=”select * from schoolPhoto where state=0 order by id desc”;

}

System.out.println(sql);

PageBean pageBean =new PageBean();

pageBean.pageshow(request,sql,”com.toowell.schoolPhoto.model.PhotosList”,16);

request.setAttribute(“type”,”old”);

return mapping.findForward(“success”);

}

//查询新照片

if(“new”.equalsIgnoreCase(parameter)) {

String sql=”select * from schoolPhoto where state=1 and audit=1 order by id desc”;

if(isAdmin){

sql=null;

sql=”select * from schoolPhoto where state=1 order by id desc”;

}

System.out.println(sql);

PageBean pageBean =new PageBean();

pageBean.pageshow(request,sql,”com.toowell.schoolPhoto.model.PhotosList”,16);

request.setAttribute(“type”,”new”);

return mapping.findForward(“success”);

}

//

if(“add”.equalsIgnoreCase(parameter)) {

String servletDir = servlet.getServletContext().getRealPath(“/schoolPhoto/images”);

PhotosForm photos=(PhotosForm)form;

String explain=Util.changeCode(photos.getExplain());

String state=photos.getState();

int type=Integer.parseInt(state);

int id=0;

try{

id = PhotosBean.getNowID()+1;

} catch(Exception e){

return mapping.findForward(“imageError”);

}

//获取上传的图片文件

FormFile formFile = photos.getSchoolPhoto();

//上传位置加上文件名,不含扩展名。回为扩展名可以自动判断

String dir = servletDir + “/” +id;

Upload up = new Upload(formFile,dir);

up.upload();

String imageType =up.getFileType();

PhotosVo photosVo=new PhotosVo();

photosVo.setId(id);

photosVo.setImageType(imageType);

photosVo.setExplain(explain);

try{

PhotosBean.add(photosVo,state);

}catch(Exception e){

return mapping.findForward(“imageError”);

}

if(type==0){

request.setAttribute(“type”,”old”);

return mapping.findForward(“oldSuccess”);

} else{

request.setAttribute(“type”,”new”);

return mapping.findForward(“newSuccess”);

}

}

if(“delete”.equalsIgnoreCase(parameter)) {

String servletDir = servlet.getServletContext().getRealPath(“/schoolPhoto/images”);

String id=request.getParameter(“id”);

ResultSet rs=null;

int state=0;

//删除照片

String sqlImageType=”select imageType,state from schoolPhoto where id=”+id;

try{

rs= DbBean.getRs(sqlImageType);

rs.next();

String imageType=rs.getString(1);

state=rs.getInt(2);

String imageName = id+imageType;

File file = new File(servletDir,imageName);

file.delete();

DbBean.close();

}catch(Exception e){

return mapping.findForward(“imageError”);

}

//删除照片数据库记录

String sql=”delete from schoolPhoto where id=”+id;

try{

DbBean.execute(sql);

DbBean.close();

}catch(Exception e){

return mapping.findForward(“imageError”);

}

if(state==0){

request.setAttribute(“type”,”old”);

return mapping.findForward(“oldSuccess”);

} else{

request.setAttribute(“type”,”new”);

return mapping.findForward(“newSuccess”);

}

}

if(“modify”.equalsIgnoreCase(parameter)) {

ResultSet rs=null;

Vector list=new Vector();

String id=request.getParameter(“id”);

String sql=”select * from schoolPhoto where id=”+id;

String servletDir = servlet.getServletContext().getRealPath(“/schoolPhoto/images”);

String state=request.getParameter(“type”);

try{

rs=DbBean.getRs(sql);

while(rs.next()){

PhotosVo photosVo=new PhotosVo();

photosVo.setId(rs.getInt(1));

photosVo.setImageType(rs.getString(2));

photosVo.setExplain(rs.getString(3));

photosVo.setState(rs.getInt(6));

list.add(photosVo);

}

}catch(Exception e){

return mapping.findForward(“imageError”);

}

request.setAttribute(“listPhoto”,list);

if(state.equals(“old”)){

request.setAttribute(“type”,”old”);

} else{

request.setAttribute(“type”,”new”);

}

return mapping.findForward(“success”);

}

if(“modifyOne”.equalsIgnoreCase(parameter)) {

String id=request.getParameter(“id”);

PhotosForm photo=(PhotosForm)form;

String explain=Util.changeCode(photo.getExplain());

int state=Integer.parseInt(photo.getState());

System.out.println(“—-=-=-=-=-=–“+state);

String sql=”update schoolPhoto set explain='”+explain+”‘,audit=0 where id=”+id;

System.out.println(sql);

try{

DbBean.execute(sql);

DbBean.close();

}catch(Exception e){

return mapping.findForward(“imageError”);

}

if(state==0){

request.setAttribute(“type”,”old”);

return mapping.findForward(“oldSuccess”);

} else{

request.setAttribute(“type”,”new”);

return mapping.findForward(“newSuccess”);

}

}

if(“audit”.equalsIgnoreCase(parameter)) {

String id=request.getParameter(“id”);

String state=request.getParameter(“type”);

String sql=”update schoolPhoto set audit=1 where id=”+id;

// System.out.println(sql);

try{

DbBean.execute(sql);

DbBean.close();

}catch(Exception e){

return mapping.findForward(“imageError”);

}

if(state.equals(“old”)){

request.setAttribute(“type”,”old”);

return mapping.findForward(“oldSuccess”);

} else{

request.setAttribute(“type”,”new”);

return mapping.findForward(“newSuccess”);

}

}

return null;

}

}

以上代码仅供参考,恕不能提供更多的代码;

祝好运

关于jsp获取数据库图片并且显示的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。


数据运维技术 » P显示数据库图片详解 (jsp获取数据库图片并且显示)