扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
/*
* 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");
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("<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";
}
}
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者