Home > front end >  Java web access database blob images, test shows the X, inquire!
Java web access database blob images, test shows the X, inquire!

Time:09-25

Built an inventory data on the virtual machine images, dates, names, images stored in a blob form, I want to do a web page and refresh time pictures, date, name, name and date can be show, but the pictures show is X · · · · · · · a novice for bosses to see code!!

Servlet client code:
Package the servlet;
Import the Java. The awt. Image. BufferedImage;
Import the Java. IO. BufferedInputStream;
Import the Java. IO. BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Import the Java. IO. InputStream;
Import the Java. SQL. Blob;
Import the Java. SQL. Connection;
Import the Java. SQL. The ResultSet;
Import Java, SQL Statement;
The import javax.mail. Imageio. Imageio;
The import javax.mail. Servlet. ServletException;
The import javax.mail. Servlet. ServletOutputStream;
The import javax.mail. Servlet. The annotation. WebServlet;
The import javax.mail. Servlet. HTTP. HttpServlet;
The import javax.mail. Servlet. HTTP. It;
import javax.servlet.http.HttpServletResponse;
The import com. Sun. Image. The codec. Jpeg. JPEGCodec;
The import com. Sun. Image. The codec. Jpeg. JPEGImageEncoder;
An import entity. The Course;
The import util. Shujuku;
/* *
* the Servlet implementation class showImgServlet
*/
@ WebServlet ("/showImgServlet ")
Public class showImgServlet extends the HttpServlet {
private static final long serialVersionUID=1L;

Protected void doPost (it request, HttpServletResponse response) throws ServletException, IOException {
//TODO Auto - generated method stub


String SQL="select image from PDate";
The Connection conn=Shujuku. Conn ();
The Statement st=null;
The ResultSet rs=null;

Try {
St=conn. CreateStatement ();
Rs=st. executeQuery (SQL);

While (rs), next ()) {
Blob Blob=rs. GetBlob (" image ");//this is pick up a BLOB values in weblogic environment
InputStream in=blob. GetBinaryStream ();//generated input stream
BufferedImage bimage=null;//cache the image object
BufferedInputStream ins=new BufferedInputStream (in);//input stream generated cache
Bimage=ImageIO. Read (ins);//read the ImageIO object into the cache object images, ImageIO is an important object
FileOutputStream SOS=new FileOutputStream (new File (" D:/1. JPG "));
BufferedOutputStream bos=new BufferedOutputStream (SOS);//generated output caching
JPEGImageEncoder encoder=JPEGCodec. CreateJPEGEncoder (bos);//generated JPEG image object
Encoder. Encode (bimage);//according to the format of the image code stream
Bos. Close ();
sos.close();
}
} the catch (Exception e) {
e.printStackTrace();
} the finally {
Shujuku. Close (rs, st, conn);
}
}
}

JSP shows:
<% @ page language="Java" contentType="text/HTML. Charset=utf-8 "
PageEncoding="utf-8" % & gt;
<% @ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" % & gt;



<meta HTTP - equiv="content-type" Content="text/HTML. Charset=ISO - 8859-1 "& gt;
This is the second test images & lt;/title> <br/><style> <br/>TB, td {<br/>border: 1px solid black; <br/>The font - size: 22 px; <br/>} <br/></style> <br/></head> <br/><body> <br/><H1 & gt; Through the flow load in the form of pictures to the JSP page displays & lt;/h1 & gt; <br/><Img Alt="" SRC=" https://bbs.csdn.net/topics/servlet/showImgServlet "title=" "style=" width: 300 px; Height: 300 px "& gt; <br/></body> <br/></html> <br/><p class="article - content rp"> CodePudding user response: </p>Just save images are generally not save the file, database file path </div> <div class="th_page th_page_color"></div> <div class="umCopyright"> <p>Page link:<a href="/frontend/29642.html" target="_blank" style="color:#999">https//www.codepudding.com/frontend/29642.html</a></p> </div> <div class="detail-arr"> <div class="detail-arr-left">Prev:<a href='/frontend/29641.html'>After using the body - the parser still cannot handle a post request</a></div> <div class="detail-arr-right">Next:<a href='/frontend/29643.html'>Mescrolljs after each dropdown refresh data why always repeated load, not to the bottom</a></div> </div> </div> </div> </div> </div> <div class="container th_top"> <div class="row"> <div class="col-md-12"> <div class="hot-tags neitags"> <ul> <li><i class="iconfont icon-x-tags"></i> Tags:  </li> <a href='/e/tags/?tagname=HTML+%28CSS%29' target='_blank'>HTML (CSS)</a> </ul> </div> </div> </div> </div> <div class="container th_top"> <div class="row"> <div class="col-md-12"> <div class="xiangguan"> <ul class="msg msghead"> <li class="tbname">Related</li> </ul> <ul> </ul> </div> </div> </div> </div> <div class="container th_top"> <div class="row"> <div class="col-md-12"> <div class="flinks"> <ul> <li><i class="iconfont icon-x-tags"></i> Links:  </li> <li class="liflinks"><a target="_blank" href="/" title="CodePudding">CodePudding</a></li> </ul> </div> </div> </div> </div> <div class="footer"> <p><span style="font-size:16px;color:#666;font-weight: bold">About Us:</span>  <a href="https://www.codepudding.com/contact.html">Contact Us</a>      <a href="https://www.codepudding.com/service.html">Terms of Service</a>      <a href="https://www.codepudding.com/privacy.html"> Privacy Policy</a></p> <p class="foot_info">Copyright © 2010-2023,Powered By <a href="/" target="_blank">CodePudding</a> </p> </div> <script type="text/javascript" src="/skin/code/tianhu.js"></script> </body></html>