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))); 设置编码
改成下面这一行:
解决:
修改部分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
- JAVA利用FreeMarker生成(导出)Word文档(Bug修改)
- JAVA利用FreeMarker生成(导出)Word文档
- Java 中利用 Freemarker 生成 Word 文档
- Java生成word文档(freemarker)
- freemarker利用word模版导出word文档
- java利用freemarker生成简单格式的word文档
- JAVA利用FreeMarker生成(导出)Excel表格
- 利用freemarker生成word(带图片)
- freemarkerExportWord.java freemarker+xml导出Word文档
- Java用freemarker导出word文档
- Java使用freemarker导出word文档
- java中使用freemarker导出word文档
- freemarker导出word文档
- freemarker导出word文档
- Java之利用FreeMarker导出Word实例
- 【java】模板freemarker实现word生成导出
- POI应用:利用word模板批量生成word文档(java中word文档的读写)
- java生成word 文档学习过程--Freemarker
- java中的自动拆箱、装箱是指什么?
- 10 静态公有成员与C++单例模式
- Linux系统中下载文件的wget命令操作实例
- 启动maven项目时Address already in use: JVM_Bind<null>:8080错误的解决办法
- SQL Server2008启动报错问题
- JAVA利用FreeMarker生成(导出)Word文档(Bug修改)
- 3.3.4_绘制坐标系旁边的文本标签
- 删除cocoapods中的一些repo选项:pod repo remove xxx
- 系统分区
- Webrtc服务器搭建
- dtb to dts by dtc
- 服务器端渲染和客户端渲染有什么区别?
- 第八届山东省ACM省赛比赛感想
- 服务机器人常用的定位导航技术及优缺点分析