[英]I want to retrieve image from the database and display it in a jsp page
First i have a method 'insertimage' for inserting the image in the database and then I have used a Servlet class and called the method 'insertimage' for inserting operation.I have a bean class property 'image' of Part type and I have used a setter method of the bean class and set the image which i got from the index page. 首先,我有一个用于在数据库中插入图像的方法'insertimage',然后我使用了一个Servlet类并称为方法'insertimage'进行插入操作。我有一个Part类的bean类属性'image',并且我已经使用了bean类的setter方法,并设置我从索引页获得的图像。 please help with the code for retrieving the image and displaying it in a jsp page
请帮助获取代码并将其显示在jsp页面中的代码
inserting the image the database 将图像插入数据库
public boolean insertimage(FoodItems food)
{
boolean result=false;
try
{
InputStream inputstream=null;
image=food.getImage();// i have a bean class property of Part type
if(image !=null)
{
long fileSize=image.getSize();
String fileContent=image.getContentType();
inputstream=image.getInputStream();
}
PreparedStatement pst=con.prepareStatement("insert into AvailableItems values(?)");
pst.setBlob(1,inputstream);
pst.executeUpdate();
result=true;
}
catch(Exception e)
{
System.out.println("error st Available insert"+e);
}
return result;
}
//servlert class // servservt类别
@MultipartConfig(maxFileSize=169999999)
@WebServlet("/InsertFoods")
public class InsertFoods extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
Part image=request.getPart("image");
DBOperations db=new DBOperations();
FoodItems food=new FoodItems();
food.setImage(image);
if(db.insertimage(food))
{
response.sendRedirect("AvailableItems.jsp");
}
else
{
pw.println("not inserted");
}
}
}
Suppose you have jsp page where you want to retreive image . 假设您有一个要在其中获取图像的jsp页面。 you can do something like this to retrieve any image from
database
. 您可以执行类似的操作以从
database
检索任何图像。
<% //dbconnection
try {
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","");
Statement statement = conn.createStatement() ;
resultSet=statement.executeQuery("select * from tablename") ;
%>
<% while(resultSet.next()){ %>
Image - <img src="ImageProcess?id=<%=resultSet.getString("id")%>" width="20%"/>
<%
}
}catch(Exception e){}
%>
In Above code this -> <img src="ImageProcess?id=<%=resultSet.getString("id")%>" />
line is important ,here you are passing parameter
to servlet to get particular image
在上面的代码中,此->
<img src="ImageProcess?id=<%=resultSet.getString("id")%>" />
行很重要,这里您将parameter
传递给servlet以获取特定的image
Now,in your servlet
ie ImageProcess
you have to retreive the id
in doGet
and pass in query ,lastly send back the reponse to jsp page . 现在,在您的
servlet
即ImageProcess
您必须检索doGet
的id
并传递查询,最后将响应发送回jsp页面。
Blob image = null;
byte[] imgData = null;
String id= request.getParameter("id");//here you are getting id
int i;
ResultSet rs =null;
try {
//loading drivers for mysql
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","");
String sql = "SELECT image FROM tablename where id=?"; //here pass that id in query to get particular image
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, id);
rs = ps.executeQuery();
while (rs.next()) {
image = rs.getBlob("image");//getting image from database
imgData = image.getBytes(1,(int)image.length()); //extra info about image
}
response.setContentType("image/gif");//setting response type
OutputStream o = response.getOutputStream();
o.write(imgData);//sending the image to jsp page
o.flush();
o.close();
}
catch(Exception e)
{
e.printStackTrace();
}
Also this is not complete code,make changes as per your requirements .also don't forget to add jar's file
这也不是完整的代码,请根据您的要求进行更改。也不要忘记添加
jar's file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.