• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • Last Modified:

How to download files from remote application server?

Do you know how to download files from remote application server? I don't know whether I need to create a session bean which returns a file back to the servlet and save it under webApplication, and then make a link to the file in jsp page to enable the user to download. But the problem is that how to clean those files.
Some of those files are lvery large. I'm looking for the best solution.

Thanks for the help in advance!

1 Solution
you can use servlet:
response.setHeader("Content-Disposition", "attachment; filename=filename;");
response.setHeader("Content-Type", "application/octet-stream");

response.getOutputStream().write(); // white the file data


If your EJB container and servlet container are the same, or they both have access to the same filesystems, then obviously don't bother with the session bean.  If you're getting your content from a database and using EJBs then it seems sensible to have a servicing session bean provide the content.

If the files are static, oft-downloaded and are not sensitive then the best approach may be to store them in a special area that people can download from.  Otherwise, don't bother writing them to the filesystem.  If you do for some reason decide to cache the file locally, you can save to a temporary file:

InputStream in = myService.getContent( myCriteria );
File temp = File.createTempFile( "download", ".dat" );
OutputStream out = new FileOutputStream( temp );
..write in to out..

As the previous answer suggests, you can then just write the content to the ServletOutputStream that the JSP or Servlet's ServletResponse object will supply you with.

DevelHelperAuthor Commented:
In my case, EJB container and Servlet container are not same (different machines). So based on Dodddiames's suggestion, it doesn't request to save the file which return from the remote EJB session bean when make the following call, right?

OutputStream out = new FileOutputStream( file );

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now