* 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
GET
and POST
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("");
out.println("");
out.println("
out.println("");
out.println("");
out.println("");
out.println("");
*/
//out.close();
}
/** Handles the HTTP
GET
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
POST
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";
}
}
没有评论:
发表评论