不经过临时文件,直接从BLOB字段,下载数据显示图片!
2005-08-16 18:12
966 查看
看下面的这个函数:
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){
}
}
%>
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){
}
}
%>
相关文章推荐
- 如何直接下载保存在Oracl数据库中Blob字段的文件(不生成临时文件)???
- 用于显示ORACLE_BLOB存储的图片、flash文件的JSP文件,用于直接下载文件的JSP
- 保存在数据中image字段的图片文件显示出来
- 保存在数据中image字段的图片文件显示出来
- Struts2直接下载从数据中的blob类型字段( Struts2.0下载文件流)
- c# cs显示字段为blob类型的图片数据
- 文件上传下载and图片上传及前台显示
- java下载远程http地址的图片文件到本地-自动处理图片是否经过服务器gzip压缩的问题
- VFP将图片或其他文件保存到SQL Server的Text类型字段或者Access的备注字段中,并可以还原或显示。 .
- Control Study->AdRotator广告控件显示自定义配置文件中广告以及与DataGrid控件结合使用显示图片(示例代码下载)
- 阿里云DRDS保存数据blob字段乱码,文件损坏
- 解决下载下来的flexgrid.js下载下来直接使用数据不能显示的问题
- VC6.0中使用Stream Object读取数据中流文件并显示Bmp,JPG等图片
- 用.sql文件通过navicat导数据到mysql中,大文本会显示BLOB的问题
- 利用COM组件IPicture读取jpg、gif、bmp图片文件数据和显示图片
- 使用redis文件服务器完成图片下载显示
- myBatis + SpringMVC上传、下载文件、显示byte图片文件、等比例缩放图片
- JAVA读取Oracle中的blob图片字段并显示
- winform直接显示二进制数据中的图片
- 下载文件出现提示框或者直接显示在浏览器中