Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.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 将根文件夹设置为索引_Java_Servlets_File Io - Fatal编程技术网

Java 将根文件夹设置为索引

Java 将根文件夹设置为索引,java,servlets,file-io,Java,Servlets,File Io,我这里有这个代码: import java.io.*; import javax.servlet.*; import javax.servlet.http.*; /** * * @author Nathan Campos */ public class Files extends HttpServlet { PrintWriter out = null; // moved outside doGet() for use in ls() @Over

我这里有这个代码:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Nathan Campos
 */
public class Files extends HttpServlet {
    PrintWriter out = null;              // moved outside doGet() for use in ls()
    @Override
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        // PrintWriter out = response.getWriter(); would create a copy no accessable in ls()
        out = response.getWriter();   // this uses the out declared outside
        File startDir = new File("C:\\test");
        ls(startDir);
    }

    private void ls(File f) {
        File[] list = f.listFiles();
        if ( list == null ) {
            out.println("Returned null");
                    return; // otherwise the for loop will crash
        }
        for(File file : list) {
            if(file.isDirectory()) {
                ls(file);
            } else {
                out.println("<a href='+file.toURL()+'>'+file.getName()+'</a>");
            }
        }
    }
}

应该从配置中读取启动目录。但是你可以这样称呼它:

PrintWriter out = null; // moved outside doGet() for use in ls()

public void doGet(HttpServletRequest request,
                  HttpServletResponse response)
throws ServletException, IOException {
    response.setContentType("text/html");
    out = response.getWriter();

    File startDir = new File("C:\\WorkFiles\\ServletFiles");
    ls( startDir );
}
ls()中的打印行有一个问题(您不能将“和”混合),应将其重写为

out.println("<a href="+file.toURL()+'>'+file.getName()+"</a>");

应该从配置中读取启动目录。但是,您可以这样称呼它:

PrintWriter out = null; // moved outside doGet() for use in ls()

public void doGet(HttpServletRequest request,
                  HttpServletResponse response)
throws ServletException, IOException {
    response.setContentType("text/html");
    out = response.getWriter();

    File startDir = new File("C:\\WorkFiles\\ServletFiles");
    ls( startDir );
}
ls()中的打印行有一个问题(您不能将“和”混合),应将其重写为

out.println("<a href="+file.toURL()+'>'+file.getName()+"</a>");

与实际问题无关:

PrintWriter out = null;              // moved outside doGet() for use in ls()
这是一个极坏的主意。这样,响应编写器在所有HTTP请求中共享,并且每个新HTTP请求都将覆盖前一个请求的响应编写器。当前一个请求实际上仍未完成时,客户端将永远不会检索响应的剩余部分,而这将显示在另一个客户端的最新请求的响应中

换句话说:您的servlet不是线程安全的。不要将请求和/或特定于会话的数据作为servlet的实例变量获取。只需将其作为方法参数传递

private void ls(File f, PrintWriter out) {
    // ...
}
另见:

与实际问题无关:

PrintWriter out = null;              // moved outside doGet() for use in ls()
这是一个极坏的主意。这样,响应编写器在所有HTTP请求中共享,并且每个新HTTP请求都将覆盖前一个请求的响应编写器。当前一个请求实际上仍未完成时,客户端将永远不会检索响应的剩余部分,而这将显示在另一个客户端的最新请求的响应中

换句话说:您的servlet不是线程安全的。不要将请求和/或特定于会话的数据作为servlet的实例变量获取。只需将其作为方法参数传递

private void ls(File f, PrintWriter out) {
    // ...
}
另见:

doGet不调用ls方法。也许你忘记粘贴什么了?f.listFiles();可能会返回null(我在c:\\windows中测试时看到了这一点),并认为只有在windows“特殊文件”检查null并返回时才会出现这种情况。@stacker:我仍然得到相同的结果thing@Nathan请您发布整个代码,或者至少描述一下Files.java的第30行是什么?@Nathan Oops PrintWriter out=response.getWriter();应该是out=response.getWriter();否则,ls()中使用的成员out不会初始化。如果(list==null)doGet不调用ls方法,也在块中添加一个返回。也许你忘记粘贴什么了?f.listFiles();可能会返回null(我在c:\\windows中测试时看到了这一点),并认为只有在windows“特殊文件”检查null并返回时才会出现这种情况。@stacker:我仍然得到相同的结果thing@Nathan请您发布整个代码,或者至少描述一下Files.java的第30行是什么?@Nathan Oops PrintWriter out=response.getWriter();应该是out=response.getWriter();否则,ls()中使用的成员out不会初始化。如果(list==null)我刚刚得到这个错误,
非静态变量out不能从静态上下文引用,那么也在块中添加一个返回。参考
out.println(此处的url)
从ls()方法中删除static,将“PrintWriter out”设为static将是一个坏主意。我仍然得到相同的结果,我刚刚得到一个错误:
非静态变量out不能从静态上下文引用
。参考
out.println(此处的url)
Remove static from ls()方法,将“PrintWriter out”设为static将是一个坏主意。我仍然得到同样的结果