5

私の問題には、背景の説明が必要だと思います。私の課題は、クライアントが要求する HTML ファイルを私のシステムに送信する基本的なサーバーを作成することです。localhost:8080/index.htmlFirefox ブラウザにテスト クライアントとして入力するだけで、サーバーをテストするように言われました。その入力行に入力すると正しく機能し、 の内容が出力され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();
        }

    }
}
4

1 に答える 1

5

ファイルに相対パスを指定し、魔法のようにファイルを見つけることを期待しています。相対パスは、JVM が開始された場所に対して相対的に解決されます。絶対パスが必要な場合は、次のようにする必要があります。

new File("C:\\Users\\Gabrielle\\Documents\\NetBeansProjects\\CS2 Assignment 5\\src\\index.html");

リクエストは URL エンコードされているため、アプリケーションへのリクエストからそれを取得していません。

もちろん、より良い解決策は、ファイルをアプリケーションに対して適切な場所に置き、相対的に参照することです。

于 2013-03-29T00:12:22.490 に答える