我認爲我的問題將需要一些背景解釋。我的任務是創建一個基本服務器,它將根據客戶端請求在我的系統上發送HTML文件。我被告知通過在我的Firefox瀏覽器中輸入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();
}
}
}
你是什麼意思URLEncoded?這就是爲什麼路徑名中有隨機'%'的原因嗎?我想,因爲這個任務,我必須能夠接受完整的路徑名輸入。無論如何繞過URLEncoding?爲什麼我只輸入'index.hmtl'時沒有編碼? – art3m1sm00n
當您將特殊字符放入URL中時,它們將被編碼。例如%5C是一個反斜槓,%20是一個空格。我認爲你的任務真的要檢查的是如果有人做了「localhost:8080 /../../ someValuableFile.file」之類的東西。 – Aurand
是的,這正是我想要做的。所以你告訴我,因爲它被編碼的方式,這樣的事情是不可能的?我給我的教授發了一封電子郵件,詢問我是否真的需要接受全路徑輸入並且沒有得到答覆。我認爲我需要接受完整路徑的原因是因爲「這個服務器是非常不安全的,並且會允許惡意用戶訪問你機器上的任何文本文件。你應該執行一個檢查,以便服務器只發送那些位於您指定的目錄內「 – art3m1sm00n