Download-Datei mit JSF

alle!

Habe ich ein Problem. Ich versuchte zu retten, excel-Datei in jsf-web-Anwendung.
Ich generierte Datei von meinem utils und versuchen, zu "speichern" - Fenster, aber ich konnte nicht.

Hier ist mein code:

  <div>
  <h:commandButton value="Apply" actionListener="#{hornPhonesBean.generateReport}"/>
  </div>

und:

   public void generateReport(ActionEvent event) {
    System.out.println("GENERATE REPORT FROM = " + this.dateFrom + "; TO = " + this.dateTo);

    try {
        XSSFWorkbook workbook = (XSSFWorkbook) HornReportGenerator.getWorkbook(null, null);
        String fileName = "1.xlsx";

        FacesContext fc = FacesContext.getCurrentInstance();
        ExternalContext ec = fc.getExternalContext();

        //Some JSF component library or some Filter might have set some headers in the buffer beforehand. We want to get rid of them, else it may collide.
        ec.responseReset(); 

        //Check http://www.w3schools.com/media/media_mimeref.asp for all types. Use if necessary ExternalContext#getMimeType() for auto-detection based on filename.
        ec.setResponseContentType("application/vnd.ms-excel"); 

        //Set it with the file size. This header is optional. It will work if it's omitted, but the download progress will be unknown.
        //ec.setResponseContentLength(contentLength); 

        //The Save As popup magic is done here. You can give it any file name you want, this only won't work in MSIE, it will use current request URL as file name instead.
        ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); 

        OutputStream output = ec.getResponseOutputStream();
        workbook.write(output); 
        output.flush();
        output.close();

        fc.responseComplete(); //Important! Otherwise JSF will attempt to render the response which obviously will fail since it's already written with a file and closed.
        System.out.println("END");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Lese ich Vorschläge hier und aus anderen Foren jeder sagt, ich sollte nicht verwenden , aber ich habe nicht verwenden, es überhaupt nicht.

Dann dachte ich, dass das problem sein könnte, in der

 <ice:form>, 

wo ich hielt die

 <h:commandButton>, 

und ich wechselte zu

 <h:form>, 

aber es hat nicht geholfen.

Vielleicht ist das problem in der Anfrage -, es hat Kopf-Faces-Request-partial/ajax. Aber ich bin mir nicht sicher.

Bitte geben Sie mir einige Ideen, die ich bereits seit 4 Stunden für diese tolle jsf-download-Problem)

InformationsquelleAutor Elena | 2012-11-20

Schreibe einen Kommentar