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

新手求教!!关于使用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条回答 默认 最新

  • threenewbee 2015-07-22 11:02
    关注

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

    评论

报告相同问题?