`
yinbinhome
  • 浏览: 120792 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

webwork下载

阅读更多
WEBWORK的文件下载机制。使用起来还是比较简单的。
下面是用法说明:
首先在一个ACTION中,如果判断有权限进行文件下载。
则:
1、读出该下载文件,并生成一个流。 文件名应当从请求的request中读出,或从用户的表中取出。
public String downLoadFile(String fileName){
try {
File input = new File("e:/engilish literature.doc");
docStream = new FileInputStream(input);
contentDisposition = "test.txt";
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "download";
}
2、将输出导向到一个特殊的RESULT中去。叫做Steam Result。
<action name="register" class="com.job2easy.web.user.RegisterAction">
<result name="success" type="dispatcher">
<param name="location">/home/register-result.jsp</param>
</result>
<result name="input">
<param name="location">/home/register.jsp</param>
</result>
<result name="download" type="stream">
<param name="contentType">application/x-msdownload</param>
<param name="inputName">docStream</param>
<param name="bufferSize">1024</param>
<param name="contentDisposition">attachment;filename="${contentDisposition}"</param>
</result>

<interceptor-ref name="params"/>
</action>
3、这中间有几个参数需要配置:
contentType设成 application/x-msdownload 就可以。这样浏览器会保证弹出一个下载文件的对话框。
inputName 这个比较重要,这个名字是输入流的名称, 以后要steam result的实现类中为根据OGNL的表达式去查找的。
contentDisposition 这个是下载之后,保存在用户端的文件名称。${contentDisposition} 看一下代码。如果写成上述的方式,就有机会在ACTION中设置文件名。
4、另外一个参数:contentLength就是下载文件的大小,webwork的stream result似乎实现有问题,不能根据文件的大小动态进行设置,只能写死。
这个参数的意义是告诉浏览下载的文件有多大,以便浏览器正确的显示进度条。如果这个功能很重要的话,可以重新写一个RESULT来实现。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics