package com.tenwa.app.invoice.servlet; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URLDecoder; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import jbo.app.tenwa.calc.LI_INVOICE_RESULT; import com.amarsoft.are.ARE; import com.amarsoft.are.jbo.BizObject; import com.amarsoft.are.jbo.BizObjectManager; import com.amarsoft.are.jbo.JBOException; import com.amarsoft.are.jbo.JBOFactory; import com.amarsoft.are.jbo.JBOTransaction; import com.amarsoft.are.util.DataConvert; import com.amarsoft.awe.Configure; import com.tenwa.comm.util.jboutil.DataOperatorUtil; import com.tenwa.util.SecurityUtil; public class InvoiceDownloadServlet extends HttpServlet { private static final long serialVersionUID = 1L; public InvoiceDownloadServlet() { super(); } @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String contract_no = ""; String slocalFile = request.getParameter("path"); JBOTransaction tx= null; try { tx = JBOFactory.createJBOTransaction(); String sql = "SELECT DISTINCT lci.contract_no FROM LI_INVOICE_RESULT lir LEFT JOIN li_invoice_confirm lic ON lic.sid = lir.sid LEFT JOIN li_invoice_confirm_detail licd ON licd.invoice_confirm_id = lic.id LEFT JOIN li_rent_invoice_info lrii ON lrii.id=licd.invoice_id LEFT JOIN lb_contract_info lci ON lci.id=lrii.contract_id WHERE lir.localFile=:localFile"; Map conditon = new HashMap(); conditon.put("localFile", slocalFile); List> data; try { data = DataOperatorUtil.getDataBySql(tx, sql, conditon); if(data !=null){ contract_no = data.get(0).get("contract_no"); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } conditon.clear(); tx.commit(); /*if(invoiceResult !=null){ contract_no = invoiceResult.getAttribute("contract_no").toString(); }*/ } catch (JBOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { HttpSession session = request.getSession(true); if ((session == null) || (session.getAttributeNames() == null)) { throw new Exception("------Timeout------"); } Configure CurConfig = Configure.getInstance(session.getServletContext()); if (CurConfig == null) { throw new ServletException("读取配置文件错误!请检查配置文件。"); } String localFile = CurConfig.getConfigure("INVOICE_LOCAL_PATH"); String path = DataConvert.toString(request.getParameter("path")); path = SecurityUtil.detrypt(path, "invoice"); localFile = localFile + path; File file = new File(localFile); if (!file.exists()) throw new Exception("文件不存在"); SimpleDateFormat dateFormat = new SimpleDateFormat("YYYYMMddHHmmss"); String filename = contract_no+dateFormat.format(new Date()) + ".pdf"; //String filename = contract_no + ".pdf"; response.setContentType(getServletContext().getMimeType(filename)); response.setHeader("Content-Disposition", "attachment;filename=" + new String( filename.getBytes("gb2312"), "ISO8859-1" ) ); InputStream in = new FileInputStream(localFile); OutputStream out = response.getOutputStream(); int b; while ((b = in.read()) != -1) { out.write(b); } in.close(); out.close(); } catch (Exception e) { ARE.getLog().error("AttachmentView SQL Error:", e); } } }