下载时获取空文件:jsf

时间:2014-05-22 10:46:18

标签: file jsf download javabeans

我想下载存储在系统目录中的文件,但是当在jsf页面下载文件时,我将其清空。 这是我的xhtml页面:

<h:form>
   <h:commandButton value="Download" action="#{helloBean.downloadFile}" />
</h:form>

和我的托管bean:

@ManagedBean
@SessionScoped
public class HelloBean {
    public void downloadFile() {

        File file = new File("C:\\data\\contacts.doc");
        HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();  

        response.setHeader("Content-Disposition", "attachment;filename=contacts.doc");  
        response.setContentLength((int) file.length());  
        ServletOutputStream out = null;  
        try {  
            FileInputStream input = new FileInputStream(file);  
            byte[] buffer = new byte[1024];  
            out = response.getOutputStream();  
            int i = 0;  
            while ((i = input.read(buffer)) != -1) {  
                out.write(buffer);  
                out.flush();  
            }  
            FacesContext.getCurrentInstance().getResponseComplete();  
        } catch (IOException err) {  
            err.printStackTrace();  
        } finally {  
            try {  
                if (out != null) {  
                    out.close();  
                }  
            } catch (IOException err) {  
                err.printStackTrace();  
            }  
        }  

    }
}

那怎么解决呢?提前谢谢。

1 个答案:

答案 0 :(得分:0)

我认为此代码可以解决您的问题。

public void download() throws IOException {  
            FacesContext context = FacesContext.getCurrentInstance();  
            HttpServletResponse response = (HttpServletResponse) context  
                      .getExternalContext().getResponse();  
            File file = new File(filePath);  
            if (!file.exists()) {  
                 response.sendError(HttpServletResponse.SC_NOT_FOUND);  
                 return;  
            }  
            response.reset();  
            response.setBufferSize(DEFAULT_BUFFER_SIZE);  
            response.setContentType("application/octet-stream");  
            response.setHeader("Content-Length", String.valueOf(file.length()));  
            response.setHeader("Content-Disposition", "attachment;filename=\""  
                      + file.getName() + "\"");  
            BufferedInputStream input = null;  
            BufferedOutputStream output = null;  
            try {  
                 input = new BufferedInputStream(new FileInputStream(file),  
                           DEFAULT_BUFFER_SIZE);  
                 output = new BufferedOutputStream(response.getOutputStream(),  
                           DEFAULT_BUFFER_SIZE);  
                 byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];  
                 int length;  
                 while ((length = input.read(buffer)) > 0) {  
                      output.write(buffer, 0, length);  
                 }  
            } finally {  
                 input.close();  
                 output.close();  
            }  
            context.responseComplete();  
       }