本文目錄一覽:
- 1、讀取保存在資料庫里的圖片JSP頁面顯示無法顯示圖片
- 2、如何從資料庫里把數據顯示在JSP頁面上
- 3、jsp圖片插入資料庫並讀出頁面
- 4、jsp中顯示資料庫中圖片和文字
- 5、資料庫中照片怎麼在jsp中顯示
讀取保存在資料庫里的圖片JSP頁面顯示無法顯示圖片
我把你的代碼稍微改造了下,我這邊是可以顯示圖片的。代碼如下:
資料庫操作部分:
package com.database;
import java.io.InputStream;
import java.sql.*;
/**
* @作者 王建明
* @創建日期 13-10-7
* @創建時間 下午12:32
* @版本號 V 1.0
*/
public class DataBaseUtil {
public static InputStream getImageStreamFromDataBase() {
Connection conn = null;
try {
Class.forName(“com.mysql.jdbc.Driver”);
conn =
DriverManager.getConnection(“jdbc:mysql://localhost/quickstart”, “root”, “123456”);
Statement stmt = conn.createStatement();
String sql = “select book_image from tbl_book where id=1 “;
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
return rs.getBinaryStream(“book_image”);
}
} catch (Exception e) {
System.out.println(“出現異常: ” + e.getMessage());
} finally {
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
}
servlet部分:
package com.servlet;
import com.database.DataBaseUtil;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* @作者 王建明
* @創建日期 13-10-7
* @創建時間 下午12:18
* @版本號 V 1.0
*/
public class ShowImage extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
InputStream in = DataBaseUtil.getImageStreamFromDataBase();
OutputStream toClient = response.getOutputStream();
response.reset();
response.setContentType(“image/jpg”);//或gif
int len = 10*1024*1024;
byte[] P_Buf = new byte[len];
int i;
while((i = in.read(P_Buf)) != -1){
toClient.write(P_Buf, 0, i);
}
in.close();
toClient.flush();
toClient.close();
}
}
web.xml中的servlet配置:
servlet
servlet-nameShowImage/servlet-name
servlet-classcom.servlet.ShowImage/servlet-class
/servlet
servlet-mapping
servlet-nameShowImage/servlet-name
url-pattern/showImage/url-pattern
/servlet-mapping
頁面中載入圖片方式:
img src=”showImage” /
希望對你有幫助O(∩_∩)O~
如何從資料庫里把數據顯示在JSP頁面上
把資料庫的數據保存在response、session、application,然後在頁面使用struts標籤、jsp標籤等顯示,或者用%。
對於你的這個問題,建議你學一下系統的javaee體系,應為把數據讀到頁面不是那麼簡單的,需要jdbc鏈接資料庫,需要一個tomcat伺服器,需要資料庫jar包、、、、、、
jsp圖片插入資料庫並讀出頁面
2008-11-02 15:321.序
資料庫應用程序,特別是基於WEB的資料庫應用程序,常會涉及到圖片信息的存儲和顯示。
通常我們使用的方法是將所要顯示的圖片存在特定的目錄下,在資料庫中保存相應的圖片的名稱,在JSP中建立相應的數據源,利用資料庫訪問技術處理圖片信息。但是,如果我們想動態的顯示圖片,上述方法就不能滿足需要了。我們必須把圖片存入資料庫,然後通過編程動態地顯示我們需要的圖片。實際操作中,可以利用JSP的編程模式來實現圖片的資料庫存儲和顯示。
2. 建立後台資料庫
if exists (select * from dbo.sysobjects
where id = object_id(N'[dbo].[p]’) and OBJECTPROPERTY(id, N’IsUserTable’) = 1)
drop table [dbo].[p]
GO
CREATE TABLE [dbo].[p] (
[picid] [int] IDENTITY (1, 1) NOT NULL ,
[picname] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[pic] [image] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
3.向資料庫存儲二進位圖片
啟動Dreamweaver MX後,新建一個JSP文件。其代碼如下所示。
%@ page contentType=”text/html;charset=gb2312″%
%
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%”
titleMy JSP ‘InputImage.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”
—
/head
body
form action=”testimage.jsp” method=”POST”br
題目input name=”picname” type=”text”br
圖片input name=”pic” type=”file”br
input type=”Submit” name=”button1″ value=”提交”br
/form
/body
/html
將此文件保存為InputImage.jsp文件,其中testimage.jsp文件是用來將圖片數據存入資料庫的,具體代碼如下所示:
%@ page contentType=”text/html;charset=gb2312″%
%@ page import=”java.sql.*” %
%@ page import=”java.util.*”%
%@ page import=”java.text.*”%
%@ page import=”java.io.*”%
jsp:useBean id=”conn” scope=”page” class=”dbconn.DBResult”/
%
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%”
titleMy JSP ‘testimage.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”
—
/head
body
%
request.setCharacterEncoding(“gb2312”);
//建立Statement對象
String picname=request.getParameter(“picname”);
String pic=request.getParameter(“pic”);
//獲得所要顯示圖片的標題、存儲路徑、內容,並進行中文編碼
FileInputStream str=new FileInputStream(pic);
String sql=”insert into p(picname,pic) values(?,?)”;
PreparedStatement pstmt=conn.getPreparedStatement(sql);
pstmt.setString(1,picname);
pstmt.setBinaryStream(2,str,str.available());
pstmt.execute();
//將數據存入資料庫
out.println(“Success,You Have Insert an Image Successfully”);
%
/body
/html
4. 網頁中動態顯示圖片
接下來我們要編程從資料庫中取出圖片,其代碼如下所示。
%@ page contentType=”text/html;charset=gb2312″%
%@ page import=”java.sql.*” %
%@ page import=”java.util.*”%
%@ page import=”java.text.*”%
%@ page import=”java.io.*”%
jsp:useBean id=”conn” scope=”page” class=”dbconn.DBResult”/
%
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%”
titleMy JSP ‘testimageout.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”
—
/head
body
%
int id= Integer.parseInt(request.getParameter(“picid”));
String sql = “select pic from p WHERE picid=”+id;
ResultSet rs=conn.getResult(sql);
while(rs.next())
{
ServletOutputStream sout = response.getOutputStream();
//圖片輸出的輸出流
InputStream in = rs.getBinaryStream(1);
byte b[] = new byte[0x7a120];
for(int i = in.read(b); i != -1;)
{
sout.write(b);
//將緩衝區的輸入輸出到頁面
in.read(b);
}
sout.flush();
//輸入完畢,清除緩衝
sout.close();
}
%
/body
/html
將此文件保存為testimageout.jsp文件。下一步要做的工作就是使用HTML標記:
%@ page contentType=”text/html;charset=gb2312″%
%@ page import=”java.sql.*” %
%@ page import=”java.util.*”%
%@ page import=”java.text.*”%
%@ page import=”java.io.*”%
jsp:useBean id=”conn” scope=”page” class=”dbconn.DBResult”/
%
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%”
titleMy JSP ‘lookpic.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”
—
/head
body
%
String sql = “select * from p”;
ResultSet rs=conn.getResult(sql);
while(rs.next())
{
%
ccid_file values=”testimageout” % /” width=”100″ height=”100″
br
%
}
rs.close();
%
/body
/html
版權歸原版所有!!!
jsp中顯示資料庫中圖片和文字
可以提前把圖片和文字寫好放在頁面中,屬性設為隱藏,調用的時候用js控制,把屬性改為顯示就行
資料庫中照片怎麼在jsp中顯示
用JSP從資料庫中讀取圖片並顯示在網頁上:
環境mysql+tomcat:
1先在mysql下建立如下的table. 並insert圖像
mysql.sql文件如下:
CREATE TABLE photo (
photo_no int(6) unsigned NOT NULL auto_increment,
image blob,
PRIMARY KEY (`photo_no`)
)
2把show.jsp放在tomcat的任意目錄下. show.jsp作用:從資料庫中讀出blob,併產生image/jpg.
show.jsp文件如下:
%@ page contentType=”text/html; charset=gbk” %
%@ page import=”java.io.*”%
%@ page import=”java.sql.*, javax.sql.*” %
%@ page import=”java.util.*”%
%@ page import=”java.math.*”%
%
String photo_no = request.getParameter(“photo_no”);
//mysql連接
Class.forName(“com.mysql.jdbc.Driver”).newInstance();
String URL=”jdbc:mysql://localhost:3306/job?user=rootpassword=111111″;
Connection con = DriverManager.getConnection(URL);
//oracle連接
//String URL=”jdbc:oracle:thin@localhost:1521:orcl2″;
//user=”system”;
//password=”manager”;
//Connection con = DriverManager.getConnection(URL,user,password);
try{
// 準備語句執行對象
Statement stmt = con.createStatement();
String sql = ” SELECT * FROM PHOTO WHERE photo_no = “+ photo_no;
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
Blob b = rs.getBlob(“photo_image”);
long size = b.length();
//out.print(size);
byte[] bs = b.getBytes(1, (int)size);
response.setContentType(“image/jpeg”);
OutputStream outs = response.getOutputStream();
outs.write(bs);
outs.flush();
rs.close();
}
else {
rs.close();
response.sendRedirect(“./images/error.gif”);
}
}
finally{
con.close();
}
%
3把如下文件放在show.jsp的同一目錄下.
index.html文件如下:
HTML
HEAD
TITLE 圖像測試 /TITLE
/HEAD
BODY
TABLE
TR
TD圖像測試/TD
/TR
TR
TDimg src=”show.jsp?photo_no=2″/TD
/TR
/TABLE
/BODY
/HTML
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/283057.html