Java uses Freemarker to generate (export) Word documents (bug modifications) __java

Source: Internet
Author: User
Bug One: Put the code on the server, can download the Word file, but, download to the server on the C disk;
Solve:
To modify a partial export.js code:
function Table01 () {
var stime = $ (' #day '). Val ();
var etime = $ (' #end '). Val ();
$.ajax ({
Type: "POST",
url:path+ "/yuqing/table01",
data:{
Stime:stime,
Etime:etime
},
Cache:false,
DataType: "JSON",
Async:false,
Success:function (data) {
Downword ();
}
});
}
function Downword () {
var url = path + '/indexdownload/downword ';
Window.location.href=encodeuri (URL);
}
In Com.yuanls.index.controller.IndexDownloadController.java: (Part of the Code:)
@Controller
@RequestMapping (value = "/indexdownload")
@SuppressWarnings ("Serial")
public class Indexdownloadcontroller extends basecontroller{
@ResponseBody
@RequestMapping (value = "/downword")
public void Downword (HttpServletRequest request,httpservletresponse response) throws Exception {
Response.setcharacterencoding ("Utf-8");
String name = Request.getparameter ("filename");
String name = "1.doc";
Name = Urldecoder.decode (name, "UTF-8");
if (Name.lastindexof (". htm") = = Name.length ()-4) {
Name = name.substring (0, Name.length ()-4) + ". Doc";
//}
String webpath=indexdownloadutils.getpathweb () +name;
Name = name + ". Doc";
Properties Pro = Indexdownloadutils.getproperties ("zd.properties");
String Zdpath = Pro.get ("Zdpath"). ToString ();
Zdpath = Zdpath + "yqjcword/";
Zdpath = Zdpath + "qxfw/";
String Webpath=zdpath+name;
File F=new file (Webpath);
if (!f.exists ()) {
PrintWriter Out=response.getwriter ();
Out.print (' <script>alert (' file is not exists. '); History.back ();</script> ");
}else{
String Filenamepath = name.replace ("\", "/");
String AA = filenamepath.substring (Filenamepath.lastindexof ("/") + 1,
Filenamepath.length ());
String[] Files=aa.split ("\.");
String fileName = aa.substring (0, Aa.lastindexof ("."));
String suffix = aa.substring (Aa.lastindexof ("."));
Bufferedoutputstream write = null;
Bufferedinputstream read = null;
try {
Response.setcontenttype ("Application/octet-stream");
Response.AddHeader ("Content-disposition", "attachment;filename=")
+ New String (Filename.getbytes ("gb2312"), "iso8859-1")
+ suffix);
Response.AddHeader ("Content-disposition",
"Attachment;filename=" + New String (Files[0].getbytes ("gb2312"),
"Iso8859-1")
// + "." +FILES[1]);
String Webpath = Indexdownloadutils.getpathweb ();
String Webpath = "e:/zd/qxfw/";
String Webpath = "e:/zd/yqjcword/";
File File = new file (Webpath + name);
Byte[] by = new byte[1024];
int i;
Read = new Bufferedinputstream (new FileInputStream (file));
Write = new Bufferedoutputstream (Response.getoutputstream ());
while ( -1!= (i = Read.read (by, 0, by.length)) {
Write.write (by, 0, I);
}
catch (IOException e) {
E.printstacktrace ();
throw new Exception ("The system cannot find the specified file");
finally {
try {
if (write!= null) {
Write.flush ();
Write.close ();
}
if (write!= null)
Read.close ();
catch (IOException E2) {
E2.printstacktrace ();
}
}
}
}
}
Note: The above write code, in the download time can choose any letter and folder, you can modify the filename;

bug Two: Download to C disk on the Word file is not open, error: XML illegal characters;
Solve:
Put the following line:
out = new BufferedWriter (new OutputStreamWriter (New FileOutputStream (outfile))); Set encoding
Change to the following line:

out = new BufferedWriter (new OutputStreamWriter (New FileOutputStream (outfile), "UTF-8"));



We do template export need to pay attention to the following three sets of coding set, my Chinese garbled problem is because there is no third set caused.

(1) configuration.setdefaultencoding ("UTF-8");
(2) Template t = configuration.gettemplate ("Template file", "UTF-8");
(3) Writer out = new BufferedWriter (new OutputStreamWriter (file output stream fos, "UTF-8")).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.