strict warning: Only variables should be assigned by reference in /home/itechpros/servoyguy.com/sites/all/modules/links/links.inc on line 1434.

Stream file via JSP

Allows you to stream a file off the file system on the server to the client via JSP.  This could be modified to add security

 

<%@ page import = "java.util.*" %> 
<%@ page import = "java.io.*" %> 
<%@ page import = "com.servoy.j2db.server.headlessclient.*" %> 
<%@ page import = "com.servoy.j2db.dataprocessing.IDataSet" %> 
<%@ page errorPage="errorpage.jsp" %>  
<%  
   ISessionBean servoy_hc = (ISessionBean)session.getAttribute("servoy"); 
   if (servoy_hc == null)  
   {  
      servoy_hc = HeadlessClientFactory.createSessionBean(request,"jsp_docmgt"); 
      session.setAttribute("servoy",servoy_hc);  
   }  
   boolean ok = servoy_hc.setMainForm("jsp_docmgt_main"); 
   if (!ok)  
   {  
      out.print("error cannot work on required form");  
      return;  
   }  
     
    String filename = request.getParameter("fileName");  
   String filepath = request.getParameter("filePath");  
   
   response.setContentType("application/octet-stream");   
   response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\""); 
   
   java.io.FileInputStream fileInputStream = new java.io.FileInputStream(filepath + filename); 
   
   int i;  
   while ((i=fileInputStream.read()) != -1) {  
      out.write(i);  
   }  
   fileInputStream.close();  
   out.close();  
%>

  • Code Repository: