Java 如何在XPages中读取服务器文件并将其作为用户web浏览器中的可下载文件输出

Java 如何在XPages中读取服务器文件并将其作为用户web浏览器中的可下载文件输出,java,download,xpages,output,Java,Download,Xpages,Output,我需要在服务器上读取一个文件(在web上不可用),并将其作为可下载文件输出给用户 情况是 用户单击XPage中的链接 请求被发送到服务器,该服务器读取服务器文件系统中的预定义文件 该文件作为webbrowser中的可下载文件带回给用户 服务器上的文件可以是任何格式,例如.pdf、.exe、.doc等 不管这是在SSJS上还是在java中完成的 我很欣赏一些代码这里有一个类似的问题: 这里是我从那里获得并完成的Java代码的一部分(+您的修复!)。我现在也测试了它,它可以工作了: FacesCo

我需要在服务器上读取一个文件(在web上不可用),并将其作为可下载文件输出给用户

情况是

  • 用户单击XPage中的链接
  • 请求被发送到服务器,该服务器读取服务器文件系统中的预定义文件
  • 该文件作为webbrowser中的可下载文件带回给用户
  • 服务器上的文件可以是任何格式,例如.pdf、.exe、.doc等

    不管这是在SSJS上还是在java中完成的


    我很欣赏一些代码这里有一个类似的问题:

    这里是我从那里获得并完成的Java代码的一部分(+您的修复!)。我现在也测试了它,它可以工作了:

    FacesContext facesContext = FacesContext.getCurrentInstance();
    XspHttpServletResponse  response = (XspHttpServletResponse) facesContext.getExternalContext().getResponse();
    
    String strFileName = "myfile.txt";
    String strFilePath= "c:" + File.separator + strFileName;
    response.setContentType(URLConnection.guessContentTypeFromName(strFileName));
    response.setHeader("Content-Disposition","attachment;filename=" + strFileName);
    
    //File file = new File(strFilePath);
    FileInputStream fileIn = new FileInputStream(strFilePath);
    ServletOutputStream out = response.getOutputStream();
    
    int iLen = 0;
    byte[] btBuffer = new byte[10240];  // Not sure about optimal buffer size
    while ((iLen = fileIn.read(btBuffer)) != -1) {
      out.write(btBuffer, 0, iLen);
    }
    
    facesContext.responseComplete();
    out.close();
    
    您也可以在SSJS中完成所有这些


    如果
    guessContentTypeFromName
    没有猜到它,则需要修改服务器上的定义文件。或者,如果您有一组有限的文件类型,您可以将MIME类型表放在代码/应用程序中。

    以下是我为此而编写的代码,而不是生产代码

        public static byte[] grabFile(String readFile) throws IOException {
    
            File file = new File(readFile);
            ByteArrayOutputStream ous = new ByteArrayOutputStream();
            InputStream ios = new FileInputStream(file);
    
            try {
                byte []buffer = new byte[4096];
    
                int read = 0;
                while ( (read = ios.read(buffer)) != -1 ) {
                    ous.write(buffer, 0, read);
                }
            } finally { 
                try {
                     if ( ous != null ) 
                         ous.close();
                } catch ( IOException e) {
                }
    
                try {
                     if ( ios != null ) 
                          ios.close();
                } catch ( IOException e) {
                }
            }
            return ous.toByteArray();
        }
    
     public static void download() throws IOException {
            byte[] data = grabFile("\\\\server\\path\\to\\file.pdf");
            HttpServletResponse response = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
            response.reset(); 
            response.setContentType("application/pdf"); 
            response.setHeader("Content-disposition", "attachment; filename=\"filename.pdf\"");
            OutputStream output = response.getOutputStream();
            output.write(data);
            output.close();
            FacesContext.getCurrentInstance().responseComplete(); 
        }
    

    然后从您的Xpage的beforeRenderResponse调用下载方法

    能否请您编写一个完整的脚本,我无法让它工作。我找不到文件,我今天晚些时候想做。同时,检查文件路径和名称是否正确可能是一个好主意:-)谢谢,如何使用getFacesContext()解决源代码错误?已尝试此FacesContext FacesContext=FacesContext.getCurrentInstance();XspHttpServletResponse=(XspHttpServletResponse)facesContext.getExternalContext().getResponse();但当运行Yes时,我得到了一个未找到的文件,看起来链接答案中的代码是要从servlet运行的。我认为你的代码是正确的(更新到我的答案)。检查运行代码(系统?)的帐户是否有权访问该文件。如果可以从Java.Panu打开任何文件,也可以尝试使用简单的一行代码,我只能选择一个正确答案,因此我选择了Toby,因为他有最后缺少的部分来解决它。但是你的代码也很有用,所以我投了你一票。