我已经编写了一个简单的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();
}
}
}
我希望看到打印的页面,但不会发生。
答案 0 :(得分:0)
问题出在服务器上的readLine()方法下的readInputStream()方法下。在“ HTTP / 1.1 \ n”之后,它不会超出字符串中的换行符(\n
)的字符。
在这里,我已经为readInputStream()
方法修改了您的代码。我正在使用read()
方法,并且还更改了子字符串的索引。在此之前,您的代码试图在start index: 21
和end index: 8
上显示子字符串,因为您要从字符串长度中减去结束索引。修改后的方法:
public void readInputStream() throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder sb = new StringBuilder();
int i;
// read the inputStream character by character
while ((i = in.read()) != -1) {
char ch = (char)i;
String str = Character.toString(ch);
sb.append(str);
str = sb.toString();
if(str.contains(".html")) {
// modified substring indices
fileName = str.substring(str.indexOf("File:") + 6, str.lastIndexOf("html") + 4);
System.out.println("\n"+fileName);
break;
}
}
}
SERVER端的输出如下所示:
Waiting for a client connection...
Client has connected successfully
Entered the readInputStream() method.
site.html
Waiting for a client connection...
CLIENT端的输出如下:
Enter IP and port:
127.0.0.1
8080
Enter name of the file
site.html
GET / HTTP/1.1
File: site.html:
<html><h2>404:file not found</h2></html>
Process finished with exit code 0
如果在正确的位置创建了文件路径,则文件路径应在您的系统上正常工作。