0

I have currently a JSP page with a list of files and I would like download each file by a click on this name in the list. My JSP code :

    <c:forEach var="listFiles" items="${listFiles}" varStatus="status">
       <span id="file-${status.index}" class="files">
           <a href="Download">
              <c:out value="${listFiles[status.index]}"></c:out>
           </a>
    </c:forEach>

I found a function for my servlet which run but it's a simple example where we give the path file to the servlet and I would I want this code be generic. How to give each path file to the servlet ?

user3661334
  • 95
  • 2
  • 9

1 Answers1

3

Put this code in your servlet :

String filename=null;

try
{
    filename = request.getParameter("filename");        

    if(filename == null || filename.equals(""))
    {
        throw new ServletException("File Name can't be null or empty");
    }

    String filepath = "yourDirPath"+filename;   //change your directory path

    File file = new File(filepath);
    if(!file.exists())
    {
        throw new ServletException("File doesn't exists on server.");
    }

    response.setContentType("APPLICATION/OCTET-STREAM");
    response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\""); 

    java.io.FileInputStream fileInputStream = new java.io.FileInputStream(filepath);

    int i; 
    while ((i=fileInputStream.read()) != -1) 
    {
         response.getWriter().write(i); 
    } 
    fileInputStream.close();
}
catch(Exception e)
{
    System.err.println("Error while downloading file["+filename+"]"+e);
}

Lets say your servlet url-pattern is : download

Now, your html code for downloading file should be something like this :

<a href="download?filename=YourFileName" target="_blank">Click here to download file</a>
Darshan Patel
  • 2,839
  • 2
  • 25
  • 38
  • 1
    It's better to expose an identifier to the client side which the server can decode into a filename, otherwise exposing and letting the client set the filename is a major security risk – omerio Aug 22 '14 at 14:02
  • Based on this question I assumed, he was learning so that I did not cover security portion. – Darshan Patel Aug 22 '14 at 14:12
  • Thanks, it's run ! But is there a way to have the path without write it in the servlet ? I have the same problem for the upload. It's not generic if I must write the path myself. – user3661334 Aug 22 '14 at 14:18
  • It might be beneficial to write something on what omerio said anyways, that way the OP can learn how to do this, and avoid starting bad habits such as plain file names. – Paul Richter Aug 22 '14 at 14:18
  • @user3661334 There are lots of way.1)You can put directory path in your helper class which contins all your `static final String`. 2)You can load it from `config file`. 3)You can put it in servlet `init param`[how-to-retrieve-init-param-value-from-xml-to-servlet](http://stackoverflow.com/questions/8682740/how-to-retrieve-init-param-value-from-xml-to-servlet). etc. – Darshan Patel Aug 22 '14 at 14:41
  • But your solutions can resolve my problem ? How an user of my application can upload a file, regardless of the directory ? – user3661334 Aug 28 '14 at 09:33
  • Do you want code for file upload??? Refer this link. [file-upload-example-in-servlet-and-jsp](http://javarevisited.blogspot.in/2013/07/ile-upload-example-in-servlet-and-jsp-java-web-tutorial-example.html) – Darshan Patel Aug 28 '14 at 13:57