Download java front-end document tools

package com.ry.project.util.commUtil;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.*;
import java.util.Map;

public class WordUtil {
private Configuration configuration = null;

public WordUtil() {
configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
}
//指定下载路径
public void createDoc(Map<String, Object> dataMap, String fileName) throws UnsupportedEncodingException {
configuration.setClassForTemplateLoading(this.getClass(), "/");
Template doct = null;
try {
doct = configuration.getTemplate("infoModel.ftl", "utf-8");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

File outfile = new File(fileName);

Writer out = null;
FileOutputStream outputStream = null;

try {
outputStream = new FileOutputStream(outfile);
OutputStreamWriter oWriter = new OutputStreamWriter(outputStream, "UTF-8");
out = new BufferedWriter(oWriter);

} catch (FileNotFoundException e) {
// Auto-Generated Block the catch the TODO
e.printStackTrace ();
}

the try {
doct.process (Datamap, OUT);
the out.close ();
outputStream.close ();

} the catch (TemplateException E) {
// the TODO the catch Auto-Generated Block
e.printStackTrace ();
} the catch (IOException E) {
// the TODO Auto-Generated Block the catch
e.printStackTrace ();
}

}
// do not specify a download path ,,, default browser to download, the request needs to be set in the code first
public void createDoc (the Map <String, Object> Datamap, the OutputStream outfil) throws UnsupportedEncodingException {

configuration.setClassForTemplateLoading ( this.getClass (), "/");
the System.out.println(">>>>>class:" + this.getClass());
Template doct = null;
try {
doct = configuration.getTemplate("model.ftl", "utf-8");
} catch (IOException e) {
e.printStackTrace();
}
Writer out = null;
OutputStreamWriter oWriter = new OutputStreamWriter(outfil, "UTF-8");
out = new BufferedWriter(oWriter);

try {
doct.process(dataMap, out);
System.out.println("《《《《《《《《下载结束》》》》》");
out.close();

} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}


response.reset (); // remove blank lines textile error 
response.setHeader ( "Access-Control-the Allow-Origin", "*"); // all domains across
response.setHeader ( "Content-Type", " file application / OCTET-stream; charset = UTF-. 8 "); // binary stream file
response.setHeader ( " the Content-Disposition ", " Attachment; filename = "+ + endCodeFileName " .doc "); // file name and download
response.setHeader ( "connection", "Close"); // close the connection request header
// open settings file browser or download
the response.setContentType ( "file application / X-downloads");
 


}

Guess you like

Origin www.cnblogs.com/wangbiaohistory/p/11761869.html