Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

How to list files in a directory and make them downloadable

    I have a JSP on which Iam displaying a link (eg :Display All Files) which upon clicking calls a struts action class which does a Dir read and does a listing of all the files .
I want to display these files as links and when clicked I want the download window to pop up so that the user can download the file.
   Any  idea how I can do this in Java ?

2 Solutions
nothing special just generate an html page that contains links to the files like
<a href="file1 path">download file1</a>
<a href="file2 path">downloda file2</a>
You'll need a servlet that is able to read the file from the local hard disk and send it to the client. If your servlet i called 'GetFile', then you would add links in your JSP page somethign like the folloiwng:

<a href="GetFile?path=1.txt">file1</a>
<a href="GetFile?path=2.txt">file1</a>

Depending on how your server is setup, you might find that your servlet cant access the local file store, in which case the best solution would be to place your files in a database for the servlet to access.

Either way, the servlet will extract the path from the JSP paramater posted to it, open the file based on that path, then send the file found back to the client.


Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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