public class PayAccDownLoadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.reset(); response.setContentType("application/x-msdownload; charset=gb2312"); response.setHeader("Pragma","No-cache"); response.setHeader("Cache-Control","no-cache"); response.setDateHeader("Expires", 0); ServletOutputStream out2 = response.getOutputStream(); long id=Convert.strToLong(request.getParameter("id")); Connection conn=DBConnectionPool.getNewConnection(); PreparedStatement statement=null; ResultSet rs=null; InputStream acc_data1 = null; try { statement=conn.prepareStatement("select o.*,i.code,i.title,i.sum_group,i.custom from t_mp_other_pay as o,t_mp_pay_item as i where i.id=o.pay_item_id and o.id=?"); statement.setLong(1,id); rs=statement.executeQuery(); if (rs.next()) { byte[] fileData = read(rs.getBinaryStream("acc_data")); setHeadData(response, rs.getString("acc_title")); out2.write(fileData); out2.close(); } } catch(Exception e) { e.printStackTrace(); } finally{ DBConnectionPool.closeResultsetSafey(rs); DBConnectionPool.closeStatementSafey(statement); DBConnectionPool.closeConnectionSafey(conn); } } public static byte[] read(InputStream in) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { byte[] bs = new byte[10240]; int n = 0; while ((n = in.read(bs)) != -1) bos.write(bs, 0, n); return bos.toByteArray(); } finally { in.close(); } } public static void setHeadData(HttpServletResponse response, String fileName) throws UnsupportedEncodingException { response.setContentType("APPLICATION/OCTET-STREAM;charset=UTF-8"); String headerDisp = new String(("filename=" + fileName).getBytes("GBK"), "iso8859-1"); response.setHeader("Content-disposition", "attachment;" + headerDisp); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { super.doGet(request, response); }