當前位置:首頁 » 操作系統 » jsp顯示資料庫圖片

jsp顯示資料庫圖片

發布時間: 2022-05-30 05:56:51

A. 資料庫中照片怎麼在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=root&password=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>
<TD><img src="show.jsp?photo_no=2"></TD>
</TR>
</TABLE>
</BODY>
</HTML>

B. jsp從資料庫中讀取圖片顯示不出來

/SkyShop/images/brand/2010/02/09/09/4599577f-7fca-4163-a752-24a3e331ea39.jpg
像一樓說的是正解,不行的原因可能是根的位置問題,自己對著檢查一下。
另外你這個路徑太長。。太不好測試了,先弄個簡單點的。
你右鍵點圖片,屬性的地址應該是
http://localhost:8080/SkyShop/images/brand/2010/02/09/09/4599577f-7fca-4163-a752-24a3e331ea39.jpg
這個才是你的伺服器上圖片的地址。E:的是計算機的真實路徑,和伺服器上的絕對路徑,相對路徑不是一回事。伺服器啟動後,只能訪問當前伺服器路徑之內的文件,而windows下打開html文件不受此限制,所以不能用計算機的路徑。
保存到tomcat中就是保存到項目中了,如果你希望保存到源代碼中,可以去改tomcat的server.xml配置文件,
<Context path="/" docBase="D:\workspace\SkyShop\ROOT"
debug="0" privileged="true">

</Context>
並刪除webapps下的內容。
或者以流的方式將寫到圖片寫到資料庫中。

最後建議你去看看關於相對路徑的文章,篇幅有限,說的還是不太清楚。

C. jsp中顯示資料庫中圖片和文字

可以提前把圖片和文字寫好放在頁面中,屬性設為隱藏,調用的時候用js控制,把屬性改為顯示就行

D. 圖片在資料庫中存的是圖片名(記為字元串),如何在jsp上以此字元串來顯示圖片

<img src="<%=basePath%>resource/ ok.gif"></img>
basePath是文件的路徑,資料庫一般都會存的是路徑,也就是你說的字元串

E. 怎麼把資料庫里的圖片顯示到jsp

從資料庫中查找圖片的二進制數據,把查找出來的數據set到會話request.getSession().setAttribute("img",圖片的二進制數據 );

F. 讀取保存在資料庫里的圖片JSP頁面顯示無法顯示圖片

我把你的代碼稍微改造了下,我這邊是可以顯示圖片的。代碼如下:


資料庫操作部分:

packagecom.database;

importjava.io.InputStream;
importjava.sql.*;

/**
*@作者王建明
*@創建日期13-10-7
*@創建時間下午12:32
*@版本號V1.0
*/
publicclassDataBaseUtil{
(){
Connectionconn=null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn=
DriverManager.getConnection("jdbc:mysql://localhost/quickstart","root","123456");
Statementstmt=conn.createStatement();
Stringsql="selectbook_imagefromtbl_bookwhereid=1";

ResultSetrs=stmt.executeQuery(sql);
if(rs.next()){
returnrs.getBinaryStream("book_image");
}

}catch(Exceptione){
System.out.println("出現異常:"+e.getMessage());
}finally{
try{
if(conn!=null)
conn.close();
}catch(SQLExceptione){
e.printStackTrace();
}
}
returnnull;
}
}


servlet部分:

packagecom.servlet;

importcom.database.DataBaseUtil;

importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;

/**
*@作者王建明
*@創建日期13-10-7
*@創建時間下午12:18
*@版本號V1.0
*/
{
protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
doGet(request,response);
}

protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
InputStreamin=DataBaseUtil.getImageStreamFromDataBase();
OutputStreamtoClient=response.getOutputStream();
response.reset();
response.setContentType("image/jpg");//或gif
intlen=10*1024*1024;
byte[]P_Buf=newbyte[len];
inti;
while((i=in.read(P_Buf))!=-1){
toClient.write(P_Buf,0,i);

}
in.close();
toClient.flush();
toClient.close();
}
}


web.xml中的servlet配置:

<servlet>
<servlet-name>ShowImage</servlet-name>
<servlet-class>com.servlet.ShowImage</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ShowImage</servlet-name>
<url-pattern>/showImage</url-pattern>
</servlet-mapping>


頁面中載入圖片方式:

<imgsrc="showImage"/>


希望對你有幫助O(∩_∩)O~

G. jsp顯示資料庫存儲目錄的圖片

首先,代碼是不會有的了。
其次你要把圖片存入資料庫,資料庫只存儲圖片的目錄。這兩句話本身就互斥好吧……姑且以後半句為例,講講思路。
你在一個文件夾中存儲了圖片,在資料庫里應該有個表。這個表起碼有ID,有文件名filename,有路徑這三個欄位。路徑path存儲的是相對路徑,並且是你項目有權訪問的路徑。
在java程序中讀取這個表的數據,將path和filename拼接成完整的相對路徑+文件名的形式。在eclipse中新建jsp的時候,會自動生成獲取basePath的代碼。basePath是當前頁面所在項目的根目錄,用basePath
+
path
+
filename就可以顯示出圖片了

H. 如何檢索和顯示資料庫中的圖像在 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=root&password=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>
<TD><img src="show.jsp?photo_no=2"></TD>
</TR>
</TABLE>
</BODY>
</HTML>

I. jsp如何根據sql2008資料庫中的圖片路徑顯示圖片

這個要看你資料庫中存儲的圖片路徑是哪種了?
1、如果是映射出的URL路徑,如(www.123.com/pic/1.jpg或/pic/1.jpg),那麼直接把路徑放到img標簽的src中就行了;
2、如果是真實的路徑,如(D:\pic\1.jpg),如果D:\pic這個在jsp伺服器中被映射成成了URL,如映射成"/pic/",那麼src中的值為"/pic/1.jpg",就可以顯示了
3、如果是真實路徑,且沒有映射成URL,那麼jsp是無法訪問的

J. Java jsp中根據從資料庫中查詢出來的路徑顯示圖片

jsp是靠載入圖片路徑來顯示圖片的,圖片可以保存在項目中,也可以保存到磁碟的某個路勁下。
圖片放webroot下,一般是webroot下建一個img或pic
文件夾專門存圖。
jsp使用的時候<img
src"<%=request.getContextPath()%>/pic/mypic.gif"/>
這樣就可以載入到圖片了。

熱點內容
步驟條源碼 發布:2024-05-05 15:35:55 瀏覽:844
安卓拍光遇視頻如何高清 發布:2024-05-05 15:23:20 瀏覽:932
linuxo文件 發布:2024-05-05 15:19:12 瀏覽:943
手機伺服器地址或者域名 發布:2024-05-05 15:19:09 瀏覽:372
我的世界伺服器版如何登錄 發布:2024-05-05 15:17:28 瀏覽:794
綦江dns伺服器地址 發布:2024-05-05 15:04:11 瀏覽:556
山東省日照市監控伺服器地址 發布:2024-05-05 15:03:59 瀏覽:342
java提升教程 發布:2024-05-05 15:00:51 瀏覽:144
驅動編譯龍芯 發布:2024-05-05 14:41:31 瀏覽:957
起什麼密碼 發布:2024-05-05 14:29:48 瀏覽:562