Java 下载的文件大小为0

Java 下载的文件大小为0,java,jsp,jsf,servlets,alfresco,Java,Jsp,Jsf,Servlets,Alfresco,我在这里尝试添加一个特定的对话框bean,用于在Alfresco Explorer上执行操作,该浏览器应该下载一个特定的docx文件。当我点击下载操作时,代码工作正常,它下载文件,但正如我的问题标题中提到的,文件大小为0字节 我用这个来做这个: public class NewFormDialog extends BaseDialogBean { protected String aspect; protected String finishImpl(FacesContext context

我在这里尝试添加一个特定的对话框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
。检查以确保该文件存在

提示:简化了许多与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
}

事实上我想我弄错了这条路。我在filepath“”中使用了类似的内容,但我正在重新启动服务器以尝试这样做:“localhost:8080\\alfresco\\d\\a\\workspace\\SpacesStore\\space1\\”我刚刚检查了Tomcat的日志文件,发现这是由以下原因引起的
:javax.servlet.ServletException:在。。。54更多
谢谢你的提示。问题解决了。这是一个路径问题,我必须执行类似“C:\\Alfresco\\”的操作。现在它工作得很好。再次感谢你的提示@Michael。对不起,我刚刚看到你的留言。完成了@哦,好的,这不是问题