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.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 { 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"; response.setContentType(getServletContext().getMimeType(filename)); response.setHeader("Content-Disposition", "attachment;filename=" + filename); 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); } } }