webwork实现下载服务器上工程目录外的硬盘文件

zhkj21589 2011-03-18 08:54:20
我用的是spring+hibernate+webwork,我现在想做个功能实现下载文件,要下载的文件存在服务器的另外一个硬盘上,高手给支点招,谢谢。。。。
...全文
111 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
游一游走一走 2011-03-18
  • 打赏
  • 举报
回复
类似于这样的代码吧
public String download() {

ServletActionContext.getResponse().setContentType(
"application/octet-stream");
try {
ServletActionContext.getResponse().setHeader(
"Content-Disposition",
"attachment;filename=downloadfile.txt");
InputStream is = new FileInputStream("d:\\下载文件.txt");
try {
byte[] bs = new byte[10240];
int i = 0;
while ((i = is.read(bs)) != -1) {
ServletActionContext.getResponse().getOutputStream().write(
bs, 0, i);
}
} finally {
is.close();
}
} catch (Exception e) {
log.error("HTML页面输出下载文件出错", e);
}

return null;
}
zhkj21589 2011-03-18
  • 打赏
  • 举报
回复
怎么米有人支点招。。。。。
智鹿软件 2011-03-18
  • 打赏
  • 举报
回复
那就绝对路径啊。
游一游走一走 2011-03-18
  • 打赏
  • 举报
回复
将上面的代码修改下:
ServletActionContext.getResponse().setHeader(
"Content-Disposition",
"attachment;filename="
+ Utils.getAttachment("中文下载名.txt",
ServletActionContext.getRequest()
.getHeader("User-Agent")));
新增如下方法:
public static String getAttachment(String fileName, String userAgent) {
if (userAgent == null)
return fileName;
userAgent = userAgent.toLowerCase();
try {
if (userAgent.indexOf("msie") != -1) {
return URLEncoder.encode(fileName, "utf-8");
} else {
return new String(fileName.getBytes(), "iso-8859-1");
}
} catch (UnsupportedEncodingException e) {
log.error("下载文件名中文处理时出错", e);
}
return fileName;
}
zhkj21589 2011-03-18
  • 打赏
  • 举报
回复
谢谢楼上的,我那些文件有些中文乱码有米有啥好方法处理?O(∩_∩)O

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧