【问题标题】:why downloading to file is not working in jsf? [duplicate]为什么下载到文件在 jsf 中不起作用? [复制]
【发布时间】:2017-12-18 23:22:56
【问题描述】:

我调用了 download() 方法将 json 保存到扩展名为“.svg”的 xml 中。 jsondata是全局变量存储json。

public void download(){
File file = exportFile(jsondata);
         HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();

     writeOutContent(response, file, file.getName());
     FacesContext.getCurrentInstance().responseComplete();
     FacesContext.getCurrentInstance().renderResponse();

}

exportFile(jsondata) 是

public File exportFile(String jsonData){


File xmlFile = null;
        try {
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.newDocument();
            JSONObject jsonObject = new JSONObject(jsonData);

            Element root = doc.createElement("web");
            doc.appendChild(root);

            Element rootElement1 = doc.createElement("class");
            rootElement1.appendChild(doc.createTextNode(jsonObject.getString("class")));
            root.appendChild(rootElement1);

            JSONArray jsonArray1 = (JSONArray) jsonObject.get("nodes");
            Element rootElement2 = doc.createElement("nodes");
            root.appendChild(rootElement2);
            for (int i = 0; i < jsonArray1.length(); i++) {
                Element staff = doc.createElement("node");
                rootElement2.appendChild(staff);
                JSONObject childObject = (JSONObject) jsonArray1.get(i);
                Iterator<String> keyItr = childObject.keys();
                while (keyItr.hasNext()) {
                    String key = keyItr.next();
                    Element property = doc.createElement(key);
                    property.appendChild(doc.createTextNode(childObject.getString(key)));
                    staff.appendChild(property);
                }
            }
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            //for pretty print
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            DOMSource source = new DOMSource(doc);

            xmlFile = new File("file.svg");
            //write to console or file
//            StreamResult console = new StreamResult(System.out);
            StreamResult file = new StreamResult(xmlFile);

            //write data
//            transformer.transform(source, console);
            transformer.transform(source, file);
        } catch (Exception pce) {
            pce.printStackTrace();
        }
        return xmlFile;
    }

终于写到了这个文件的writeOutContent()

public void writeOutContent(final HttpServletResponse res, final File content, final String theFilename) {
    if (content == null) {
        System.out.println("content is null");
        return;
    }
    try {
        res.setHeader("Content-Disposition", "attachment; filename=\"" + theFilename + "\"");
        System.out.println("res " + res.getHeader("attachment; filename=\"" + theFilename + "\""));
        res.setContentType("application/octet-stream");
        FileInputStream fis = new FileInputStream(content);
        OutputStream os = res.getOutputStream();
        int bt = fis.read();
        while (bt != -1) {
            os.write(bt);
            bt = fis.read();
        }
        os.flush();
        fis.close();
        os.close();
    } catch (Exception ex) {
        Logger.getLogger(DownloadFile.class.getName()).log(Level.SEVERE, null, ex);
    }
}

我可以在控制台中看到 xml,但是它没有下载有什么问题?请帮我。 提前致谢。

【问题讨论】:

    标签: jsf download richfaces


    【解决方案1】:

    我弄错了。它不在上面的代码中。如果我们通过 commandLink 进行调用,那么它将无法工作,但如果通过 commandButton 进行调用,则它可以工作。如果您想了解更多,请阅读difference between commandButton vs commandLink

    【讨论】:

      猜你喜欢
      • 2011-03-26
      • 2013-02-09
      • 2015-11-08
      • 2018-08-03
      • 2012-02-08
      • 1970-01-01
      • 2012-12-24
      • 2016-07-15
      • 1970-01-01
      相关资源
      最近更新 更多