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.io.PrintWriter; import java.net.URLDecoder; import java.text.SimpleDateFormat; import java.util.ArrayList; 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 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.jbo.JBOTransaction; import com.amarsoft.are.util.DataConvert; import com.amarsoft.awe.Configure; import com.tenwa.app.invoice.utils.FileCopy; import com.tenwa.app.invoice.utils.FilesToZipUtils; import com.tenwa.comm.util.jboutil.DataOperatorUtil; import com.tenwa.util.SecurityUtil; public class InvoiceLotDownloadServlet extends HttpServlet { private static final long serialVersionUID = 1L; public InvoiceLotDownloadServlet() { super(); } @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { JBOTransaction tx= null; String files = request.getParameter("files"); String[] arr = files.split("@"); String str = ""; //String sid =""; //String customerName = ""; String contract_no = ""; String zipLocalPath = ""; List filess = new ArrayList<>(); for (int i = 0; i < arr.length; i++) { str = arr[i]; try { tx = JBOFactory.createJBOTransaction(); /*BizObject invoiceResult = JBOFactory.createBizObjectQuery("jbo.app.tenwa.calc.LI_INVOICE_RESULT", "localFile=:localFile").setParameter("localFile", str).getSingleResult(false); if(invoiceResult !=null){ 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(); */ 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", str); List> data; try { data = DataOperatorUtil.getDataBySql(tx, sql, conditon); if(data !=null){ contract_no = data.get(0).get("contract_no"); } } catch (Exception e) { e.printStackTrace(); } conditon.clear(); tx.commit(); } catch (JBOException e1) { 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 copyLocalPath = CurConfig.getConfigure("COPY_LOCAL_PATH"); zipLocalPath = CurConfig.getConfigure("ZIP_LOCAL_PATH"); String path = DataConvert.toString(str); path = SecurityUtil.detrypt(path, "invoice"); localFile = localFile + path; File file = new File(localFile); if (!file.exists()) throw new Exception("文件不存在"); //复制文件 File from = new File(localFile); String pdfname = ""; pdfname = contract_no + ".pdf"; //String name ="E:/liuzhaoPDF/"+pdfname; String name=""; name =copyLocalPath+"/"+pdfname; File to = new File(name); if(filess.contains(to)){ contract_no +="-1"; pdfname = contract_no + ".pdf"; name =copyLocalPath+"/"+pdfname; to = new File(name); } FileCopy.copyFile(from, to); //filess.add(new File(localFile)); filess.add(to); } catch (Exception e) { ARE.getLog().error("AttachmentView SQL Error:", e); } } SimpleDateFormat dateFormat = new SimpleDateFormat("YYYYMMddHHmmss"); String filename = dateFormat.format(new Date()) + ".zip"; //String zipFilename ="E:/liuzhaoCopyPDF/"+filename; String zipFilename =zipLocalPath+"/"+filename; try { FilesToZipUtils.toZipFiles(filess, zipFilename); } catch (Exception e) { e.printStackTrace(); //error } File localZipFile = new File(zipFilename); try { if (localZipFile.exists() && localZipFile.isFile() && localZipFile.length() > 0) { response.setContentType(getServletContext().getMimeType(filename)); response.setHeader("Content-Disposition", "attachment;filename=" + filename); InputStream in = new FileInputStream(localZipFile); OutputStream out = response.getOutputStream(); int b; while ((b = in.read()) != -1) { out.write(b); } in.close(); out.close(); }else{ throw new Exception("压缩包不存在!"); }} catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }