@Action("export")
public void exportWord(){
plan = service.showInfo(plan);
File file = null;
file = service.createWord(plan,imgBase64);[color=orange]
SimpleDateFormat format2=new SimpleDateFormat("yyyy-MM-dd");
String a5 = format2.format(new Date());
fileName = plan.getRp_name() + a5 +".doc";
String agent = request.getHeader("USER-AGENT");
if(null != agent && -1 != agent.indexOf("MSIE")){
fileName = URLEncoder.encode(fileName,"UTF-8");
}else if(null != agent && -1 != agent.indexOf("Mozilla")){
fileName = new String(fileName.getBytes("UTF-8"),"iso-8859-1");
}
if (file.exists()) {
this.inputStream = new FileInputStream(file);
} else {
m_StringResult = "导出失败";
return;
}
response.reset();
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
int fileLength = (int) file.length();
response.setContentLength(fileLength);
/*如果文件长度大于0*/
if (fileLength != 0) {
/*创建输入流*/
byte[] buf = new byte[4096];
/*创建输出流*/
ServletOutputStream servletOS = response.getOutputStream();
int readLength;
while (((readLength = this.inputStream.read(buf)) != -1)) {
servletOS.write(buf, 0, readLength);
}
this.inputStream.close();
servletOS.flush();
servletOS.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
public void exportWord(){
plan = service.showInfo(plan);
File file = null;
file = service.createWord(plan,imgBase64);[color=orange]
SimpleDateFormat format2=new SimpleDateFormat("yyyy-MM-dd");
String a5 = format2.format(new Date());
fileName = plan.getRp_name() + a5 +".doc";
String agent = request.getHeader("USER-AGENT");
if(null != agent && -1 != agent.indexOf("MSIE")){
fileName = URLEncoder.encode(fileName,"UTF-8");
}else if(null != agent && -1 != agent.indexOf("Mozilla")){
fileName = new String(fileName.getBytes("UTF-8"),"iso-8859-1");
}
if (file.exists()) {
this.inputStream = new FileInputStream(file);
} else {
m_StringResult = "导出失败";
return;
}
response.reset();
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
int fileLength = (int) file.length();
response.setContentLength(fileLength);
/*如果文件长度大于0*/
if (fileLength != 0) {
/*创建输入流*/
byte[] buf = new byte[4096];
/*创建输出流*/
ServletOutputStream servletOS = response.getOutputStream();
int readLength;
while (((readLength = this.inputStream.read(buf)) != -1)) {
servletOS.write(buf, 0, readLength);
}
this.inputStream.close();
servletOS.flush();
servletOS.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
本文介绍了一个使用特定服务将计划信息转换为Word文档,并通过日期和请求头参数自动生成文件名,实现自动下载的过程。重点在于处理文件路径、编码、存在性检查以及内容流读取和响应输出。
1万+

被折叠的 条评论
为什么被折叠?



