Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 给定文件夹结构,在Jsp中列出文件夹和文件_Java_File_Jsp_Servlets_Web Applications - Fatal编程技术网

Java 给定文件夹结构,在Jsp中列出文件夹和文件

Java 给定文件夹结构,在Jsp中列出文件夹和文件,java,file,jsp,servlets,web-applications,Java,File,Jsp,Servlets,Web Applications,给定文件夹结构,使用Jsp和JavaServlet列出文件夹以及文件夹中存在的文件 提供的文件夹结构是: C:\Users\abc\Desktop\def\def1\text1.txt C:\Users\abc\Desktop\def\def2\text2.txt 申请流程应为:- 第一个index.jsp将显示在屏幕上,其中包含文件和文件夹的超链接列表。单击链接后,它将转发到一个Servlet,在那里它将获取详细信息,而Servlet将转发到view.jsp jsp将显示以下带有超链接的文

给定文件夹结构,使用Jsp和JavaServlet列出文件夹以及文件夹中存在的文件

提供的文件夹结构是:

  • C:\Users\abc\Desktop\def\def1\text1.txt
  • C:\Users\abc\Desktop\def\def2\text2.txt
申请流程应为:-

第一个index.jsp将显示在屏幕上,其中包含文件和文件夹的超链接列表。单击链接后,它将转发到一个Servlet,在那里它将获取详细信息,而Servlet将转发到view.jsp

jsp将显示以下带有超链接的文件夹列表:

  • def1
  • def2
单击def1时,view.jsp应显示

  • def1.\text1.txt
  • def2
单击def2时,应该显示view.jsp

  • def1
  • def2。\text2.txt
我可以在view.jsp中显示文件夹列表,但在单击文件夹超链接后无法显示其中的文本文件

我想不出一个逻辑来获取文件夹的详细信息以及其中的文件,并在单击文件夹超链接的同时显示它们。 所以我决定继续前进,用jsp编写逻辑,然后通过Servlet发送view.jsp。我知道在jsp中编写任何业务逻辑都是不好的做法。 但我甚至在使用jsp代码时遇到了麻烦,我只能设法显示文件夹列表

我尝试了以下代码:

myview.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="java.io.File,java.io.IOException,java.util.*,java.io.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Contents <br>
<%!
public boolean displayFileContents(String path) {
    try {
        File currentDirectory = new File("C:\\Users\\abc\\Desktop\\def\\"+path);
        System.out.println("Current Directory is :- " + currentDirectory.getCanonicalPath());

        File[] files = currentDirectory.listFiles();
        for (File file : files) {
            if (file.isFile()) {
                String filePath=file.getCanonicalPath();
                String filePathList[]=filePath.split(path);
                String filePathnew="";
                for(int i=0;i<filePathList.length;i++)
                {
                    filePathnew=filePathList[i];

                }
            }
            
        }
        //display file contents in list
        
    } catch (IOException e) {
        e.printStackTrace();
    }
    return true;
}

%>


<% 
 

try {
    File currentDirectory = new File("C:\\Users\\abc\\Desktop\\def\\");
   
    File[] files = currentDirectory.listFiles();
    for (File file : files) {
        if (file.isDirectory()) {
            String directoryPath=file.getCanonicalPath();
            String directoryPathList[]=directoryPath.split("def");
            String b="";
            for(int i=0;i<directoryPathList.length;i++)
            {
                directoryPath=directoryPathList[i];
                System.out.println(directoryPath);

            }
            %>
            <li>
            <a href="./<%=displayFileContents(file.getName())%>"><%=file.getName()%></a>
            </li>
            <%
        }
    }
} catch (IOException e) {
    e.printStackTrace();
}
%>

</body>
</html>

任何帮助都将不胜感激。

有人能帮我吗?
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Archetype Created Web Application</title>
         
        <link rel="stylesheet" href="resource/css/main.css" />
        <script type="text/javascript" src="resource/js/jquery-3.2.1.min.js"></script>
        <script type="text/javascript" src="resource/js/fileupload.js"></script>
    </head>
    <body>
        <div class="panel">
            <h1>List Files and Folders</h1>
        </div>
         
        <!-- List Files and Folders -->
        <div class="panel">
            <a id="listFilesFolders" class="hyperLink" href="<%=request.getContextPath()%>/FileXplorerServlet">List Files and Folders</a>
        </div>
    </body>
</html>
@WebServlet(description = "List Files and Folders", urlPatterns = { "/FileXplorerServlet" })
public class FileXplorerServlet extends HttpServlet {
    
   
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        RequestDispatcher dispatcher = request.getRequestDispatcher("/view.jsp");
        dispatcher.forward(request, response);

    }

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

}