熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Oracle >> 正文

servlet實現從oracle數據庫的blob字段中讀出文件並顯示

2022-06-13   來源: Oracle 

  /*
 * ReadFilejava
 *
 * Created on 下午:
 */

  package jgwlapp;

  import javaio*;
import *;

  import javaxservlet*;
import javaxservlethttp*;
import javasql*;
import jgwlbaseDataBaseConnection;
import javaio*;
import oraclesql*;

  /**
 *
 * @author  Roy Chen
 * @version
 */
public class ReadFile extends HttpServlet {
   
    /** Initializes the servlet
     */
    public void init(ServletConfig config) throws ServletException {
        superinit(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[];
        try{
            Statement stmt=new DataBaseConnection()getStatement();
            ResultSet rs=stmtexecuteQuery(select * from demo where id=);
            responsereset();
            //responsesetHeader(ContentDisposition attachement;filename=ssdoc;);
            //**如要浏覽器直接另存為就不屏蔽上行filename 為文件名
            responsesetContentType(application/msword;charset=GBK);
            //**此處為文件的mime類型可以在網上搜到本例為word
            //application/vndmsexcel;charset=GBK  表示excel文件類型
           //application/vndpdf;charset=GBK           表示pdf文件類型

  ServletOutputStream out=responsegetOutputStream();
           InputStream in;
           if(rsnext())
             in = blobgetBinaryStream();
            while(inread(buf)!=){
                outwrite(buf);
            }
           
            outflush();
            outclose();
           //**以下為網上看到的寫如文件的樣例
           //==================
            // while(rsnext()) {
            //ServletOutputStream sout = responsegetOutputStream();
            //InputStream in = rsgetBinaryStream();
            //byte b[] = new byte[xa];
            //for(int i = inread(b); i != ;){
            // soutwrite(b);
            //  inread(b);
            // }
            // soutflush();
            // soutclose();
            //}
           
            //====================
            //FileOutputStream file_out=new FileOutputStream(f:\filename);
           
            //InputStream blob_in=blobgetBinaryStream();
            //int temp;
            // while((temp=blob_inread())!=){
            // file_outwrite(temp);}
            // blob_inclose();
            // file_outclose();
            //outwrite(blobgetBinaryStream());
            //outflush();
            stmtclose();
        }catch (SQLException e){
            eprintStackTrace();
        }
        //PrintWriter out = responsegetWriter();
        /* TODO output your page here
        outprintln(<html>);
        outprintln(<head>);
        outprintln(<title>Servlet</title>);
        outprintln(</head>);
        outprintln(<body>);
        
        outprintln(</body>);
        outprintln(</html>);
         */
        //outclose();
    }
   
    /** 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;
    }
   
}


From:http://tw.wingwit.com/Article/program/Oracle/201311/17924.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.