简体   繁体   中英

Download file or folder from URL using servlet

I am fairly new to JSP/Servlets and trying to download file/folder from my local directory by passing file/folder path and file/folder name to servlet using below code in JSP file

<a href="<%=request.getContextPath()%>\download?filename=<filename>&filepath=http://192.168.0.101:8080<%=request.getContextPath()%>/<foldername>">Download Here</a>

I want to enhance the my servlet to download any type of file or folder passed in the URL

e.g. 
If the Folder/File URL is passed to the servlet

http://192.168.0.101:8080/folder
http://192.168.0.101:8080/file.pdf

Below is my Servlet Code :

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DownloadServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String filename = request.getParameter("filename");
        String filepath = request.getParameter("filepath");
        BufferedInputStream buf=null;
           ServletOutputStream myOut=null;

        try{

        myOut = response.getOutputStream( );
             File myfile = new File(filepath+filename);
             response.setContentType("application/x-download"); 
             response.addHeader(
                "Content-Disposition","attachment; filename="+filename );
             response.setContentLength( (int) myfile.length( ) );
             FileInputStream input = new FileInputStream(myfile);
             buf = new BufferedInputStream(input);
             int readBytes = 0;
             while((readBytes = buf.read( )) != -1)
               myOut.write(readBytes);
        } catch (IOException ioe){
                throw new ServletException(ioe.getMessage( ));
             } finally {
                 if (myOut != null)
                     myOut.close( );
                  if (buf != null)
                  buf.close( ); 
             }
    }
}

If anyone can point me to right direction with above ask, that would be really helpful for me.

You can't download folders from HTTP URL. You always download files.

If you want to download an entire directory then you basically have to download all the files inside it and files inside it's sub directories recursively. Or you can create an archive of that folder and provide that as the download.

You should use a buffer for the following portion of code instead of reading one byte in each iteration.

int readBytes = 0;
while((readBytes = buf.read( )) != -1)
    myOut.write(readBytes);

With buffer

byte[] buffer = new byte[4096];//4K buffer
int readLen = 0; //Number of bytes read in last call to read, max is the buffer length
while((readLen = buf.read(buffer)) != -1) {
    myOut.write(buffer, 0, readLen);
}
  • Don't close myOut OutputStream, it's handled by the Servlet container. Rule of thumb is if you didn't open it don't close it.

  • Passing the filePath to the Servlet can pose a security risk, you don't know which files user will try to download, may be he will try to download the password hash. You should make the file path relative to a folder which you want to provide for download. Then you will always have to prepend that folder path to the relative path, so that user can download the files from that folder only.

  • On your download page, you can list ( File.list() ) the files and folders from the download folder, as hyperlink to your folder, so that user doesn't have to type the file name or path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM