Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1187135
  • 博文数量: 89
  • 博客积分: 10546
  • 博客等级: 上将
  • 技术积分: 1510
  • 用 户 组: 普通用户
  • 注册时间: 2004-10-16 01:24
文章分类

全部博文(89)

文章存档

2012年(7)

2011年(4)

2010年(5)

2009年(52)

2008年(21)

分类: Java

2009-03-24 22:26:44

在前面的章节中已经认识了 Resolution 接口,这一章将使用StreamingResolution 实现文件下载。

创建一个 ActionBean

public class DownloadActionBean extends BaseActionBean {

@Validate(required=true)
private String filename;

public String getFilename() {
return filename;
}

public void setFilename(String filename) {
this.filename = filename;
}

public Resolution download() {
return new StreamingResolution("application/octet-stream", buildDowloadSource()).setFilename(filename);
}

public InputStream buildDowloadSource() {
String rootPath = getContext().getServletContext().getRealPath("/");
String filePath = rootPath + "/public/Fedora.png";
try {
return new FileInputStream(filePath);
} catch (FileNotFoundException ex) {
getContext().getValidationErrors().addGlobalError(new SimpleError("Can not found file!"));
}
return null;
}
}

StreamingResolution 设置了filename 属性时,会自动弹出下载文件确认框。

创建下载页面,提供下载链接。

<%@page contentType="text/html" pageEncoding="UTF-8"%>
"">
<%@taglib prefix="stripes" uri="" %>



Download File Page


Download File!



Fedora BackGround:








这里将一张图片放入项目程序的根目录下的 /public下。运行程序进行测试。

阅读(1896) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~