Java 如何从绝对路径名创建文件对象?
我想我的问题需要解释一下背景。我的任务是创建一个基本服务器,在我的系统上发送客户端请求的HTML文件。我被告知只需在firefox浏览器中作为测试客户端输入Java 如何从绝对路径名创建文件对象?,java,file,pathname,Java,File,Pathname,我想我的问题需要解释一下背景。我的任务是创建一个基本服务器,在我的系统上发送客户端请求的HTML文件。我被告知只需在firefox浏览器中作为测试客户端输入localhost:8080/index.html即可测试我的服务器。输入该行输入正确,并打印出index.html的内容。为了安全起见,我需要测试以确保请求的文件直接在我当前的工作范围内,如果不是,我应该拒绝请求。我设置了这样一个陷阱,我想检查一下。我想再次使用我的文件index.html,但是使用整个路径名 C:\Users\Gabrie
localhost:8080/index.html
即可测试我的服务器。输入该行输入正确,并打印出index.html
的内容。为了安全起见,我需要测试以确保请求的文件直接在我当前的工作范围内,如果不是,我应该拒绝请求。我设置了这样一个陷阱,我想检查一下。我想再次使用我的文件index.html
,但是使用整个路径名
C:\Users\Gabrielle\Documents\NetBeansProjects\CS2 Assignment 5\src\index.html
所以我在浏览器中输入
localhost:8080/C:\Users\Gabrielle\Documents\NetBeansProjects\CS2 Assignment 5\src\index.html
我收到一个错误,说文件不存在。然后我检查了一下,看看它想用什么做文件,它想用什么做文件,我得到了
C:%5CUsers%5C%5CGabreille%5C%5CDocuments%5C%5CNetBeansProjects%5C%5CCS2%20Assignment%205%5Cindex.html
这显然不是文件名。我只是输入了错误的文件名吗?如果有什么不同,我将从windows命令提示符运行该程序。下面是我的多线程客户端的代码和我的可运行对象的代码。如果你有任何问题或想澄清,我会密切关注这条线索
import java.io.*;
import java.net.*;
public class WebServer {
public static void main(String[] args)
{
try{
ServerSocket ss = new ServerSocket(8080);
while(true){
Thread conn = new Thread(new ClientConnection(ss.accept()));
conn.start();
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
以下是实际内容
import java.io.*;
import java.net.*;
public class ClientConnection implements Runnable{
private Socket socket;
File requestedFileName;
String entireInput ="";
String editedInput="";
String fileContent="";
String fileLine="";
PrintWriter out;
File defaultFile = new File("index.html");
File toBeRead;
public ClientConnection(Socket socket)
{
this.socket=socket;
}
public void run()
{
try{
System.out.println("Client Connected");
String workingDirectory = System.getProperty("user.dir");
BufferedReader in =
new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream());
String line;
entireInput = in.readLine();
editedInput= entireInput.substring(entireInput.indexOf("GET")+3,
entireInput.indexOf("HTTP/1.1"));
System.out.println("File name:" + editedInput);
requestedFileName = new File(editedInput);
System.out.println("What about here?");
if(editedInput.equals(" / ") || editedInput.equals(" "))
{
toBeRead = defaultFile;
System.out.println("Parent file "+toBeRead.getParent());
String absolutePath = toBeRead.getAbsolutePath();
System.out.println("absolute path "+ absolutePath);
String filePath = absolutePath.substring(0,absolutePath.lastIndexOf(File.separator));
if(filePath.equals(workingDirectory))
{
System.out.println("is in directory");
}
else{
System.out.println("not in directory");
}
}
else
{
String hope = editedInput.substring(2);
toBeRead = new File(hope);
}
//toBeRead = new File("index.html");
if(toBeRead.exists())
{
System.out.println("File exists");
}
else
{
System.out.println("file doesn't exist");
}
BufferedReader fileIn = new BufferedReader(new FileReader(toBeRead));
while((fileLine = fileIn.readLine()) != null)
{
//System.out.println("can i get in while loop?");
fileContent = fileContent + fileLine;
//System.out.println("File content: \n" + fileContent);
}
out.print("HTTP/1.1 200 OK\r\n");
out.print("content-type: text/html\r\n\r\n");
out.print(fileContent);
out.flush();
out.close();
}
catch(FileNotFoundException f)
{
System.out.println("File not found");
out.print("HTTP/1.1 404 Not Found\r\n\r\n");
out.flush();
out.close();
}
catch(Exception e)
{
out.print("HTTP/1.1 500 Internal Server Error\r\n\r\n");
out.flush();
out.close();
}
}
}
您为该文件提供了一个相对路径,并期望它神奇地找到该文件。相对路径是相对于JVM的启动位置解析的。如果需要绝对路径,则需要执行以下操作:
new File("C:\\Users\\Gabrielle\\Documents\\NetBeansProjects\\CS2 Assignment 5\\src\\index.html");
您无法从应用程序的请求中获得这些信息,因为请求是URL编码的
当然,更好的解决方案是将文件放在相对于应用程序的合理位置,并相对引用它。您的意思是什么?这就是为什么路径名中有随机的
%
?我认为,由于分配,我必须能够接受完整的路径名输入。有什么办法绕过这个问题吗?当我只键入index.hmtl
时,为什么它不被编码?当你在URL中输入特殊字符时,它们被编码了。例如%5C是反斜杠,%20是空格。我想你的作业真正想检查的是,是否有人做了类似“localhost:8080/./../someValuableFile.file”的事情。是的,这正是我想做的。你是说因为它的编码方式,这样的事情是不可能的?我给我的教授发了一封电子邮件,问我是否真的必须接受完整路径输入,但还没有得到回复。我认为我需要接受完整路径的原因是“此服务器非常不安全,允许恶意用户访问您计算机上的任何文本文件。您应该执行检查,以便服务器只发送位于您指定目录内的文件”我应该指定的目录是当前工作目录。如果我不必接受完整的路径名输入,我看不出我机器上的任何文本文件是如何被访问的。我不是你的老师,所以我不知道他们在想什么。然而,在一个理智的宇宙中,没有。