从数据库中读取Blob对象图片并显示
来源:互联网 发布:mac os x的终端命令 编辑:程序博客网 时间:2024/05/16 06:17
大致方法就是,从数据库中读出Blob的流来,把流写到页面中去:
package com.XXXXXX.file;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.XXXXXX.jdbc.DBManager;
public class DownloadAsStream extends HttpServlet ...{
/** *//**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
doPost(request,response);
}
/** *//**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
Connection conn = DBManager.getConnection();
String sql = "SELECT picture FROM teacher WHERE id=1";
PreparedStatement ps = null;
ResultSet rs = null;
InputStream is = null;
OutputStream os = null;
try ...{
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
if(rs.next())...{
is = rs.getBinaryStream(1);
}
response.setContentType("text/html");
os = response.getOutputStream();
int num;
byte buf[] = new byte[1024];
while( (num=is.read(buf))!=-1 )...{
os.write(buf, 0, num);
}
} catch (SQLException e) ...{
e.printStackTrace();
}
try ...{
is.close();
os.close();
rs.close();
ps.close();
} catch (SQLException e) ...{
e.printStackTrace();
}
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.XXXXXX.jdbc.DBManager;
public class DownloadAsStream extends HttpServlet ...{
/** *//**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
doPost(request,response);
}
/** *//**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
Connection conn = DBManager.getConnection();
String sql = "SELECT picture FROM teacher WHERE id=1";
PreparedStatement ps = null;
ResultSet rs = null;
InputStream is = null;
OutputStream os = null;
try ...{
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
if(rs.next())...{
is = rs.getBinaryStream(1);
}
response.setContentType("text/html");
os = response.getOutputStream();
int num;
byte buf[] = new byte[1024];
while( (num=is.read(buf))!=-1 )...{
os.write(buf, 0, num);
}
} catch (SQLException e) ...{
e.printStackTrace();
}
try ...{
is.close();
os.close();
rs.close();
ps.close();
} catch (SQLException e) ...{
e.printStackTrace();
}
}
}
在页面中:
<%...
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<img name="pic" src="<%=basePath+"servlet/DownloadAsStream"%>"/>
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<img name="pic" src="<%=basePath+"servlet/DownloadAsStream"%>"/>
搞定。
- 从数据库中读取Blob对象图片并显示
- 从数据库中读取Blob对象图片并显示
- 从数据库中读取Blob对象图片并显示
- Java从数据库中读取Blob对象图片并显示
- 从数据库中读取blob字段并生成图片文件
- grails从数据库读取blob显示图片
- 数据库存储BloB格式图片,并从数据库中取出显示到页面中
- C#从数据库中读取二进制数据,并显示图片
- PowerBuilder从数据库读取并显示图片。
- Jsp从数据库读取并显示图片
- hibernate从sqlserver数据库中读取图片 Image (Blob)
- 利用数据库如何存入BLOB格式图片,并从数据库中取出BLBO格式图片显示出来
- 将图片保存到数据库表中及从数据库表中读取图片并显示
- 从Oracle中数据库中读取以Blob类型存储的图片显示在jsp页面中
- 从oracle中读取blob图片资源
- 从数据库中读取多张图片并显示在同一页面中
- JSP读取Oracle数据库里的图片Blob字段并显示在页面上
- 用JSP从数据库中读取图片并显示在网页上
- CONVERSION_EXIT_ALPHA_INPUT
- 孙振耀畅谈人生
- WCF
- PL/pgSQL - SQL过程语言
- ABAP 判断内表为空
- 从数据库中读取Blob对象图片并显示
- Windows 启动过程
- 一个用Jdom输出XML的java程序
- Ajax In Action 附录 B3.2
- 部分GNU代码片 13、函数中malloc内存,函数外用
- ConfigurationManager的使用。
- “谁养鱼”的穷举解法。
- Ajax In Action 附录 B3.3
- 英国《金融时报》:北京房奴VS纽约房奴