import java.io.Writer;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.FileOutputStream;
import oracle.sql.BLOB;
import oracle.jdbc.OracleResultSet;import java.sql.Connection;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import org.apache.struts.action.ActionForm;
public class DownloadBean extends BaseBean{    private String strSELECT = "SELECT INFILE_DATA FROM BILL_INFILE_TBL" ;
    
public BLOB Download()
            throws Exception
     {
        InputStream in = null;
        OutputStream out = null;
        BLOB blob = null;
        PreparedStatement  pstmt = null;
        ResultSet rs = null;
        Connection conn = null;
        try{
            conn = getDataSource().getConnection();
            pstmt = conn.prepareStatement(strSELECT);            rs = pstmt.executeQuery();            if (rs.next()) {
                blob = (BLOB)rs.getBlob("INFILE_DATA");
                
                in = blob.getBinaryStream();
                out = new FileOutputStream("c:\\xxx.jpg");
                int BuffSize = blob.getBufferSize();
                byte[] buffer = new byte[BuffSize];
                int bytesRead = 0;
                while ((bytesRead = in.read(buffer)) != -1) {
                    out.write(buffer, 0, bytesRead);
                }
                buffer = null;
                
            }
            pstmt.clearParameters();        }catch(SQLException sqle) {
            sqle.printStackTrace();
            throw sqle;
        }catch(Exception e) {
            e.printStackTrace();
            throw e;
        }finally {
            try{
                
                in.close();
                out.close();
                in = null;
                out = null;
                blob = null;
        
            }catch(Exception e) {throw e;}            if (rs != null) {
                try{
                    rs.close();
                }catch(Exception e){throw e;}
            }            if (pstmt != null) {
                try{
                    pstmt.close();
                }catch(Exception e) {throw e;}
            }            return blob;
        }    }
}

解决方案 »

  1.   

    import javax.sql.DataSource;
    import oracle.jdbc.driver.*;
    import org.apache.struts.upload.FormFile;
    import java.io.*;
    import java.sql.*;
    import java.sql.SQLException;public class UploadBean extends BaseBean {    public static final String INFILE_DATA = "INFILE_DATA";    private String strSELECT = "SELECT INFILE_DATA FROM BILL_INFILE_TBL " ;    private String strInsertSQL = "INSERT INTO BILL_INFILE_TBL ( " +
                                      "        INFILE_DATA,          " +    
                                      "        )                " +    
                                      "  VALUES(                     " +
                                      "         EMPTY_BLOB(),    " +    
                                      "        )         ";         public void Upload(FormFile file
                           )throws Exception    {
            DataSource ds = getDataSource();        Connection con = null;
            PreparedStatement stmt =null;        try{
                con = ds.getConnection();            stmt= con.prepareStatement(strInsertSQL);            stmt.executeUpdate();
                stmt.close();            stmt=con.prepareStatement(strSELECT);            ResultSet rs = stmt.executeQuery();
                while (rs.next()) {
                        oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("INFILE_DATA");
                        BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());
                        BufferedInputStream in = new BufferedInputStream(file.getInputStream());                    int c;
                        while ((c=in.read())!=-1) {
                            out.write(c);
                        }
                        in.close();                    out.close();             }            stmt.close();
                con.commit();        }catch(Exception e) {
                e.printStackTrace();            try{
                    con.rollback();
                }catch(Exception e1) {throw e1;}            throw e;
            }finally {
                try{
                    stmt.close();
                }catch(Exception e) {
                    throw e;
                }            try{
                    con.close();
                }catch(Exception e) {
                    throw e;
                }
            }
        }
    }
      

  2.   

    谢谢 sagittarius1979(射手爱狮子)
    狮子 爱  天秤
    哈哈哈哈哈
      

  3.   

    sagittarius1979(射手爱狮子)的不错
    支持!
      

  4.   

    太好了。
      DESL (奇多多 o_O (★★★★))  
    说做就做,是个好版主,
    大力支持!!!
      

  5.   

    编译出错,
    package not existsoracle.sql.BLOB;
    org.apache.struts.action.ActionForm;
    从那找啊
      

  6.   

    oracle.sql.BLOB
    装oracle的驱动即可org.apache.struts.action.ActionForm
    这个,你用不到,删掉吧
      

  7.   

    我来发布一个从服务器某目录下下载文件的例子
    import java.io.IOException;
    import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequestWrapper;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.RequestDispatcher;
    import java.io.File;
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.FileInputStream;public class download extends HttpServlet { private static final String CONTENT_TYPE = "text/html; charset=GBK";   //Initialize global variables
       public void init() throws ServletException {
       }
       //Process the HTTP Get request
       public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //HttpServletRequestWrapper httpReqWrapper = (HttpServletRequestWrapper) request;
    //HttpServletRequest httpRequest =(HttpServletRequest)httpReqWrapper.getRequest();
    try {
           downloadArtifact(request, response);
         }catch (Exception x) {
           x.printStackTrace();
           RequestDispatcher dispatcher = this.getServletContext().
               getRequestDispatcher("/common/ShowError.jsp");
          
           dispatcher.forward(request, response);
           }
        }
        
       public void downloadArtifact(HttpServletRequest request, HttpServletResponse response) throws Exception {
         Exception ex=null;
         String fileId = (String)request.getParameter("ID");
         String realFile = null;
         String realFileName = "";
         File uploadDir = new File("D:\\upload");
         if (uploadDir.isDirectory()) {
         File[] files = uploadDir.listFiles();
         for (int j = 0; j < files.length; j++) {
         File tempFile = files[j];
         if ((tempFile.getName().indexOf(fileId)) >= 0){
         //System.err.println("get The file" + tempFile.getName());
         realFile = tempFile.getName();
         break;
         }
         }
         }
         int seperatePos = realFile.lastIndexOf(".");
         String fileAppendix = realFile.substring(seperatePos+1);
         String tempFileName = realFile.substring(0,seperatePos);
         int idIndex = tempFileName.lastIndexOf(fileId);
         realFileName = tempFileName.substring(0,idIndex) + "." + fileAppendix;
        
    File file = new File("D:\\upload", realFile);
    if(!file.exists()) throw new Exception("Sorry, File Not Found");
         int length=(new Long(file.length())).intValue();
         response.setContentType("application/octet-stream; charset=UTF-8");
         response.setHeader("Content-disposition", "attachment; filename=\""+realFileName+"\"");
         //response.setHeader("Content-disposition", "attachment; filename=\""+file.getName()+"\"");
         //System.err.println("******" + realFileName);
         int bufferSize=1024;
    BufferedOutputStream output = null;
         BufferedInputStream input = null;
         output = new BufferedOutputStream(response.getOutputStream());
         input = new BufferedInputStream(new FileInputStream(file));
         try {
           int once = 0;
           int total = 0;
          byte[] buffer = new byte[bufferSize];
           do {
             once = input.read(buffer);
             total += once;
             if (once >= 0)
               output.write(buffer, 0, bufferSize);
           }
           while ( (total < length) && (once >= 0));
           response.flushBuffer();
         }
         catch (Exception e) {
           ex = e;
         } // maybe user cancelled download
         finally {
           if (input != null) input.close();
           if(output!=null) output.close();       if(null!=ex) throw ex;
         }
        }   //Process the HTTP Post request
       public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doGet(request, response);
       }
    }