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 如何从数据库中读取二进制文件(图片)并显示

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

香港服务器首选树叶云,2H2G首月10元开通。
树叶云(shuyeidc.com)提供简单好用,价格厚道的香港/美国云服务器和独立服务器。IDC+ISP+ICP资质。ARIN和APNIC会员。成熟技术团队15年行业经验。

文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/254018.html<

(0)
运维的头像运维
上一篇2025-04-30 04:25
下一篇 2025-04-30 04:26

相关推荐

  • 个人主题怎么制作?

    制作个人主题是一个将个人风格、兴趣或专业领域转化为视觉化或结构化内容的过程,无论是用于个人博客、作品集、社交媒体账号还是品牌形象,核心都是围绕“个人特色”展开,以下从定位、内容规划、视觉设计、技术实现四个维度,详细拆解制作个人主题的完整流程,明确主题定位:找到个人特色的核心主题定位是所有工作的起点,需要先回答……

    2025-11-20
    0
  • 社群营销管理关键是什么?

    社群营销的核心在于通过建立有温度、有价值、有归属感的社群,实现用户留存、转化和品牌传播,其管理需贯穿“目标定位-内容运营-用户互动-数据驱动-风险控制”全流程,以下从五个维度展开详细说明:明确社群定位与目标社群管理的首要任务是精准定位,需明确社群的核心价值(如行业交流、产品使用指导、兴趣分享等)、目标用户画像……

    2025-11-20
    0
  • 香港公司网站备案需要什么材料?

    香港公司进行网站备案是一个涉及多部门协调、流程相对严谨的过程,尤其需兼顾中国内地与香港两地的监管要求,由于香港公司注册地与中国内地不同,其网站若主要服务内地用户或使用内地服务器,需根据服务器位置、网站内容性质等,选择对应的备案路径(如工信部ICP备案或公安备案),以下从备案主体资格、流程步骤、材料准备、注意事项……

    2025-11-20
    0
  • 如何企业上云推广

    企业上云已成为数字化转型的核心战略,但推广过程中需结合行业特性、企业痛点与市场需求,构建系统性、多维度的推广体系,以下从市场定位、策略设计、执行落地及效果优化四个维度,详细拆解企业上云推广的实践路径,精准定位:明确目标企业与核心价值企业上云并非“一刀切”的方案,需先锁定目标客户群体,提炼差异化价值主张,客户分层……

    2025-11-20
    0
  • PS设计搜索框的实用技巧有哪些?

    在PS中设计一个美观且功能性的搜索框需要结合创意构思、视觉设计和用户体验考量,以下从设计思路、制作步骤、细节优化及交互预览等方面详细说明,帮助打造符合需求的搜索框,设计前的规划明确使用场景:根据网站或APP的整体风格确定搜索框的调性,例如极简风适合细线条和纯色,科技感适合渐变和发光效果,电商类则可能需要突出搜索……

    2025-11-20
    0

发表回复

您的邮箱地址不会被公开。必填项已用 * 标注