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

/*
 * 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");

            rs.next();
           //buf=rs.getBytes("theblob");
           Blob blob=rs.getBlob(2);//这个类只有oracle提供的驱动才有,但是可以不用,用InputStream is=rs.getBinaryStream(2);此处第二字段为Blob字段,然后读出来,同下面的用out.write(buf)方式写到客户浏览器。
           //ByteArrayOutputStream ba=new ByteArrayOutputStream(blob.toString());
           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);//按二进制流取rs结果集的第一个字段
            //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";
    }
   
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值