有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java实现一个简单的文件下载servlet

我应该如何实现简单的文件下载servlet

其思想是使用GET请求index.jsp?filename=file.txt,用户可以下载file.txt来自文件servlet,文件servlet会将该文件上载到用户

我可以获得文件,但如何实现文件下载


共 (5) 个答案

  1. # 1 楼答案

    尝试使用资源

    File file = new File("Foo.txt");
    try (PrintStream ps = new PrintStream(file)) {
       ps.println("Bar");
    }
    response.setContentType("application/octet-stream");
    response.setContentLength((int) file.length());
    response.setHeader( "Content-Disposition",
             String.format("attachment; filename=\"%s\"", file.getName()));
    
    OutputStream out = response.getOutputStream();
    try (FileInputStream in = new FileInputStream(file)) {
        byte[] buffer = new byte[4096];
        int length;
        while ((length = in.read(buffer)) > 0) {
            out.write(buffer, 0, length);
        }
    }
    out.flush();
    
  2. # 2 楼答案

    并发送一个大文件

    byte[] pdfData = getPDFData();
    
    String fileType = "";
    
    res.setContentType("application/pdf");
    
    httpRes.setContentType("application/.pdf");
    httpRes.addHeader("Content-Disposition", "attachment; filename=IDCards.pdf");
    httpRes.setStatus(HttpServletResponse.SC_OK);
    OutputStream out = res.getOutputStream();
    System.out.println(pdfData.length);
             
    out.write(pdfData);
    System.out.println("sendDone");
    out.flush();
    
  3. # 3 楼答案

    那要看情况。如果所述文件通过HTTP服务器或servlet容器公开可用,则只需通过response.sendRedirect()重定向到即可

    如果不是,则需要手动将其复制到响应输出流:

    OutputStream out = response.getOutputStream();
    FileInputStream in = new FileInputStream(my_file);
    byte[] buffer = new byte[4096];
    int length;
    while ((length = in.read(buffer)) > 0){
        out.write(buffer, 0, length);
    }
    in.close();
    out.flush();
    

    当然,您需要处理适当的异常

  4. # 4 楼答案

    假设您可以访问servlet,如下所示

    http://localhost:8080/myapp/download?id=7
    

    我需要创建一个servlet并将其注册到web。xml

    网络。xml

    <servlet>
         <servlet-name>DownloadServlet</servlet-name>
         <servlet-class>com.myapp.servlet.DownloadServlet</servlet-class>
    </servlet>
    <servlet-mapping>
         <servlet-name>DownloadServlet</servlet-name>
         <url-pattern>/download</url-pattern>
    </servlet-mapping>
    

    下载servlet。爪哇

    public class DownloadServlet extends HttpServlet {
    
    
        protected void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
             String id = request.getParameter("id");
    
             String fileName = "";
             String fileType = "";
             // Find this file id in database to get file name, and file type
    
             // You must tell the browser the file type you are going to send
             // for example application/pdf, text/plain, text/html, image/jpg
             response.setContentType(fileType);
    
             // Make sure to show the download dialog
             response.setHeader("Content-disposition","attachment; filename=yourcustomfilename.pdf");
    
             // Assume file name is retrieved from database
             // For example D:\\file\\test.pdf
    
             File my_file = new File(fileName);
    
             // This should send the file to browser
             OutputStream out = response.getOutputStream();
             FileInputStream in = new FileInputStream(my_file);
             byte[] buffer = new byte[4096];
             int length;
             while ((length = in.read(buffer)) > 0){
                out.write(buffer, 0, length);
             }
             in.close();
             out.flush();
        }
    }
    
  5. # 5 楼答案

    实现下载的最简单方法是将用户引导到文件位置,浏览器将自动为您执行此操作

    您可以通过以下方式轻松实现:

    HttpServletResponse.sendRedirect()