Upload an image file using JSP and store the code in the stream format where the field type is blob.

Source: Internet
Author: User

The program that writes Image Code to the MSSQL database, so I am writing about how to write image files to the Database Using JSP. You should first create such a table in the database. The code below is common to any database, as long as it supports blob-type
You only need to change the database connection parameters.
SQL> Create Table image (ID int, content varchar (200), image BLOB );
If you use the SQL Server database, you can change the Blob field to the image type, which is added in SQL Server.
The content of the testimage.html file is as follows:
<HTML>
<Head>
<Title> image file </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>
<Form method = post action = "testimage. jsp">
<Input type = "text" name = "content"> <br>
<Input type = "file" name = "image"> <br>
<Input type = "Submit"> </form>
<Body>
</Body>
</Html>

We define an action testimage. jsp in form action. Its content is as follows:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<% @ Page import = "Java. util. *" %>
<% @ Page import = "Java. Text. *" %>
<% @ Page import = "Java. Io. *" %>
<HTML>
<Body>
<% Class. forname ("org. gjt. Mm. MySQL. Driver"). newinstance ();
String url = "JDBC: mysql: // localhost/MySQL? User = root & Password = & useunicode = true & characterencoding = 8859_1 ";
// MySQL indicates your database name, user indicates your database connection user, and password indicates your database connection password. You can change the password by yourself.
Connection conn = drivermanager. getconnection (URL );
String content = request. getparameter ("content ");
String filename = request. getparameter ("image ");
Fileinputstream STR = new fileinputstream (filename );
String SQL = "insert into test (ID, content, image) values (1 ,?,?) ";
Preparedstatement pstmt = dbconn. Conn. preparestatement (SQL );
Pstmt. setstring (1, content );
Pstmt. setbinarystream (2, STR, str. Available ());
Pstmt.exe cute ();
Out. println ("success, you have insert an image successfully ");
%>

The following is an example of testing the image output. The content of testimageout. jsp is as follows:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<% @ Page import = "Java. util. *" %>
<% @ Page import = "Java. Text. *" %>
<% @ Page import = "Java. Io. *" %>
<HTML>
<Body>
<% Class. forname ("org. gjt. Mm. MySQL. Driver"). newinstance ();
String url = "JDBC: mysql: // localhost/MySQL? User = root & Password = & useunicode = true & characterencoding = 8859_1 ";
// MySQL indicates your database name, user indicates your database connection user, and password indicates your database connection password. You can change the password by yourself.
Connection conn = drivermanager. getconnection (URL );
String SQL = "select image from test where id = 1 ";
Statement stmt = NULL;
Resultset rs = NULL;
Try {
Stmt = conn. createstatement ();
Rs1_stmt.exe cutequery (SQL );
} Catch (sqlexception e ){}
Try {
While (Rs. Next ()){
Res. setcontenttype ("image/JPEG ");
Servletoutputstream sout = response. getoutputstream ();
Inputstream in = Rs. getbinarystream (1 );
Byte B [] = new byte [0x7a120];
For (INT I = in. Read (B); I! =-1 ;)
{
Sout. Write (B );
In. Read (B );
}
Sout. Flush ();
Sout. Close ();
}
}
Catch (exception e) {system. Out. println (E );}
%>
</Body>
</Html>
When you run this program, you will see that the beautiful image you just wrote will be displayed in front of you. How about using JSP.
Writing images to the database in this way will cause the database to soar in a short period of time, which will affect the performance. Another way is to store images on the server,
It is a good method to store only the image path in the database. I suggest you use the following method.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.