2

I need to read a file (that is not available on web) on the server and output it to the user as a downloadable file.

The scenario is

  1. The user click a link from an XPage
  2. The request is sent to the server which reads a predefined file in the server file system
  3. The file is brought back to the user as a downloadable file in the webbrowser.

The file on the server can be in any format, e.g .pdf, .exe, .doc etc

It does not matter if this is done on SSJS or in java.

I would really appreicate some code

4

2 回答 2

3

Here is a similar question:

How to stream file from xPages?

And here is part of the Java code taken from there and completed by me (+a fix from you!). I have now tested it also and it works:

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();

You could do all this in SSJS also.

If guessContentTypeFromName does not guess it then you need to modify the definition file on server. Or if you have a limited set of file types you can place the MIME-type table in your code/application.

于 2013-02-11T19:39:33.427 回答
0

Here is the code I came up with to do this, def not production code.

    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(); 
    }

Then just call the download method from the beforeRenderResponse of your Xpage

于 2013-02-12T15:23:04.450 回答