Java 本地主机没有’;不发送任何数据。-错误\u空\u响应
但是要检查一下,localhost没有正确响应,它说“页面没有发送任何数据。我尝试到处查找,并尽可能更改了所有可能的设置 恢复chrome设置清除缓存、清除历史记录、更改端口号、Java 本地主机没有’;不发送任何数据。-错误\u空\u响应,java,Java,但是要检查一下,localhost没有正确响应,它说“页面没有发送任何数据。我尝试到处查找,并尽可能更改了所有可能的设置 恢复chrome设置清除缓存、清除历史记录、更改端口号、 import java.io.*; import java.net.*; import java.util.*; import java.net.InetAddress.*; public final class WebServer { public static void main(String args[
import java.io.*;
import java.net.*;
import java.util.*;
import java.net.InetAddress.*;
public final class WebServer
{
public static void main(String args[]) throws Exception
{
// Set port number
int port = 0;
// Establish the listening socket
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Port number is: "+serverSocket.getLocalPort());
// Wait for and process HTTP service requests
while (true) {
// Wait for TCP connection
Socket requestSocket = serverSocket.accept();
// Create an object to handle the request
HttpRequest request = new HttpRequest(requestSocket);
//request.run()
// Create a new thread for the request
Thread thread = new Thread(request);
// Start the thread
thread.start();
}
}
}
final class HttpRequest implements Runnable
{
// Constants
// Recognized HTTP methods
final static class HTTP_METHOD
{
final static String GET = "GET";
final static String HEAD = "HEAD";
final static String POST = "POST";
}
final static String HTTPVERSION = "HTTP/1.1";
final static String CRLF = "\r\n";
Socket socket;
// Constructor
public HttpRequest(Socket socket) throws Exception
{
this.socket = socket;
}
// Implements the run() method of the Runnable interface
public void run()
{
try {
processRequest();
} catch (Exception e) {
System.out.println(e);
}
}
// Process a HTTP request
private void processRequest() throws Exception
{
// Get the input and output streams of the socket.
InputStream ins = socket.getInputStream();
DataOutputStream outs = new DataOutputStream(socket.getOutputStream());
// Set up input stream filters
BufferedReader br = new BufferedReader(new InputStreamReader(ins));
// Get the request line of the HTTP request
String requestLine = br.readLine();
// Display the request line
System.out.println();
System.out.println("Request:");
System.out.println(" " + requestLine);
// Close streams and sockets
outs.close();
br.close();
socket.close();
}
private static void sendBytes(FileInputStream fins,
OutputStream outs) throws Exception
{
// Coopy buffer
byte[] buffer = new byte[1024];
int bytes = 0;
while ((bytes = fins.read(buffer)) != -1) {
outs.write(buffer, 0, bytes);
}
}
private static String contentType(String fileName)
{
if (fileName.toLowerCase().endsWith(".htm") ||
fileName.toLowerCase().endsWith(".html")) {
return "text/html";
} else if (fileName.toLowerCase().endsWith(".gif")) {
return "image/gif";
} else if (fileName.toLowerCase().endsWith(".jpg")) {
return "image/jpeg";
} else {
return "application/octet-stream";
}
}
}
请尽快帮助我。我需要此代码方面的帮助。发布的代码有一个方法
sendBytes
显然可以将数据发送回,但它没有被调用-没有数据被发送回-消息是正确的
对于每个连接(尝试),代码只读取一条发送的行,将其打印到标准输出并关闭连接。如果我没有错,浏览器希望收到回复(肯定或否定/错误),但由于连接关闭时没有任何回复,浏览器必须显示未发送任何回复的消息