[英]Retriving image from mysql database and display it in html page
I am simply trying to retrieve a image from database and display it. 我只是想从数据库中检索图像并显示它。 I have written the following code:
我写了以下代码:
RetreiveImage.java
import myconnection.ConnetionDefault;
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class RetreiveImage extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
PrintWriter out = response.getWriter();
Connection con=null;
String id=request.getParameter("id");
try{
con=ConnetionDefault.getConnection();
ResultSet results = null;
PreparedStatement ps = null;
ps = con.prepareStatement("select picture from pic_229084519 where pic_id=?");
ps.setString(1,id);
results = ps.executeQuery();
String imgLen="";
if(results.next()){
imgLen = results.getString(1);
System.out.println(imgLen.length());
int len = imgLen.length();
byte [] rb = new byte[len];
InputStream readImg = results.getBinaryStream(1);
int index=readImg.read(rb, 0, len);
System.out.println("index"+index);
ConnetionDefault.close(results,ps,con);
response.reset();
response.setContentType("image/jpg");
response.getOutputStream().write(rb,0,len);
response.getOutputStream().flush();
RequestDispatcher rd=request.getRequestDispatcher("gg.html");
rd.forward(request,response);
}
} catch (Exception e){
e.printStackTrace();
}
}
}
retImagel.html
<html>
<head>
<title>Login</title>
</head>
<body bgcolor="Gold">
<center>
<form action="ret.pic">
<table>
<tr><th>Namme</th><td><input type="text" name="id" size="3"/></td><td></td>
<td><input type="submit" value="Get"></td><td><input type="reset" value="Cancel"/></td></tr>
</table>
</form>
</center>
</body>
</html>
gg.html
<html>
<head>
<title>Login</title>
</head>
<body bgcolor="Gold">
<img src="/ret.pic?id=${id}"/>
</center>
</body>
</html>
My problem is its displaying image in default response page. 我的问题是它在默认响应页面中显示图像。 but i want it in gg.html where i am going wrong.. pls help me.
但是我想要在gg.html中出错的地方..请帮助我。
Set the filter in your web.xml to call your servlet. 在web.xml中设置过滤器以调用servlet。
On your gg.html page insert tag < img "url to image that filter can intersept" > 在gg.html页面上,插入标签<img“过滤器可以插入的图片的网址”>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.