Dateidownload mit jsf

Status
Nicht offen für weitere Antworten.
H

Hanniball

Gast
Hallo,

habe ein Problem das ich keine ahnung habe wie ich einen Dateidownload (XML dateien) für eine JSF-Seite realisieren kann. Der Server ist ein Tomcat auf dem die Daten liegen. Ich bin mit folgendem Code soweit gekommen das er mir das Download fenster offnet aber die HTML seite Downloaden will

die Bean:
Code:
public void downloadLink(ActionEvent e){
		FacesContext context = FacesContext.getCurrentInstance();
		HttpServletResponse response = (HttpServletResponse)context.getExternalContext(). getResponse();
		 try {

				   String filename = "xxx.xml";
				   response.setContentType("APPLICATION/OCTET-STREAM");
				   String disHeader = "Attachment";
				   response.setHeader("Content-Disposition", disHeader);
				   File fileToDownload = new File(filename);
				   FileInputStream fileInputStream = new
				      FileInputStream(fileToDownload);
				   OutputStream out=null; 
				   int i;
				   while ((i=fileInputStream.read())!=-1)
				   {
				      out.write(i);
				   }
				   fileInputStream.close();
				   out.close();
				   }catch(Exception f)
				   {
				   f.printStackTrace();
				}
			}

die JSF-Seite:
Code:
<h:panelGrid>
	<h:outputText value="Name" style="color: black; font-size: 13px;"/>
			
	<h:commandLink value="#{config_handler.name}" style="text-decoration: none;"
	actionListener="#{config_handler.antwortOfFileLink}" action="download" />

</h:panelGrid>

kann mi vieleicht jemand helfen oder hat ne Idee wie ich mei Prob lösen könnte

Hanniball
 
G

Guest

Gast
so doch noch selber hin bekommen

hier wie es geht in der Bean:

Code:
public void downloadLink(ActionEvent e){
		FacesContext context = FacesContext.getCurrentInstance();
		HttpServletResponse response = (HttpServletResponse)context.getExternalContext(). getResponse();
		int BUFFER_SIZE = 16384;
		File file = new File("xxx.xml");
		StringBuilder type = new StringBuilder("attachment; filename=");
		type.append(file.getName());
		response.setContentLength((int) file.length());
		response.setContentType("application/octet-stream");
		response.setHeader("Content-Disposition", type.toString());

		try {
			OutputStream os = response.getOutputStream();
			FileInputStream fis = new FileInputStream(file);
			byte[] buffer = new byte[BUFFER_SIZE];
			int bytesRead = 0;
			while ((bytesRead = fis.read(buffer)) > 0) {
				os.write(buffer, 0, bytesRead);
			}
			os.flush();
			fis.close();
		} catch (FileNotFoundException e1) {
		
			e1.printStackTrace();
		} catch (IOException e1) {

			e1.printStackTrace();
		}
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Oben