前端页面代码(请求Action),fn_downLoad方法的参数fileName是传给Action的需要卸载文件的名字:
// 下载文件方法
function fn_downLoad(fileName) {
Ext.Ajax.request({
url : "downLoadFile.action",
params : {
delFileName : fileName
},
method : "POST",
timeout : 4000,
success : function(response, opts) {
// Action放回文件流对象在response对象里
// 请问代码如何实现下载
// var obj = Ext.decode(response.responseText);
// window.location.href = obj.path;
}
});
}
==============================================
后台Action代码
/**
*
* 获取文件流对象
* @return
* @throws Exception
*/
public InputStream getInputStream() throws Exception {
// 通过 ServletContext,也就是application 来读取数据
return getServletContext().getResourceAsStream(inputPath);
}
/**
* 双击下载文件
*
* @return
* @throws IOException
* @throws IOException
* @throws IOException
*/
public String downLoadFile() throws IOException {
this.inputPath = "/upload/" + "Dota英雄出装攻略_(超全版).doc";
this.fileName = "Dota英雄出装攻略_(超全版).doc";
// this.inputPath = "/upload/" + delFileName;
// this.fileName = delFileName;
// 文件下载目录路径
String downloadDir = getServletContext().getRealPath("/upload");
// 文件下载路径
String downloadFile = getServletContext().getRealPath(inputPath);
java.io.File file = new java.io.File(downloadFile);
downloadFile = file.getCanonicalPath();// 真实文件路径,去掉里面的..等信息
// 安全性
if (!downloadFile.startsWith(downloadDir)) {
return null;
}
return SUCCESS;
}
/**
* 提供转换编码后的供下载用的文件名
*
* @return
*/
public String getDownloadFileName() {
String downFileName = fileName;
try {
downFileName = new String(downFileName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return downFileName;
}
=====================================================
Struts.xml文件下载配置
<action name="downLoadFile" class="com.oa.action.FileAction"
method="downLoadFile">
<result name="success" type="stream">
<param name="contentType">
application/octet-stream;charset=ISO8859-1
</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">
attachment;filename="${downloadFileName}"
</param>
<param name="bufferSize">4096</param>