JavaEE,Servlet创建指向文件夹中文件的链接

时间:2018-06-26 11:01:09

标签: java tomcat servlets java-ee tomcat7

有一个Servlet,它从客户端接受文件并将它们存储在文件夹中。 现在必须列出该文件夹中的文件并创建指向它们的链接(即,单击文件名,然后从您那里下载文件)。

现在只输出文本形式的文件列表。如何创建指向它们的链接?我读到了这一点,足以暴露标题了,但是如何完成和尚未找到。 示例代码:

public class FileListServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public FileListServlet() {
        super();
  }

    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {


        String param = "/dirs";

        PrintWriter w = res.getWriter();
        res.setContentType("text/html");

        String root="/dirs";
        java.io.File dir = new java.io.File(root);

        File[] fileList = dir.listFiles();

        w.println("<H2><FONT COLOR=TEAL>" + "Total number of files in the choosen directory - " +
            fileList.length + "</FONT></H2>");
        w.println("<H3><FONT COLOR=PURPLE>" +
            "Directory path - " + param + "</FONT></H3><HR>");
        w.println("<TABLE BORDER=0 CELLSPACING=5>");

       for(int i = 0; i < fileList.length; i++)
            printName(fileList[i], w);      

        w.println("</TABLE><HR>");

      }

     private void printName(File name, PrintWriter output)
      {

        String type = name.isDirectory()
            ? " (Directory)" : " (File)";
        output.println("<TR><TD>" + type + "</TD><TD><FONT COLOR=BLUE>"
            + name.getName()  + "</FONT></TD></TR>");

      }

      public String getServletInfo()
      {
        return "This servlet shows a content of a directory" +
               "mentioned in dirToShow parameter or property.";
      }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

我解决了我的问题。万一有人需要它,或者有人知道更美好的方式。

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

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

public class FileListServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public FileListServlet() {
        super();
  }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        PrintWriter printWriter = response.getWriter();
        response.setContentType("text/html");

        File currentFolder = new File(".");
        File workingFolder = new File(currentFolder, "Sorted files");

        String root = workingFolder.getAbsolutePath();
        java.io.File dir = new java.io.File(root);

        File[] fileList = dir.listFiles();

        printWriter.println("<H2><FONT COLOR=TEAL>" + "Total number of files in the choosen directory - " +
            fileList.length + "</FONT></H2>");
        printWriter.println("<H3><FONT COLOR=PURPLE>" +
            "Directory path - " + root + "</FONT></H3><HR>");
        printWriter.println("<TABLE BORDER=0 CELLSPACING=5>");



        for(int i = 0; i < fileList.length; i++) {
            printName(fileList[i], printWriter);      
        }
        printWriter.println("</TABLE><HR>");

      }

     private void printName(File file, PrintWriter output)
      {

         System.out.println(file.getName());

        output.println("<tr><td><a href=\"https://Upload/DownloadServlet?name="
                +file.getName()+"\">" + file.getName() + "</a></td></tr>" );

      }

      public String getServletInfo()
      {
        return "This servlet shows a content of a directory" +
               "mentioned in dirToShow parameter or property.";
      }
}

和DownloadServlet

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

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

/**
 * Servlet implementation class DownloadServlet
 */
public class DownloadServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public DownloadServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String name = request.getParameter("name");

        String fileType = "multipart/form-data";

        response.setContentType(fileType);

        response.setHeader("Content-disposition", "attachment; filename=" + name);

        File currentFolder = new File(".");
        File workingFolder = new File(currentFolder, "Sorted files");

        String root = workingFolder.getAbsolutePath();

        File file = new File(root + File.separator + name);

        OutputStream output = response.getOutputStream();
        FileInputStream input = new FileInputStream(file);

        byte[] buffer = new byte[4096];
        int lenght;

        while( ( lenght = input.read(buffer) ) > 0 ) {
            output.write(buffer, 0, lenght);
        }

        input.close();

        output.flush();
        output.close();

        response.getOutputStream().flush();
        response.getOutputStream().close();
    }

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

}