ext 右键菜单部分:
{
id:'expversion',
text:'导出版本',
handler:function(){
var _url='fileDown.action';
if(_docvId!=null){
_url+='?docvId='+_docvId;
}else if(_docId!=null){
_url+='?docId='+_docId;
}
window.open(_url,'_self','width=1,height=1,toolbar=no,menubar=no,location=no'); //注意这里 用window.open() 否则不出现下载对话框
}}]
}
struts2 Action:
private String downFileName;
public String execute() throws Exception {
File docvfile = fileService.exportFile(docvId, docId);
if (docvfile == null) {
return null;
}
setDownFileName(docvfile.getName() + "." + docvfile.getSuffix());
String path = docvfile.getPath();
setFilePath(path.substring(path
.lastIndexOf(savePath.replace("/", "//"))));
// 文件下载目录路径
String downloadDir = ServletActionContext.getServletContext()
.getRealPath(savePath);
// 文件下载路径
java.io.File file = new java.io.File(path);
path = file.getCanonicalPath();// 真实文件路径,去掉里面的..等信息
// 发现企图下载不在 /download 下的文件, 就显示空内容
if (!path.startsWith(downloadDir)) {
return null;
}
return SUCCESS;
}
public InputStream getInputStream() throws Exception {
return ServletActionContext.getServletContext().getResourceAsStream(
filePath);
}
public String getDownFileName() {//输出文件名 转码 中文 显示 问题
try {
return new String(downFileName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
return downFileName;
}
}
public void setDownFileName(String downFileName) {
this.downFileName = downFileName;
}
struts.xml:
<struts>
<package name="fileDown" extends="struts-default">
<default-action-ref name="download"/>
<action name="fileDown" class="fileDownAction">
<param name="savePath">/docUpload</param>
<result type="stream">
<param name="contentType">application/octet-stream;charset=ISO8859-1</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">
attachment;filename=${downFileName}//下载时使用的文件名 与action中 getDownFileName()对应
</param>
<param name="bufferSize">4096</param>
</result>
</action>
</package>
</struts>