不经过临时文件,直接从BLOB字段,下载数据显示图片!

看下面的这个函数:
public void createImage(ServletRequest req, ServletResponse res,Chart chart)throws IOException
{

res.setContentType("image/jpeg");    
ChartUtilities.writeChartAsJPEG(res.getOutputStream(),
100,chart.getChart(),chart.getWidth(),chart.getHeight(),null);
}

用第一钟,比较好
下面是程序,你只要在web.xml中配个servlet就可以了。
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 rs=stmt.executeQuery(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=====");
    }
}
以下是调用,其中tempChart是你生成的chart对象
<%
new com.chart.CreateChartImage().createImage(request,response,tempChart);

%>



--------两个jsp---------------

--------------------主-----------------------
<table width="100%" >
<tr>
<td align="center"><img  src="item_pic.jsp?ITEM_ID=<%=item_id%>&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.executeQuery(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){
   }
}
%>

posted on 2005-08-16 18:12  轻松逍遥子  阅读(665)  评论(0编辑  收藏  举报