struts2的下载功能实现流程包括三个方面,jsp页面,xml配置文件,Action类。
首先我前台页面效果如下:
发送请求通过form表单的形式,文件名称及文件类型存储在hidden类型的input标签里,“下载文档”超链接也包含在form表单之中,点击超链接在downFile方法中提交表单请求。
代码:
<form id="${guide.bhc101}" action="ggcx/download.html?currentPage=${currentPage}" method="post">
<input type="hidden" name='fileName' value='${guide.bhc102}'/>
<input type="hidden" name='type' value="application/msword"/>
<a style="cursor: pointer;color: red;" οnclick="javascript:queryGuide('${guide.bhc101}')">查看</a>
<a style="cursor: pointer;color: red;" οnclick="downloadFile('${guide.bhc101}')">下载文档</a>
</form>
因为项目中文章对应的下载文档命名格式是标题.doc或者标题.docx的word文档,所以fileName的value值保存的是文章的标题,type的value值则是"application/msword",这是doc格式在程序的表示字符串。
列举一下主流格式文件在程序中对应的字符串:
到了这里,有一点需要说明一下,其实单纯使用a标签发送请求也是可以实现的,但是如果a标签的href属性中带有中文那么后台接收会产生中文乱码,所以在这里我使用form表单的形式来发送请求,保证后台 能接收到正确的中文。如果有更好的方法也请和我说一下,谢谢!
接下来,就要处理struts2的配置文件:
<action name="download" class="admin.action.DownLoadAction" method="checkFile">
<!--设置找不到文件情况的请求转发-->
<result name="fileNotFound" type="chain">
<param name="actionName">jgcspublic</param>
<param name="namespace">/ggcx</param>
</result>
<!--设置响应类型为stream-->
<result name="success" type="stream">
<param name="contentType">${type}</param>
<!--设置下载文件的名字 ,attachment表示弹出对话框提示下载-->
<param name="contentDisposition">attachment;filename=${fileName}</param>
<!--设置Action中输出流的函数-->
<param name="inputName">guideFile</param>
</result>
</action>
"fileNotFound"的result 表示找不到文件的处理方法,通过chain模式转发ggcx包下的jgcspublic请求。
"success"的result则表示执行下载文件的方法。
contentType:文件的类型
contentDirposition:文件名称
inputName:返回输出流的方法名,则在Action中方法名命名应是getGuideFile()
发送请求后的第一步会调用checkFile方法,来检查标题.doc的文件是否存在,如果不存在会再检查标题.docx的文件是否存在。
如果存在返回"success",然后就是自动转到inputName属性指定的方法即是将下载文件转成Stream流回给页面的方法。
如果不存在文件,则转发jgcspublic请求,这是查询列表的请求,因为在前台form表单请求时我已经带上了当前页的值,通过chain的方式能保留传递的参数 ,于是当jgcspublic 请求完成时还能回到点击下载时的页面。
DownLoadAction类的代码:
public class DownLoadAction extends ActionSupport {
private String fileName;
private String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getFileName() {
try{
ServletActionContext.getResponse().setHeader("charset","ISO8859-1");
return new String(this.fileName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
return "获取文件名出现了错误!";
}
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String checkFile(){
String path = ServletActionContext.getServletContext().getRealPath("/download/guide/"+fileName + ".doc");
File file = new File(path);
if(file.exists()){
return SUCCESS;
}
else{
type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
path = path.substring(0, path.length()-4) + ".docx";
file = new File(path);
if(file.exists()){
return SUCCESS;
}
}
return "fileNotFound";
}
public InputStream getGuideFile(){
if(type.equals("application/msword")){
return ServletActionContext.getServletContext().getResourceAsStream("/download/guide/"+ fileName + ".doc");
}
else{
return ServletActionContext.getServletContext().getResourceAsStream("/download/guide/"+ fileName + ".docx");
}
}
}
需要注意的是在getFileName()方法还要进行一定的处理,必须要修改响应的编码格式,以保证文件名回到页面时还是正确中文 ,不然还会是一串乱码的哦。
正确的下载页面:
其实实现struts2的下载功能并不难,关键在于保证中文从前台传到后台 和从后台传到前台的正确性,以及找不到文件情况下的请求转发。整个流程保持合理的逻辑处理就行,希望这篇文章对学习struts2下载的朋友有所帮助,分享互勉!