java - 下载的文件大小 0

标签 java jsp jsf servlets alfresco

我在这里尝试添加一个特定的对话框 bean,以便在 Alfresco Explorer 上执行操作,该对话框应该下载特定的 docx 文件。当我点击下载操作时,代码工作正常,它会下载文件,但正如我的问题标题中提到的,文件大小为 0 字节。

我用这个来做到这一点:

public class NewFormDialog extends BaseDialogBean {

protected String aspect;

protected String finishImpl(FacesContext context, String outcome)
        throws Exception {

    download(aspect);

    // // get the space the action will apply to
    // NodeRef nodeRef = this.browseBean.getActionSpace().getNodeRef();
    //
    // // resolve the fully qualified aspect name
    // QName aspectToAdd = Repository.resolveToQName(this.aspect);
    //
    // // add the aspect to the space
    // getNodeService().addAspect(nodeRef, aspectToAdd, null);
    //
    // // return the default outcome
    return outcome;
}

public boolean getFinishButtonDisabled() {
    return false;
}

public String getFinishButtonLabel() {
    return "Download";
}

public void download(String pAspect) throws ServletException, IOException {

    String filename = pAspect;
    String filepath = "\\";
    BufferedInputStream buf = null;
    ServletOutputStream myOut = null;

    try {
        FacesContext fc = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) fc
                .getExternalContext().getResponse();

        myOut = response.getOutputStream();
        File myfile = new File(filepath + filename);

        // set response headers
        response.setContentType("application/octet-stream");

        response.addHeader("Content-Disposition", "attachment; filename="
                + filename);

        response.setContentLength((int) myfile.length());

        FileInputStream input = new FileInputStream(myfile);
        buf = new BufferedInputStream(input);
        int readBytes = 0;

        // read from the file; write to the ServletOutputStream
        while ((readBytes = buf.read()) != -1)
            myOut.write(readBytes);
        myOut.flush();
        response.flushBuffer();

    } catch (IOException ioe) {

        throw new ServletException(ioe.getMessage());

    } finally {
        // close the input/output streams
        if (myOut != null)
            myOut.close();
        if (buf != null)
            buf.close();
        FacesContext.getCurrentInstance().responseComplete();
    }
}

public String getAspect() {
    return aspect;
}

public void setAspect(String aspect) {
    this.aspect = aspect;
}
}

我尝试了所有我发现的解决方案,但没有一个有效。

提前谢谢您。

最佳答案

如果文件不存在,File.length() 方法将返回 0。检查并确保该文件存在。

提示: Apache Commons IO library简化了许多 I/O 相关任务。例如,以下代码片段将文件的内容流式传输到 servlet 响应:

HttpServletResponse response = ...
File myfile = ...
InputStream in = null;
OutputStream out = null;
try {
  in = new FileInputStream(myfile);
  out = response.getOutputStream();
  IOUtils.copy(in, out);
} finally {
  IOUtils.closeQuietly(in); //checks for null
  IOUtils.closeQuietly(out); //checks for null
}

关于java - 下载的文件大小 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10851778/

相关文章:

java - 如何使用 ics 文件中的数据构建日历 View ?

java - as400 使用克罗地亚数据创建 xml

java - 如何在 android 中通过清理处理全局状态

javascript - 使用AJAX将JS变量返回到JSP

java - 自动完成 Primefaces 不记录到数据库

java - Primefaces TreeTable - 无法实例化类

jsf - ViewExpiredException 错误页面未显示

java - 连接字符以形成字符串会给出不同的结果

html - 如何将上下文值传递给CSS

javascript - 使用 jsp、struts2 和 postgresql 填充下拉列表