apzl_leasing/src_invoice/com/tenwa/app/invoice/servlet/InvoiceLotDownloadServlet.java
2018-09-16 10:22:19 +08:00

137 lines
4.4 KiB
Java
Raw Blame History

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.List;
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.app.invoice.utils.FileCopy;
import com.tenwa.app.invoice.utils.FilesToZipUtils;
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 {
String files = request.getParameter("files");
String[] arr = files.split("@");
String str = "";
String sid ="";
String customerName = "";
String zipLocalPath = "";
List<File> filess = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
str = arr[i];
try {
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();
}
} 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("<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>");
}
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("<EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
//<2F><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
File from = new File(localFile);
String pdfname = customerName + ".pdf";
//String name ="E:/liuzhaoPDF/"+pdfname;
String name =copyLocalPath+pdfname;
File 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("ѹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD>");
}} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}