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 ){
}
}
%>