Java,文件不';即使存在,也不存在错误,web服务器

Java,文件不';即使存在,也不存在错误,web服务器,java,Java,我正在编写一个基本的HTTP服务器,其中一个要求是服务器检查请求的文件是否存在。下面是我的代码 Scanner scn = new Scanner(lines[0]); String command = scn.next(); String fileName = scn.next(); System.out.println("Command: " +command); System.out.println("Resource: "+"www" +fileName); File ifile =

我正在编写一个基本的HTTP服务器,其中一个要求是服务器检查请求的文件是否存在。下面是我的代码

Scanner scn = new Scanner(lines[0]);
String command = scn.next();
String fileName = scn.next();

System.out.println("Command: " +command);
System.out.println("Resource: "+"www" +fileName);

File ifile = new File(fileName);
if( ! ifile.exists() ) {
System.out.println("file "+ fileName  + " doesn't exist");
} else {
String sizestr = "bytes in file: " + ifile.length();
System.out.println(sizestr);
}
我遇到的问题是,我得到的响应是,即使文件不存在,也不存在。有人能帮忙吗?谢谢

下面是我的程序的完整代码:

import java.io.*;
import java.net.*;
import java.util.*;

public class UselessHTTPServer05 {
public static void main(String args[]) throws Exception {
int port = Integer.parseInt(args[0]);
ServerSocket serverSock=new ServerSocket(port);

while(true) {
  Socket conn = serverSock.accept();
  Scanner scanin = new Scanner(conn.getInputStream());
  String line=null;
  int nlines=0;
  String lines[] = new String[32];

  while (true) {
    line = scanin.nextLine();
if(line.length()==0) break;
lines[nlines] = line;
    nlines = nlines + 1;

  }


  for(int i=0; i<nlines; i=i+1) {
  System.out.println("line "+i+": "+lines[i]);
  }

  Scanner scn = new Scanner(lines[0]);
  String command = scn.next();
  String fileName = scn.next();

  System.out.println("Command: " +command);
  System.out.println("Resource: "+"www" +fileName);

   String webRoot = "/www";
   File f = new File(webRoot, fileName);
  if( ! f.exists() ) {
      System.out.println("file "+ f.getCanonicalPath() + " doesn't exist"); }
  else {
  String sizestr = "bytes in file: " + fileName;
  System.out.println(sizestr);
  }




  String reply="HTTP/1.0 404 Not Found\r\n" +
               "Connection: close\r\n" +
               "Content-Type: text/html\r\n" +
               "\r\n" +
               "<h1>Sorry, work in progress</h1>\r\n";


  OutputStream outs = conn.getOutputStream();
  outs.write(reply.getBytes());

  conn.close();
}
}
}
import java.io.*;
导入java.net。*;
导入java.util.*;
公共类UselessHTTPServer05{
公共静态void main(字符串args[])引发异常{
int port=Integer.parseInt(args[0]);
ServerSocket serverSock=新的ServerSocket(端口);
while(true){
Socket conn=serverSock.accept();
Scanner scanin=新扫描仪(conn.getInputStream());
字符串行=null;
int-nlines=0;
字符串行[]=新字符串[32];
while(true){
line=scanin.nextLine();
如果(line.length()==0)中断;
行[nlines]=行;
nlines=nlines+1;
}

对于(inti=0;i更改您的输出消息以包含一个调用,这样您就可以准确地知道它试图从何处读取

System.out.println("file " + ifile.getCanonicalPath()  + " doesn't exist");
您可能应该构建
文件
,这样您可以先设置“webRoot”


仔细检查您的类路径。我确信您的文件位于错误的目录中。您可以通过编程方式创建一个新文件并尝试读取此文件来测试这一点。然后检查您新创建的文件的路径。
fileName
,d的示例字符串值是多少,这些文件位于何处,以及从何处尝试访问它们??您可以e通过Java Web项目和servlets或其他更高级的工具来实现这一点?如果没有这些问题的答案,您在这里得到的最佳回答就是“数据不足”。请记住,如果您的文件在project中的某个位置,那么访问它会有点困难。我将它们放在一个子目录中,也尝试将它们放在与我的程序相同的目录中。我正在linux上使用emacs文本编辑器编写代码。“但是文件存在”在这类问题中,您经常会听到这样的说法,但在99%的问题中,这种说法是错误的。要么文件路径错误,要么文件名包含意外的空格,要么文件扩展名意外。添加getCanonicalPath()后和字符串webRoot。我可以看到它试图从正确的文件夹中读取文件。它仍然找不到它。@MikeyD它试图读取的文件存在,并且它有权读取它?你确定吗?你真的确定吗?你有一个文件夹“/www”在你的硬盘上?好的,问题解决了。我是一个十足的傻瓜,因为我没有指定路径,所以越是找不到它。现在添加字符串webRoot之后,它就找不到它了,因为我写了“/www”而不是“www”。在摆脱/it后,它找到了该文件。感谢您的帮助,很抱歉浪费了时间!很高兴提供帮助。请不要忘记添加一个检查,以检查该文件是否位于您期望的目录下。否则,相关路径可能会像
。/../../../../../../../../../../../../../quicken.qif
String webRoot = "/some/directory"; 
File ifile = new File(webRoot, fileName);