1

Hello I use this XAgent with this SSJS code to stream a file from server filesystem:

var filepath="c:/test/file.pdf"  //380MB of file
var extContext:javax.faces.context.ExternalContext = facesContext.getExternalContext();
var response:com.ibm.xsp.webapp.XspHttpServletResponse = extContext.getResponse();
var writer:javax.servlet.ServletOutputStream = response.getOutputStream();
   estensione=@Right(filepath,3).toLowerCase();
   fileName=@RightBack(filepath,"/");
   contentype="application/octet-stream";
   contentype=(estensione=="pdf")?"application/pdf":contentype;
   response.setContentType(contentype); // content type of data, for other binary see mime types or put "application/octet-stream"
   response.setHeader("Cache-Control", "no-cache");
   var filestream:java.io.File = new java.io.File(filepath); //for check file size
   var maxfilesize:long=20*1024*1024;
   var typestream= filestream.length()>maxfilesize?"attachment;":"inline;";
   response.setHeader("Content-Disposition" ,typestream+" filename=\"" + fileName+"\""); //31/12/2015 required if You need to pass it back as file
   response.setHeader("Content-Length", ""+filestream.length());
   var inStream:java.io.FileInputStream = new java.io.FileInputStream(filepath);
   var btbuffer:byte= new byte[32767];
   var ilen,bytesBuffered:int=0;
   while ((ilen=inStream.read(btbuffer))!=-1){
       writer.write(btbuffer,0,ilen)
       writer.flush(); //31/12/2015 - flush writer but don't work
   }
   inStream.close();
   writer.close();
  facesContext.responseComplete();

The code work correctly..but when we have 2 or more download in the same time, the server show HTTP JVM: java.lang.OutOfMemoryError: because I have 1024M NOTES.INI setting.

The really problem is the ServletOutputStream that use the total filesize in memory javaheap and free it after the client download is terminated or canceled. Have someone idea how fix? I have read this question and this question without any success.

UPDATE 04 JANUARY 2016

I've solve the problem with this line code after the response assignment:

response.disableXspCache(false);

the false parameter was mandatory!

Finally I have too remove the code writer.close()

Thank Sven this blog and the comment of Riand Philippe

Community
  • 1
  • 1
Daniele Grillo
  • 1,011
  • 4
  • 13
  • 31

1 Answers1

2

The XPages runtime is by default buffering the page result until the whole page is completed. This allows the runtime to fully revert the result in case of an error, and emit an error message. This is done by the XspHttpServletResponse.

You can change this behavior by casting the response and calling disableXspCache() on this class.

Philippe Riand
  • 673
  • 4
  • 12