/ * * Readfile.java * * CREATED ON November 17, 2004, 3:41 pm * /
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 get code> and
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); // This class is only available for Oracle, but it can be used, using InputStream IS = RS .getBinaryStream (2); This second field is a blob field, then read it, and write it to the customer browser with the following.
// byterrayoutputstream ba = new byterrayoutputstream (); response.reset ()); response.reset (); //response.setheader ("talk-disposition", "attachem; filename = ss.doc;"); // ** To the browser directly, filename is the file name response.setContentType ("Application / Msword; Charset = GBK"); // ** This is the MIME type of the file, which can be found online, this example To Word //application/vnd.ms-excel; character //application/vnd.pdf; in; if (RS), the PDF file type servletoutputstream out = response.getstream out = response.getStream out = response.getstream out = response.getOutputStream (); inputstream in; if (RS. NEXT ()) in = blob.getbinaryStream (); while (in.read (buf)! = - 1) {Out.write (buf);} out.flush (); out.close (); // ** The following is the example of writing, such as a file, ============================ // WHILE (rs.next ()) {// servletoutputstream snout = response. getOutputStream (); // InputStream in = rs.getbinaryStream (1); // Press the first word of the RS result set according to the binary Segment // Byte B [] = new byte [0x7a120]; // for (INT i = in.read (b); i! = -1;) {/ sout.write (b); // in.read (b); //} // sout.flush (); // snout.close (); //} / / ===================== // FileOutputStream file_out = new fileoutputStream ("f: // filename"); // inputstream blob_in = blob.getbinaryStream (); // int Temp;