Swing 应用程序中嵌入式 HTTP 服务器的 Java 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1186328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java class for embedded HTTP server in Swing app
提问by CodeAndCats
I wish to embed a very light HTTP server in my Java Swing app which just accepts requests, performs some actions, and returns the results.
我希望在我的 Java Swing 应用程序中嵌入一个非常轻的 HTTP 服务器,它只接受请求、执行一些操作并返回结果。
Is there a very light Java class that I can use in my app which listens on a specified port for HTTP requests and lets me handle requests?
是否有一个非常轻量级的 Java 类可以在我的应用程序中使用,它在指定的端口上侦听 HTTP 请求并让我处理请求?
Note, that I am not looking for a stand-alone HTTP server, just a small Java class which I can use in my app.
请注意,我不是在寻找独立的 HTTP 服务器,只是一个可以在我的应用程序中使用的小型 Java 类。
采纳答案by Ivan Dubrov
Since Java 6, the JDK contains a simple HTTP serverimplementation.
从 Java 6 开始,JDK 包含一个简单的HTTP 服务器实现。
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Executors;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
public class HttpServerDemo {
public static void main(String[] args) throws IOException {
InetSocketAddress addr = new InetSocketAddress(8080);
HttpServer server = HttpServer.create(addr, 0);
server.createContext("/", new MyHandler());
server.setExecutor(Executors.newCachedThreadPool());
server.start();
System.out.println("Server is listening on port 8080" );
}
}
class MyHandler implements HttpHandler {
public void handle(HttpExchange exchange) throws IOException {
String requestMethod = exchange.getRequestMethod();
if (requestMethod.equalsIgnoreCase("GET")) {
Headers responseHeaders = exchange.getResponseHeaders();
responseHeaders.set("Content-Type", "text/plain");
exchange.sendResponseHeaders(200, 0);
OutputStream responseBody = exchange.getResponseBody();
Headers requestHeaders = exchange.getRequestHeaders();
Set<String> keySet = requestHeaders.keySet();
Iterator<String> iter = keySet.iterator();
while (iter.hasNext()) {
String key = iter.next();
List values = requestHeaders.get(key);
String s = key + " = " + values.toString() + "\n";
responseBody.write(s.getBytes());
}
responseBody.close();
}
}
}
Or you can use Jettyfor that purpose. It's quite lightweight and perfectly fits this purpose.
或者您可以为此目的使用Jetty。它非常轻巧,非常适合此目的。
回答by Bhushan Bhangale
You can use jetty as embedded server, its fairly light weight. Other option is check this out for a simple java class to handle http requests http://java.sun.com/developer/technicalArticles/Networking/Webserver/.
您可以使用 jetty 作为嵌入式服务器,它的重量相当轻。其他选项是查看一个简单的 java 类来处理 http 请求http://java.sun.com/developer/technicalArticles/Networking/Webserver/。
Other way is in Java 6 you can use com.sun.net.httpserver.HttpServer
其他方式是在 Java 6 中你可以使用 com.sun.net.httpserver.HttpServer
回答by Brian Agnew
回答by daitangio
Sun embedded web server is useful, but com.sun.net package could be dropped without notice. A better alternative are
Sun 嵌入式 Web 服务器很有用,但 com.sun.net 包可能会被删除,恕不另行通知。更好的选择是
- http://tjws.sourceforge.net/100kb very small and jdk 1.6-aware
- http://winstone.sourceforge.net/bigger but a good shot
- http://www.eclipse.org/jetty/Jetty, very good in developement, support SPDY and websocket
- http://tjws.sourceforge.net/100kb 非常小和 jdk 1.6-aware
- http://winstone.sourceforge.net/更大但很好的镜头
- http://www.eclipse.org/jetty/Jetty,很好的开发,支持SPDY和websocket
回答by amichair
You said "very light" twice, so I think JLHTTPmight be a good match for you. You can embed it as a single source file or a ~35K/50K jar file, yet it supports most functionality you'd need in an HTTP server out of the box.
您说了两次“非常轻”,所以我认为JLHTTP可能很适合您。您可以将其嵌入为单个源文件或 ~35K/50K jar 文件,但它支持开箱即用的 HTTP 服务器中所需的大多数功能。
Disclaimer: I'm the author. But check it out for yourself and see what you think :-)
免责声明:我是作者。但是自己检查一下,看看你的想法:-)