新聞中心
隨著互聯(lián)網(wǎng)的發(fā)展,圖片已經(jīng)成為網(wǎng)站設(shè)計(jì)中不可或缺的元素之一。在許多Web應(yīng)用程序中,需要從數(shù)據(jù)庫中檢索圖像并在網(wǎng)頁上顯示它們。 P是一種能夠?qū)崿F(xiàn)此需求的強(qiáng)大工具,我們可以通過其提供的內(nèi)置函數(shù)來輕松地從數(shù)據(jù)庫中檢索和顯示圖片。

本篇文章將詳細(xì)介紹如何使用P來從數(shù)據(jù)庫中檢索圖像并在網(wǎng)頁上顯示它們。
之一步:創(chuàng)建圖像表
在這個(gè)例子中,我們將創(chuàng)建一個(gè)圖像表,其中包含兩個(gè)字段:id和image。其中id字段是自增的主鍵,而image字段是BLOB類型,我們將在其中存儲(chǔ)圖片數(shù)據(jù)。
如下所示,我們使用MySQL數(shù)據(jù)庫來創(chuàng)建此表:
“`
CREATE TABLE images (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
image BLOB NOT NULL
);
“`
現(xiàn)在我們已經(jīng)創(chuàng)建了一張新的圖像表,接下來我們需要將一些圖片數(shù)據(jù)存儲(chǔ)到其中。
第二步:存儲(chǔ)圖片數(shù)據(jù)
我們需要在數(shù)據(jù)庫中存儲(chǔ)一些圖片數(shù)據(jù),以便在接下來的步驟中使用。這里我們將使用JDBC來連接數(shù)據(jù)庫并將圖片數(shù)據(jù)插入到表中。
如下所示,我們將存儲(chǔ)三張圖片:
“`
Connection conn = null;
PreparedStatement stmt = null;
try {
// 獲取數(shù)據(jù)庫連接
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();
}
}
“`
在這個(gè)例子中,我們使用了FileInputStream類來讀取圖片文件,并使用PreparedStatement對(duì)象的setBinaryStream()方法將圖片數(shù)據(jù)存儲(chǔ)到數(shù)據(jù)庫中。
現(xiàn)在我們已經(jīng)將圖片數(shù)據(jù)存儲(chǔ)到了數(shù)據(jù)庫中,接下來我們需要使用P頁面來檢索和顯示它們。
第三步:從數(shù)據(jù)庫中檢索圖片數(shù)據(jù)
P頁面是Java Servlet技術(shù)的擴(kuò)展,因此它可以很容易地與Java進(jìn)行交互。在這里,我們將使用一個(gè)P頁面來檢索并顯示數(shù)據(jù)庫中存儲(chǔ)的圖片數(shù)據(jù)。
如下所示,我們將使用P腳本和Java代碼來從數(shù)據(jù)庫中檢索圖片數(shù)據(jù):
“`
<%
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
// 獲取數(shù)據(jù)庫連接
conn = DriverManager.getConnection(“jdbc:mysql://localhost/mydatabase”,”root”,””);
// 查詢圖像列表
stmt = conn.prepareStatement(“SELECT * FROM images”);
rs = stmt.executeQuery();
// 創(chuàng)建一個(gè)緩沖輸出流
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
// 迭代檢索到的記錄
while (rs.next()) {
// 讀取圖像數(shù)據(jù)
InputStream in = rs.getBinaryStream(“image”);
byte[] buffer = new byte[4096];
int n = 0;
while ((n = in.read(buffer)) != -1) {
outStream.write(buffer, 0, n);
}
// 將圖像數(shù)據(jù)寫入響應(yīng)輸出流
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();
}
}
%>
“`
在這個(gè)例子中,我們使用Java代碼從數(shù)據(jù)庫中檢索圖像數(shù)據(jù)。我們首先使用PreparedStatement對(duì)象執(zhí)行一個(gè)Select查詢,然后我們使用ResultSet對(duì)象來遍歷每一行數(shù)據(jù)。我們使用InputStream對(duì)象讀取BLOB類型的數(shù)據(jù),并將它們寫入緩沖流中。我們將圖像數(shù)據(jù)寫入到響應(yīng)輸出流中,以便在P頁面上顯示。
現(xiàn)在我們已經(jīng)使用P頁面成功地從數(shù)據(jù)庫中檢索圖像數(shù)據(jù)并在網(wǎng)頁上成功地顯示它們。
結(jié)論
本篇文章詳細(xì)介紹了如何使用P來從數(shù)據(jù)庫中檢索圖像并在網(wǎng)頁上顯示它們。我們首先創(chuàng)建了一個(gè)包含圖片數(shù)據(jù)的圖像表,然后使用JDBC將圖片數(shù)據(jù)存儲(chǔ)到其中。我們最后使用P頁面通過Java代碼從數(shù)據(jù)庫中檢索圖像數(shù)據(jù),并使用響應(yīng)輸出流將它們顯示在網(wǎng)頁上。
成都網(wǎng)站建設(shè)公司-創(chuàng)新互聯(lián),建站經(jīng)驗(yàn)豐富以策略為先導(dǎo)10多年以來專注數(shù)字化網(wǎng)站建設(shè),提供企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計(jì),響應(yīng)式網(wǎng)站制作,設(shè)計(jì)師量身打造品牌風(fēng)格,熱線:028-86922220jsp 如何從數(shù)據(jù)庫中讀取二進(jìn)制文件(圖片)并顯示
提供的jsp頁面絕敏代碼:
上傳照片并皮枝
function addInfo(){
if(document.PhotosForm.explain.value==””){
alert(“請(qǐng)?zhí)顚懻f明內(nèi)容!”);
return false;
}
if(document.PhotosForm.schoolPhoto.value==””){
alert(“請(qǐng)選擇照片文件!”);
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(“請(qǐng)選擇照片格式(.bmp或.jpg或.gif文件)!”);
return false;
}
}
上傳照片
師大首頁–>
校園老照片–>上傳老照片”);
else out.print(“校園新貌–>上傳新照片”);
%>
照 片:
“/>
注釋內(nèi)容:
上傳
重填
主要的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();
//上傳位置加上文件名,不含擴(kuò)展名?;貫閿U(kuò)展名可以自動(dòng)判斷
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”);
}
//刪除照片數(shù)據(jù)庫記錄
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(“l(fā)istPhoto”,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;
}
}
以上代碼僅供參考,恕不能提供更多的代碼;
祝好運(yùn)
關(guān)于jsp獲取數(shù)據(jù)庫圖片并且顯示的介紹到此就結(jié)束了,不知道你從中找到你需要的信息了嗎 ?如果你還想了解更多這方面的信息,記得收藏關(guān)注本站。
成都創(chuàng)新互聯(lián)科技公司主營:網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、小程序制作、成都軟件開發(fā)、網(wǎng)頁設(shè)計(jì)、微信開發(fā)、成都小程序開發(fā)、網(wǎng)站制作、網(wǎng)站開發(fā)等業(yè)務(wù),是專業(yè)的成都做小程序公司、成都網(wǎng)站建設(shè)公司、成都做網(wǎng)站的公司。創(chuàng)新互聯(lián)公司集小程序制作創(chuàng)意,網(wǎng)站制作策劃,畫冊(cè)、網(wǎng)頁、VI設(shè)計(jì),網(wǎng)站、軟件、微信、小程序開發(fā)于一體。
分享名稱:P顯示數(shù)據(jù)庫圖片詳解(jsp獲取數(shù)據(jù)庫圖片并且顯示)
文章地址:http://fisionsoft.com.cn/article/ccopisi.html


咨詢
建站咨詢
