Download the data display image directly from the Blob field without passing through a temporary file!

Source: Internet
Author: User

See the following function:
Public void createimage (servletrequest req, servletresponse res, chart) throws ioexception
{

Res. setcontenttype ("image/JPEG ");
Chartutilities. writechartasjpeg (res. getoutputstream (),
100, chart. getchart (), chart. getwidth (), chart. getheight (), null );
}

It's better to use the first minute.
The following is a program. You only need to configure a servlet in Web. xml.
Import java. SQL .*;
Import java. Io .*;
Import java. util .*;
Import javax. servlet .*;
Import javax. servlet. http .*;

Public class download extends httpservlet {
Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Dopost (request, response );
}
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
System. Out. println ("====== download begin ==== ");
Try {
Class. forname ("oracle. JDBC. Driver. oracledriver ");
Connection conn = drivermanager. getconnection ("JDBC: oracle: thin: @ 10.216.0.2: 1521: orcl", "test", "test ");
Statement stmt = conn. createstatement ();
String id = request. getparameter ("ID ");
String SQL = "select ID, name, content from test where id = '" + ID + "'";
Resultset rs1_stmt.exe cutequery (SQL );
If (Rs. Next ()){
Blob blob = Rs. getblob ("content ");
Byte [] AB = blob. getbytes (1, (INT) blob. Length ());
Response. setcontenttype ("image/JPEG ");
Servletoutputstream op = response. getoutputstream ();
Op. Write (AB );
Op. Flush ();
Op. Close ();
}
} Catch (exception ex ){
Ex. printstacktrace ();
}
System. Out. println ("====== download end ==== ");
}
}
The following is a call, where tempchart is the chart object you generated.
<%
New COM. Chart. createchartimage (). createimage (request, response, tempchart );

%>

-------- Two JSP ---------------

-------------------- Master -----------------------
& Lt; table width = "100%" & gt;
<Tr>
<TD align = "center"> & pic_num = <% = pic_num %> ">
</TD>
</Tr>
</Table>
---------------------------------------- Item_pic.jsp -------------------------------------- <% @ page import = "Java. SQL. *" %>
<% @ Page import = "Java. Io. *" %>
<%
Connection conn = NULL;
Statement stmt = NULL;
Resultset set = NULL;
Try {
String item_id = request. getparameter ("item_id ");
String pic_num = request. getparameter ("pic_num ");
String SQL = "select pic from item_pic where item_id =" + item_id + "and pic_num =" + pic_num;
Conn = getconnection ();
Stmt = conn. createstatement ();
Set = stmt.exe cutequery (SQL );
If (set. Next ()){
Inputstream in = set. getbinarystream ("pic ");
Response. Reset ();
Response. setcontenttype ("image/JPEG ");
Byte [] B = new byte [1024];
Int Len;
While (LEN = in. Read (B ))! =-1 ){
Response. getoutputstream (). Write (B );
}
In. Close ();
}
} Catch (exception e ){
} Finally {
Try {
If (set! = NULL)
Set. Close ();
} Catch (Java. SQL. sqlexception SE ){
}
Try {
If (stmt! = NULL)
Stmt. Close ();
} Catch (Java. SQL. sqlexception SE ){
}
Try {
If (Conn! = NULL)
Conn. Close ();
} Catch (Java. SQL. sqlexception SE ){
}
}
%>

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.