免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3098 | 回复: 2
打印 上一主题 下一主题

老手遇到新问题--有关struts2文件下载,你能解决吗? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-10-21 15:24 |只看该作者 |倒序浏览
10可用积分
现象:当用浏览器请求该action时,浏览器会提示“打开”“保存”或“取消” 。当选择”保存“时,一切正常,但是当选择“打开”时,文件名称是正确的,但是打开的内容什么都没有,一片空白,

上面催的急,恳请各位高手指点。



源代码:

public class ViewMethodFileAction extends ActionSupport {
    private String methodContentType = "text/plain;charset=gbk";
    private String filename = "content.txt";
    private String disposition = "attachment;filename=" + filename ;


    public InputStream getInputStream() {
       return  new ByteArrayInputStream("文件下载测试".getBytes());
    }

   
   public String getMethodContentType() {
       return methodContentType;
   }


   public String getDisposition(){
       return disposition;
   }


    public String execute() throws Exception {

         return SUCCESS;

    }
}


*****************************************************************

struts.xml相关配置:

<action name="viewmethod" class="telecom.china.shlr.actions.ViewMethodFileAction">
        <result name="success" type="stream">
          <param name="contentType">${methodContentType}</param>
          <param name="contentDisposition">${disposition}</param>
          <param name="inputName">inputStream</param>
          <param name="bufferSize">4096 </param>
        </result>
    </action>

论坛徽章:
0
2 [报告]
发表于 2009-10-21 17:58 |只看该作者
编码的问题吧?

论坛徽章:
0
3 [报告]
发表于 2009-10-22 08:39 |只看该作者

自己结贴,问题解决

修改如下:

public String execute() throws Exception {

        HttpServletResponse response = ServletActionContext.getResponse();
          response.reset();

         return SUCCESS;

    }

就是复位response.

另外:response头Content-Type后的charset一定要写,否则不能正常下载。
如:Content-Type:text/plain;charset=gbk

[ 本帖最后由 xztelecomlcs 于 2009-10-22 08:40 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP