java gui 接收http请求 写一段代码给我
时间: 2024-01-20 22:04:31 浏览: 98
好的,以下是一个简单的 Java GUI 程序,可以通过该程序启动一个基于 ServerSocket 的 HTTP 服务器,并在窗口中展示服务器的运行状态:
```java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class HttpServerGui extends JFrame implements ActionListener {
private JTextArea logArea;
private JButton startButton, stopButton;
private JTextField portField;
private ServerSocket serverSocket;
private Thread serverThread;
public HttpServerGui() {
// 设置窗口标题
super("HTTP Server");
// 设置窗口大小
setSize(500, 300);
// 创建窗口组件
logArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(logArea);
startButton = new JButton("Start");
startButton.addActionListener(this);
stopButton = new JButton("Stop");
stopButton.addActionListener(this);
portField = new JTextField("8080");
// 将组件添加到窗口中
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(scrollPane, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.add(startButton);
buttonPanel.add(stopButton);
buttonPanel.add(new JLabel("Port:"));
buttonPanel.add(portField);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
// 设置窗口可见性
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startButton) {
startServer();
} else if (e.getSource() == stopButton) {
stopServer();
}
}
private void startServer() {
try {
// 创建 ServerSocket 对象
serverSocket = new ServerSocket(Integer.parseInt(portField.getText()));
log("HTTP server started on port " + serverSocket.getLocalPort() + ".");
// 创建服务器线程
serverThread = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
// 接收客户端请求
Socket clientSocket = serverSocket.accept();
log("Client connected from " + clientSocket.getRemoteSocketAddress() + ".");
// 读取请求数据
BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String requestLine = reader.readLine();
log("Request line: " + requestLine);
// 响应客户端请求
String response = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nHello, world!";
OutputStream output = clientSocket.getOutputStream();
output.write(response.getBytes());
// 关闭连接
output.close();
reader.close();
clientSocket.close();
log("Connection closed.");
} catch (IOException ex) {
log("Error: " + ex.getMessage());
}
}
}
});
serverThread.start();
} catch (IOException ex) {
log("Error: " + ex.getMessage());
}
}
private void stopServer() {
try {
// 停止服务器线程
if (serverThread != null) {
serverThread.interrupt();
serverThread.join();
serverThread = null;
}
// 关闭 ServerSocket
if (serverSocket != null) {
serverSocket.close();
serverSocket = null;
}
log("HTTP server stopped.");
} catch (IOException ex) {
log("Error: " + ex.getMessage());
} catch (InterruptedException ex) {
// ignore
}
}
private void log(String message) {
logArea.append(message + "\n");
}
public static void main(String[] args) {
new HttpServerGui();
}
}
```
注意:上述代码仅为演示程序,不适用于生产环境。在实际应用中,需要根据具体业务需求进行优化和改进。
阅读全文