页面连接:
后台代码:
<a href="xxx_download.action?url=<s:property value="url" />&name=<s:property value="name" />">
后台代码:
//下载文件
public void download(){
String filePath = "";
String fileName = "";
try {
// 读取文件并且设置相关参数
if (StringUtils.isNotBlank(url)) {
filePath = request.getSession().getServletContext()
.getRealPath(url);
}
if (StringUtils.isNotBlank(name)) {
fileName = new String(name.getBytes("gbk"),
"iso8859-1");
}
File file = new File(filePath);
byte[] buf = new byte[1*1024];
int len = 0;
BufferedInputStream br = null;
OutputStream ut = null;
response.reset();// 必须加,不然保存不了临时文件
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename="
+ fileName);
br = new BufferedInputStream(new FileInputStream(file));
ut = response.getOutputStream();
while ((len = br.read(buf)) != -1) {
ut.write(buf, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
}
}