JAVA利用FreeMarker生成(导出)Word文档(Bug修改)

来源:互联网 发布:北师大网络教育会计学 编辑:程序博客网 时间:2024/06/03 21:39
Bug一:把代码放到服务器上,能下载Word文件;但是,下载到服务器的C盘上了;
 解决:
 修改部分export.js代码:
 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);
}
在com.yuanls.index.controller.IndexDownloadController.java 中:(部分代码:)
@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("系统找不到指定的文件");
} finally {
try {
if (write != null) {
write.flush();
write.close();
}
if (write != null)
read.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
}
}
注:以上这样写代码,在下载的时候可以选择任意盘符和文件夹,还可以修改文件名;
 
 Bug二:下载到C盘上的Word文件打不开;报错:XML 非法字符;
 解决:
把下面这一行: 
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile))); 设置编码
改成下面这一行:

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



我们在做模板导出时需要注意以下三处编码集的设置,我的中文乱码问题是因为第三处没有设置引起的。

(1)configuration.setDefaultEncoding("UTF-8");
(2)Template t = configuration.getTemplate("模板文件","UTF-8");
(3)Writer out = new BufferedWriter(new OutputStreamWriter(文件输出流 fos, "UTF-8"))。


1 0