Setting file name for a downloaded file
I've set up a file download using code I've gleaned from jsf forums. However, the file name is not being set correctly. When the user downloads File.doc, it shows up as downloadPage.jsf. Am I setting the header correctly?
publicclass VendorInfoBeanextends AbstractViewController{
public VendorInfoBean(){
}
public String downloadVendorApp(){
ExternalContext context = getExternalContext();
String path = context.getInitParameter("externalFiles");
String filename ="Vendor_Application_e_Form.doc";
String fullFileName = path + filename;
File file =new File(fullFileName);
HttpServletResponse response = (HttpServletResponse) context.getResponse();
response.setHeader("Content-Disposition","attachment;filename=\"" + filename +"\"");
response.setContentLength((int) file.length());
response.setContentType("application/msword");
try{
FileInputStream in =new FileInputStream(file);
OutputStream out = response.getOutputStream();
// Copy the contents of the file to the output stream
byte[] buf =newbyte[1024];
int count;
while ((count = in.read(buf)) >= 0){
out.write(buf, 0, count);
}
in.close();
//out.write(bytes);
out.flush();
out.close();
FacesContext.getCurrentInstance().responseComplete();
}catch (IOException ex){
ex.printStackTrace();
}
returnnull;
}
}

