0

我们正在开发一个文件共享站点,其中包含使用 servlet 2.5、jsp 和 jdbc 的用户和管理员的概念。我们已经通过 servlet 实现了文件上传/下载部分。用户应该在站点中看到他上传的文件和文件夹。但是我们被困在通过 servlet 实现文件夹级视图。我们在 pl/sql 中维护了一个 add_file db 表,并仅检索特定用户上传的文件详细信息。但是,我们还必须显示我们无法显示的文件夹。我遇到了一个更改 apache tomcat 6.0 的 web.xml 的简单解决方案,但它可以让每个用户都能看到每个文件夹和文件的内部,它不依赖于数据库。但我们必须仅根据数据库显示这些内容。有什么办法可以做到这一点?

  1. 上传文件视图.jsp

    ------------代码在这里

    <c:forEach items="${files}" var="file">``
            <c:url value="/DownloadServlet" var="url">
                <c:param name="name" value="${file.name }" />
            </c:url>
    
            <a href="<c:out value="${url}"/>"><c:out value="${file.name}" />
            </a>
    
    
            <br>
        </c:forEach>
    

下载Servlet.java

protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        HttpSession session = request.getSession();
        String filename = null;
        filename = request.getParameter("name");
        PrintWriter out = response.getWriter();
        String filepath = null;
        if (session.getAttribute("deletedfiles") != null)
        {
            filepath = DeleteLocationBO.DELETE_DIRECTORY;


        }
        else
        {
            filepath = UploadLocationBO.UPLOAD_DIRECTORY;
            }
        response.setContentType("APPLICATION/OCTET-STREAM");
        response.setHeader("Content-Disposition", "attachment; filename=\""
                + filename + "\"");

        FileInputStream fileInputStream = new FileInputStream(filepath
                + filename);

        int i;
        while ((i = fileInputStream.read()) != -1) {
            out.write(i);
        }
        fileInputStream.close();
        out.close();
    }

错误堆栈跟踪:(测试是文件夹名称)

D:\Users\projectname\uploads\test(访问被拒绝:Http Status 500)

    exception 
        SEVERE: Servlet.service() for servlet DownloadServlet threw exception
    java.io.FileNotFoundException:D:\Users\projectname\uploads\test (Access is denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at com.servlets.DownloadServlet.doGet(DownloadServlet.java:57)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
4

1 回答 1

0

这是非常简单的算法内容,但这里有一个示例可以帮助您入门:

public void displayFileTree(File rootDirectory) {
    displayFileTree(rootDirectory, "");
}

private void displayFileTree(File rootDirectory, String indent) {
    for (File file : rootDirectory.listFiles()) {
        System.out.println(indent + file.getName());
        if (file.isDirectory()) {
            displayFileTree(file, indent + "    ");
        } 
    }
}

从一个简单的控制台程序运行此代码,然后使其适应您的具体 Web 问题。

如果您想要一个可以在 JSP 中使用的 Java bean,那么它的结构将与文件相同:

public class Node
    private boolean directory;
    private String name;
    private List<Node> children;

    // getters omitted for brevity
}
于 2013-08-18T13:51:57.747 回答