How to list files in a directory and make them downloadable

Posted on 2006-04-20
Last Modified: 2010-03-31
    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 ?

Question by:sheikashrub
    LVL 14

    Accepted Solution

    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>
    LVL 8

    Assisted Solution

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Suggested Solutions

    Title # Comments Views Activity
    scoresAverage challenge 8 58
    hasOne  challenge 59 62
    Increment alphanumeric sequence 6 52
    sumDigits  challenge 7 37
    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now