java顯示資料庫圖片
Ⅰ java怎樣把圖片保存到資料庫然後讀出來在html中顯示
一般是將圖片本體存放到OSS,圖片的路徑存放到資料庫,然後jsp中獲取的是他的路徑
然後用<img src="OSS的鏈接/"+你資料庫的圖片路徑> 你可以用本地圖片試試 將圖片本體存放在你的E盤/image下面 然後你存入資料庫的是你的圖片名 然後將圖片上傳到本地 然後使用<img src='E盤/image'+資料庫裡面的圖片名> 就可以顯示了 你可以試試
Ⅱ 讀取保存在資料庫里的圖片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~
Ⅲ java如何讀取資料庫中的路徑在標簽框中顯示圖片
1、從DB得到圖片路徑
2、載入圖片到 ImageIcon
3、顯示在JLabel中,setIcon(ImageIcon)
Ⅳ 在JAVA中如何將圖片從資料庫讀取到頁面上
你只要開啟一個輸出流將從資料庫取出來的圖片(這個時候肯定是二進制數據是吧),寫出來就行了.
然後在調用圖片的地方調用相應的方法就可以輸出了.
Ⅳ JAVA 讀取 資料庫中的圖片顯示到頁面
你說你只想存ID到資料庫中,那你就專門用個文件夾存放圖片,ID值就是圖片文件名字!從資料庫得到ID後,就在<IMG SRE="這里寫上絕對路徑"+ID+".jpg"/>
Ⅵ Java怎麼做到把圖片轉換成流存入資料庫,然後怎麼再把圖片顯示出來。
首先創建一個空 Blob/Clob 欄位,再從這個空 Blob/Clob欄位獲取游標,例如下面的代碼:
PreparedStatement ps = conn.prepareStatement( " insert into PICTURE(image,resume) values(?,?) " );
// 通過oralce.sql.BLOB/CLOB.empty_lob()構造空Blob/Clob對象
ps.setBlob( 1 ,oracle.sql.BLOB.empty_lob());
ps.setClob( 2 ,oracle.sql.CLOB.empty_lob());
ps.excuteUpdate();
ps.close();
// 再次對讀出Blob/Clob句柄
ps = conn.prepareStatement( " select image,resume from PICTURE where id=? for update " );
ps.setInt( 1 , 100 );
ResultSet rs = ps.executeQuery();
rs.next();
oracle.sql.BLOB imgBlob = (oracle.sql.BLOB)rs.getBlob( 1 );
oracle.sql.CLOB resClob = (oracle.sql.CLOB)rs.getClob( 2 );
// 將二進制數據寫入Blob
FileInputStream inStream = new FileInputStream( " c://image.jpg " );
OutputStream outStream = imgBlob.getBinaryOutputStream();
byte [] buf = new byte [ 10240 ];
int len;
while (len = inStream.read(buf) > 0 ) {
outStream.write(buf, 0 ,len);
}
inStream.close();
outStream.cloese();
// 將字元串寫入Clob
resClob.putString( 1 , " this is a clob " );
// 再將Blob/Clob欄位更新到資料庫
ps = conn.prepareStatement( " update PICTURE set image=? and resume=? where id=? " );
ps.setBlob( 1 ,imgBlob);
ps.setClob( 2 ,resClob);
ps.setInt( 3 , 100 );
ps.executeUpdate();
ps.close();
Ⅶ java後台怎麼把資料庫二進制圖片傳到前台顯示
兩種思路,一種是你把圖片下下來。告訴路徑給前端。
另一種是你直接把圖片轉換成byte數組,返回給前端。前端是可以有辦法的。(我用過這種用來爬蟲爬
驗證碼
都是這么乾的)
望採納