1

i made a call to download() method to save json into xml with extension ".svg". The jsondata is global variable store 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();

}

and the exportFile(jsondata) is

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;
    }

finally to write this one file 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);
    }
}

i can see the xml in console but what am doing wrong that its not downloading?? please help me. thanks in advance.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
karan
  • 11
  • 2

1 Answers1

0

i got the mistake. it was not in above code. if we make through commandLink then it won't work but if make call through commandButton then it worked. if you want know know more read difference between commandButton vs commandLink

karan
  • 11
  • 2