【发布时间】:2019-05-20 07:53:15
【问题描述】:
我已经编写了一个简单的 HTTP 服务器和一个客户端。我需要实现简单的 GET 方法并传输一个文件名,我想在我的服务器页面和控制台中打印它。我写了,但它不起作用。
我将一个字符串传输到服务器,然后服务器正在解析,尝试找出文件名。我用substring。
我有一个名为“site.html”的简单 html 页面,我需要打印出来。
public class Client {
public static void main(String[] args) throws IOException{
System.out.println("Enter IP and port: ");
Scanner in = new Scanner(System.in);
String ip = in.next();
int port = in.nextInt();
System.out.println("Enter name of the file");
String name_of_File = in.next();
System.out.println(name_of_File); //no usage
Socket clientSocket = new Socket(InetAddress.getByName(ip), port);
Client client = new Client(clientSocket);
client.writeOutputStream(name_of_File);
client.readInputStream();
}
private Socket socket;
private InputStream inputStream;
private OutputStream outputStream;
public Client(Socket socket) throws IOException {
this.socket = socket;
this.inputStream = socket.getInputStream();
this.outputStream = socket.getOutputStream();
}
public void writeOutputStream(String fileName) throws IOException { //getter
String getter = "GET / HTTP/1.1\n" +"File: " + fileName + ":"+"\n\n";
outputStream.write(getter.getBytes());
outputStream.flush();
}
public void readInputStream() throws IOException { //console output from server
Scanner scan = new Scanner(inputStream);
String str;
while (scan.hasNextLine()){
str = scan.nextLine();
System.out.println(str);
}
}
}
这里有服务器
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8080);
while (true) {
System.out.println("Waiting for a client connection...");
Socket clientSocket = serverSocket.accept();
Server server = new Server(clientSocket);
System.out.println("Client has connected successfully");
server.readInputStream();
server.writeOutputStream();
server.clientSocket.close();
}
}
public Socket clientSocket;
private InputStream inputStream;
private OutputStream outputStream;
private String fileName;
public Server(Socket clientSocket) throws IOException {
this.clientSocket = clientSocket;
this.inputStream = clientSocket.getInputStream();
this.outputStream = clientSocket.getOutputStream();
// this.fileName = "site.html";
}
public void readInputStream() throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder sb = new StringBuilder();
sb.append(in.readLine()); //first line is a method(GET-method here)
while (in.readLine() != null || in.readLine().trim().length() != 0) {
String str = in.readLine();
sb.append(str);
if(str.contains(".html")) {
fileName = str.substring(str.indexOf("File:") + 5, str.length() - str.lastIndexOf("html") + 4);
System.out.println(fileName);
}
}
}
//String name_of_File = in.findInLine(Pattern.compile(".....html"));
public void writeOutputStream() throws IOException {
File file = new File(fileName);
if (file.exists()) { //reading "site.html"
String s = new String(Files.readAllBytes(Paths.get(fileName)));
String response = "HTTP/1.1 200 OK\n" +
"Type: text/html\n" +
"Length: " + s.length() + "\n" +
"Closing connection\n\n" + s;
outputStream.write(response.getBytes());
outputStream.flush();
}
else {
outputStream.write("<html><h2>404:file not found</h2></html>".getBytes());
outputStream.flush();
}
}
}
我希望看到打印的页面,但没有发生。
【问题讨论】: