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 javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.amarsoft.are.ARE; import com.amarsoft.are.jbo.BizObject; import com.amarsoft.are.jbo.JBOException; import com.amarsoft.are.jbo.JBOFactory; import com.amarsoft.are.util.DataConvert; import com.amarsoft.awe.Configure; 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 customerName = ""; String sid = ""; try { BizObject invoiceResult = JBOFactory.createBizObjectQuery("jbo.app.tenwa.calc.LI_INVOICE_RESULT", "localFile=:localFile").setParameter("localFile", request.getParameter("path")).getSingleResult(false); sid = invoiceResult.getAttribute("sid").toString(); BizObject invoiceConfirm = JBOFactory.createBizObjectQuery("jbo.app.tenwa.calc.LI_INVOICE_CONFIRM", "sid=:sid").setParameter("sid",sid).getSingleResult(false); if(invoiceConfirm !=null){ customerName = invoiceConfirm.getAttribute("ghdwmc").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 = dateFormat.format(new Date()) + ".pdf"; String filename = customerName + ".pdf"; //response.setContentType("text/html;charset=UTF-8"); 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); } } }