简体   繁体   English

使用servlet从URL下载文件或文件夹

[英]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 我对JSP / Servlet相当陌生,并尝试使用以下JSP文件中的代码将文件/文件夹路径和文件/文件夹名称传递给servlet,从而从本地目录下载文件/文件夹。

<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 我想增强我的servlet,以下载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 : 以下是我的Servlet代码:

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. 您无法从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. 不要关闭myOut OutputStream,它由Servlet容器处理。 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. filePath传递到Servlet可能会带来安全风险,您不知道用户将尝试下载哪些文件,可能是他将尝试下载密码哈希。 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. 在下载页面上,您可以从下载文件夹中列出( File.list() )文件和文件夹,作为指向您文件夹的超链接,这样用户不必键入文件名或路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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