动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   数据库 → 阅读文章
  

 servlet实现从oracle数据库的blob字段中读出文件并显示

作者来源: 
阅读 数 428 人次 , 2006-4-13 16:18:00 

/*
 * ReadFile.java
 *
 * Created on 2004年11月17日, 下午3:41
 */

package jgwl.app;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import jgwl.base.DataBaseConnection;
import java.io.*;
import oracle.sql.*;

/**
 *
 * @author Roy Chen 
 * @version
 */
public class ReadFile extends HttpServlet {
 
  /** Initializes the servlet.
*/
  public void init(ServletConfig config) throws ServletException {
  super.init(config);
 
  }
 
  /** Destroys the servlet.
*/
  public void destroy() {
 
  }
 
  /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
 
  byte[] buf=new byte[4096];
  try{
  Statement stmt=new DataBaseConnection().getStatement();
  ResultSet rs=stmt.executeQuery("select * from demo where id=23");
response.reset();
  //response.setHeader("Content-Disposition", "attachement;filename=ss.doc;");
  //**如要浏览器直接另存为就不屏蔽上行,filename 为文件名
  response.setContentType("application/msword;charset=GBK");
//**此处为文件的mime类型,可以在网上搜到,本例为word
  //application/vnd.ms-excel;charset=GBK  表示excel文件类型
//application/vnd.pdf;charset=GBK  表示pdf文件类型

  ServletOutputStream out=response.getOutputStream();
 InputStream in;
if(rs.next())
in = blob.getBinaryStream();
  while(in.read(buf)!=-1){
  out.write(buf);
  }
 
  out.flush();
  out.close();
 //**以下为网上看到的写如文件的样例
//==================
  // while(rs.next()) {
  //ServletOutputStream sout = response.getOutputStream();
  //InputStream in = rs.getBinaryStream(1);
  //byte b[] = new byte[0x7a120];
  //for(int i = in.read(b); i != -1;){
  // sout.write(b);
  //  in.read(b);
  // }
  // sout.flush();
  // sout.close();
  //}
 
  //====================
  //FileOutputStream file_out=new FileOutputStream("f:\\filename");
 
  //InputStream blob_in=blob.getBinaryStream();
  //int temp;
  // while((temp=blob_in.read())!=-1){
  // file_out.write(temp);}
  // blob_in.close();
  // file_out.close();
  //out.write(blob.getBinaryStream().);
  //out.flush();
  stmt.close();
  }catch (SQLException e){
  e.printStackTrace();
  }
  //PrintWriter out = response.getWriter();
  /* TODO output your page here
  out.println("<html>");
  out.println("<head>");
  out.println("<title>Servlet</title>");
  out.println("</head>");
  out.println("<body>");

  out.println("</body>");
  out.println("</html>");
*/
  //out.close();
  }
 
  /** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
  processRequest(request, response);
  }
 
  /** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
  processRequest(request, response);
  }
 
  /** Returns a short description of the servlet.
*/
  public String getServletInfo() {
  return "Short description";
  }
 
}

 本文Tagsoracle  数据库  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:工作中遇到的oracle故障分析和处理一例
· 下一篇:项目中的oracle开发技巧
· PHP V5.0.0b23 源代码下载地址列表
· DB2企业内容管理产品的分析(1)
· SQL Server2000数据库文件损坏时如何恢复
· MySQL数据导入导出方法与工具介绍
· SQL Server数据库技术(97)


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077