我认为我的问题需要对背景进行一些解释。我的任务是创建一个基本服务器,它将在我的系统上发送客户请求的 HTML 文件。我被告知只需输入localhost:8080/index.html
我的 Firefox 浏览器作为测试客户端来测试我的服务器。输入该行输入可以正常工作并打印出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();
}
}
}