| |

VerySource

 Forgot password?
 Register
Search
View: 871|Reply: 3

How do I display the picture file taken from the database on the page?

[Copy link]

2

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-20 13:00:01
| Show all posts |Read mode
The binary image file is stored in the database. I need to read it out and display it on the freemarker page. How can I get it and how to display it?

Searched a lot online, but not!

I use freemarker + webwork + ibatis
Reply

Use magic Report

0

Threads

16

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 United States

Post time: 2020-6-30 02:30:01
| Show all posts
Put the picture in the binary form in the database, and save the generated file of the picture to a directory when it is taken out, then the link from this directory is displayed, not directly taken from the database and displayed directly on the page,
mstrBackName = result.getString("randomname");//The name drop out
mstrfileName = result.getString("markname");//Picture name
pintid = result.getInt("id");
InputStream inStream=result.getBinaryStream("MarkBody");//The binary stream extracted from the database
String aa=getServletContext().getRealPath("/")+"upload/showFile/";
FileOutputStream fs=new FileOutputStream(aa+mstrBackName);//Define the path and image name to be operated
if(mstrBackName!=null&&mstrBackName!="")
{
byte[] buffer =new byte[1444];
while ((byteread=inStream.read(buffer))!=-1)
{
bytesum += byteread;
fs.write(buffer,0,byteread);//Write image file
}
String str;
str = "<img src='showFile/"+mstrBackName+"'"; //Show pictures
str = str + "width=39 height=42 border=0>";
out.println(str);
out.println(pintid);
}
%>
Reply

Use magic Report

0

Threads

11

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-6-30 07:45:02
| Show all posts
I think it should first output the text byte stream of the webpage to the browser. The browser will analyze the webpage file. If there is a picture tag, it will apply to the server for the picture file. You can filter the picture file in the web.xml configuration of the server. If the request is a picture file, call the picture processing servlet to let the servlet read the binary byte stream in the database of a certain picture in the database and send it back to the browser. Pay attention to set the sending type of the response before sending. The text is text/html, the picture seems to be /jpg, etc.
Reply

Use magic Report

0

Threads

11

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 Egypt

Post time: 2020-7-13 22:00:01
| Show all posts
Leaned over for you
Take the image data from the database and publish it as a picture through servlet

The code can only be used as a reference, it is best not to use it in practice, because it seriously violates the MVC pattern.


    servlet

        
The following is the program code:


/**

*

*/

package servlet;



import java.io.*;



import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.ServletConfig;

import javax.servlet.ServletOutputStream;



import javax.imageio.ImageIO;

import java.sql.*;

import java.awt.Image;

import java.awt.image.BufferedImage;

/**

* @author jaqcy

*

*/

public class ImageServlet extends HttpServlet

{



@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException

{

// TODO automatically generates method stubs

resp.setContentType("image/png"[img]/images/wink.gif[/img];

BufferedImage image=null;

try

{

Class.forName("com.mysql.jdbc.Driver"[img]/images/wink.gif[/img];

Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/sampledb","root","mysql"[img]/images/wink.gif[/img];

Statement stmt=conn.createStatement();

ResultSet rs=stmt.executeQuery("select image from customers"[img]/images/wink.gif[/img];

if(rs.next())

{

Blob blob=rs.getBlob(1);

InputStream is=blob.getBinaryStream();

image=ImageIO.read(is);

}

rs.close();

stmt.close();

conn.close();

ServletOutputStream sos=resp.getOutputStream();

ImageIO.write(image,"PNG",sos);

sos.close();

}

catch (Exception e)

{

// TODO automatically generates catch blocks

e.printStackTrace();

}

}



@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException

{

// TODO automatically generates method stubs

doGet(req, resp);

}



@Override

public void init(ServletConfig config) throws ServletException

{

// TODO automatically generates method stubs

super.init(config);

}



}



        




The following is the program code:


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

<servlet>

<servlet-name>image</servlet-name>

<servlet-class>servlet.ImageServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>image</servlet-name>

<url-pattern>/image.png</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>image.png</welcome-file>

</welcome-file-list>

</web-app>
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list