首先在一个ACTION中,如果判断有权限进行文件下载。
1、读出该下载文件,并生成一个流。这是WebWork文件下载的***步,文件名应当从请求的request中读出,或从用户的表中取出。
public String downLoadFile(String fileName)
{
try {
File input = new File("e:/engilish
literature.doc");
docStream = new FileInputStream(input);
contentDisposition = "filename=\"test.txt\"";
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "download";
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
2、将输出导向到一个特殊的RESULT中去。在WebWork文件下载中叫做Steam Result。
<action name="register" class=
"com.job2easy.web.user.RegisterAction">
name="success" type="dispatcher">
name="location">/home
/register-result.jsp
name="input">
name="location">/home/register.jsp
name="download" type="stream">
name="contentType">application
/x-msdownload
name="inputName">docStream
name="bufferSize">1024
name="contentDisposition">$
{contentDisposition}
name="params"/>
action>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
3、WebWork文件下载参数。这中间有几个参数需要配置:
◆contentType设成 application/x-msdownload 就可以。这样浏览器会保证弹出一个下载文件的对话框。
◆inputName 这个比较重要,这个名字是输入流的名称, 以后要steam result的实现类中为根据OGNL的表达式去查找的。
◆contentDisposition 这个是下载之后,保存在用户端的文件名称。
◆${contentDisposition} 看一下代码。如果写成上述的方式,就有机会在ACTION中设置文件名。
4、另外一个参数:contentLength就是下载文件的大小,webwork的stream result似乎实现有问题,不能根据文件的大小动态进行设置,只能写死。
这个参数的意义是告诉浏览下载的文件有多大,以便浏览器正确的显示进度条。如果这个功能很重要的话,可以重新写一个RESULT来实现。
【编辑推荐】