代码之家  ›  专栏  ›  技术社区  ›  Egor Vasilyev

javaee,servlet创建指向文件夹中文件的链接

  •  0
  • Egor Vasilyev  · 技术社区  · 6 年前

    有一个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 回复  |  直到 6 年前
        1
  •  0
  •   Egor Vasilyev    6 年前

    我解决了我的问题。以防有人需要或者有人知道一个更漂亮的方法。

    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.";
          }
    }
    

    和下载Servlet

    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);
        }
    
    }