package com.csnt.scdp.bizmodules.helper;
import com.csnt.scdp.framework.helper.IOHelper;
import com.csnt.scdp.framework.util.UUIDUtil;
import freemarker.template.Template;
import org.apache.struts2.ServletActionContext;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;/**
* Created by weixiao on 2018/9/6.*/public class DocGenerateHelper {
public static String genDoc(Map outMap, Template t, String clientFileName) throws Exception {
SimpleDateFormat df= new SimpleDateFormat("MMddHHmmss");
String tempFileName= IOHelper.checkPath(System.getProperty("java.io.tmpdir")) + "docgenerate" + File.separator + df.format(new Date()) + File.separator + clientFileName + ".doc";
File file= newFile(tempFileName);
File fileParent=file.getParentFile();//首先创建父类文件夹
if (!fileParent.exists()) {
fileParent.mkdirs();
}
file.createNewFile();
Writer out= null;try{
IOHelper.generateEmptyDic(tempFileName,false);
out= new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tempFileName), "utf-8"), 10240);
t.process(outMap, out);
}finally{
out.flush();
out.close();
}returntempFileName;
}//通过文件输入输出流下载word
//设置向浏览器端传送的文件格式
public static voidgenerateDocWithDownload(Map outMap, Template t, String clientFileName,HttpServletResponse response) throws Exception {if (clientFileName == null || clientFileName == "") {
clientFileName=UUIDUtil.getUUID();
}
String fileName=genDoc(outMap, t, clientFileName);
response.reset();
InputStream fis= null;
OutputStream toClient= null;
File outFile= newFile(fileName);try{
fis= new BufferedInputStream(newFileInputStream(outFile));byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();//清空response
response.reset();//设置response的Header
clientFileName = URLEncoder.encode(clientFileName + ".doc", "utf-8"); //这里要用URLEncoder转下才能正确显示中文名称
response.addHeader("Content-Disposition", "attachment;filename=" +clientFileName);
response.addHeader("Content-Length", "" +outFile.length());
toClient= newBufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
fis.close();
toClient.close();
}finally{
WordExportHelper.deleteFile(fileName);
String directory=fileName.substring(0,fileName.lastIndexOf("\\"));
WordExportHelper.deleteFile(directory);if (fis != null) {
fis.close();
}if (toClient != null) {
toClient.close();
}
}
}//通过文件输入输出流下载word
//设置向浏览器端传送的文件格式
public static voidgenerateDocDownload(String path,String clientFileName,HttpServletResponse response) throws Exception {if (clientFileName == null || clientFileName == "") {
clientFileName=UUIDUtil.getUUID();
}//String fileName=genDoc(clientFileName);
response.reset();
InputStream fis= null;
OutputStream toClient= null;
File outFile= newFile(path);try{
fis= new BufferedInputStream(newFileInputStream(outFile));byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();//清空response
response.reset();//设置response的Header//clientFileName = URLEncoder.encode(clientFileName + ".doc", "utf-8"); //这里要用URLEncoder转下才能正确显示中文名称
response.addHeader("Content-Disposition", "attachment;filename=" +clientFileName);
response.addHeader("Content-Length", "" +outFile.length());
toClient= newBufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.flush();
fis.close();
toClient.close();
}finally{if (fis != null) {
fis.close();
}if (toClient != null) {
toClient.close();
}
}
}
public static String genDoc(String clientFileName) throws Exception {
SimpleDateFormat df= new SimpleDateFormat("MMddHHmmss");
String tempFileName= IOHelper.checkPath(System.getProperty("java.io.tmpdir")) + "docgenerate" + File.separator + df.format(new Date()) + File.separator + clientFileName + ".doc";
File file= newFile(tempFileName);
File fileParent=file.getParentFile();//首先创建父类文件夹
if (!fileParent.exists()) {
fileParent.mkdirs();
}
file.createNewFile();
Writer out= null;try{
IOHelper.generateEmptyDic(tempFileName,false);
out= new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tempFileName), "utf-8"), 10240);
}finally{
out.flush();
out.close();
}returntempFileName;
}
public static String generateDocWithDownload_temp(Map outMap, Template t, String clientFileName) throws Exception {
String fileUrl= "";if (clientFileName == null || clientFileName == "") {
clientFileName=UUIDUtil.getUUID();
}
String fileName=genDoc(outMap, t, clientFileName);
HttpServletResponse response=ServletActionContext.getResponse();
response.reset();
InputStream fis= null;
OutputStream toClient= null;
File outFile= newFile(fileName);try{
fis= new BufferedInputStream(newFileInputStream(outFile));byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();//清空response
response.reset();//设置response的Header
clientFileName = URLEncoder.encode(clientFileName + ".doc", "utf-8"); //这里要用URLEncoder转下才能正确显示中文名称
response.addHeader("Content-Disposition", "attachment;filename=" +clientFileName);
response.addHeader("Content-Length", "" +outFile.length());
toClient= newBufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
fileUrl=outFile.toString();
fis.close();
toClient.close();
}finally{if (fis != null) {
fis.close();
}if (toClient != null) {
toClient.close();
}
}returnfileUrl;
}
}