从JSP下载文件

时间:2018-11-01 23:25:12

标签: jsp servlets

我在JSP上编写了代码,该代码将显示远程托管服务器上的所有文件,当我单击该文件时,应该可以下载它。什么是实现下载的最佳方法?

现在,该JSP可以向我显示该目录中的所有文件,例如->'C:/'

感谢您的帮助

这是示例jsp页面

<%@ page import="java.io.*" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Report</title>
</head>
<body>
<h1>Files</h1>
<ul>

    <%
        String root = "c:/";
        java.io.File file;
        java.io.File dir = new java.io.File(root);

        String[] list = dir.list();



        if (list.length > 0) {

            for (int i = 0; i < list.length; i++) {
                file = new java.io.File(root + list[i]);

        if (file.isFile()) {
    %>
    <li><a href="/<%=file.getAbsolutePath()+file.getName()%>" target="_top"><%=list[i]%>
    </a><br>
            <%
    }
  }
}
%>
</ul>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

我在这里张贴给任何希望在此找到答案的人。

在JSP文件上,使用此链接

<<li><a href="/reportFetch?filePath=<%=file.getAbsolutePath()%>&fileName=<%=file.getName()%>" target="_top"><%=list[i]%></a><br>

并创建servlet

...

import javax.activation.MimetypesFileTypeMap;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

import java.io.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/reportFetch")
public class Report extends HttpServlet {

    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String filePath = request.getParameter("filePath");
        String fileName = request.getParameter("fileName");

        MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
        String mimeType = mimeTypesMap.getContentType(request.getParameter("fileName"));

        response.setContentType(mimeType);
        response.setHeader("Content-disposition", "attachment; filename=" + fileName);

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

    }


    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }
}

答案 1 :(得分:0)

出于某种原因,上述答案失败了,但此解决方案有效:

index.jsp

<form action="download" method="GET">
    <input type="hidden" name="filePath" value="<%=file.getAbsolutePath()%>" />
    <input type="hidden" name="fileName" value="<%=file.getName()%>" />
    <input type="submit" value="DOWNLOAD" />
</form>

下载Servlet.java

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.activation.MimetypesFileTypeMap;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/download")
public class DownloadServlet extends HttpServlet {
  
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    String filePath = req.getParameter("filePath");
    String fileName = req.getParameter("fileName");
    
    MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
    String mimeType = "APPLICATION/OCTET-STREAM";
    if (null != fileName && !"".equals(fileName)) {
      mimeType = mimeTypesMap.getContentType(fileName);
    } else {
      fileName = "file.unknown";
    }
    
    if (filePath != null) {
      resp.setContentType(mimeType);
      resp.setHeader("Content-disposition", "attachment; filename=" + fileName);
      OutputStream out = resp.getOutputStream();
      FileInputStream in = new FileInputStream(filePath);
      byte[] buffer = new byte[4096];
      int length;
      while ((length = in.read(buffer)) > 0) {
        out.write(buffer, 0, length);
      }
      in.close();
      out.flush();
    } else {
      resp.getOutputStream().println("400 Bad Request, please provide the required parameters: /download?filePath=...&fileName=...");
    }
  }
  
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    this.doGet(req, resp);
  }
  
}