lagbr
lagbr
采纳率0%
2015-07-22 08:46

新手求教!!关于使用struts 下载csv文件的问题 急

我配置好struts后,点击下载的时候页面没有任何的响应,这是为什么啊?
struts.xml

  <action name="xxDownload" class="com.xx.action.xxAction"
            method="download" >
            <result name="success" type="stream">  
                <param name="contentType" >application/csv</param > 
                <param name="inputName" >stream</param> 
                <param name="contentDisposition" >
                    attachment;filename="${fileName}"
                </param>  
                <param name="bufferSize" >4096</param> 
            </result>

xxAction

 private InputStream stream;
String fileName;

public String download(){
    try {
        xx= service.search(condition);
        fileName = "xx_"+Math.random()+".csv";
        File file = new File(fileName);
        OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
        String str = ".........shenglve\n";
        out.write(str.getBytes());
        out.flush();
        for (int i = 0; i < x.size(); i++) {
            str = xx.get(i).getStoreCD() + ","
                + xx.get(i).getProductCD() + ","
                + xx.get(i).getStartDate() + ","
                + xx.get(i).getEndDate() + ","
                + xx.get(i).getMassExposure() + ",aa\n";
            out.write(str.getBytes());
            out.flush();
        }
        out.flush();
        stream = new FileInputStream(file);  
    } catch (Exception e) {
        logger.error(e.getMessage(),e);
        exFlag = "1";
    }
    return "success";
}

js

 var msg ="是否下载?";
        if(window.confirm(msg)){
            var url = "xxDownload";
            var param = {
                "xx.a":"1"
                ,"xx.b":"2"
                ,"xx.c":"3"
                ,"xx.d":""
            };
            $.post(url,param);
        }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • cuiwei1026522829 另一花生 6年前

    下载的时候,别用这种方式,我用的from表单,提交的,但是如果你下载,包括下载报表之类的,下不了,这是异步传输的

    点赞 评论 复制链接分享
  • caozhy 回答这么多问题就耍赖把我的积分一笔勾销了 6年前

    post不会引起跳转,也不会触发下载,需要用window.location=xxx跳转引发下载。

    点赞 评论 复制链接分享