在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:NanoHttpd/nanohttpd开源软件地址:https://github.com/NanoHttpd/nanohttpd开源编程语言:Java 98.4%开源软件介绍:NanoHTTPD – a tiny web server in JavaNanoHTTPD is a light-weight HTTP server designed for embedding in other applications, released under a Modified BSD licence. It is being developed at Github and uses Apache Maven for builds & unit testing: QuickstartWe'll create a custom HTTP server project using Maven for build/dep system. This tutorial assumes you are using a Unix variant and a shell. First, install Maven and Java SDK if not already installed. Then run:
You should now have a HTTP file server running on http://localhost:8080/. Custom web appLet's raise the bar and build a custom web application next:
Edit
Edit package com.example;
import java.io.IOException;
import java.util.Map;
import fi.iki.elonen.NanoHTTPD;
// NOTE: If you're using NanoHTTPD >= 3.0.0 the namespace is different,
// instead of the above import use the following:
// import org.nanohttpd.NanoHTTPD;
public class App extends NanoHTTPD {
public App() throws IOException {
super(8080);
start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
System.out.println("\nRunning! Point your browsers to http://localhost:8080/ \n");
}
public static void main(String[] args) {
try {
new App();
} catch (IOException ioe) {
System.err.println("Couldn't start server:\n" + ioe);
}
}
@Override
public Response serve(IHTTPSession session) {
String msg = "<html><body><h1>Hello server</h1>\n";
Map<String, String> parms = session.getParms();
if (parms.get("username") == null) {
msg += "<form action='?' method='get'>\n <p>Your name: <input type='text' name='username'></p>\n" + "</form>\n";
} else {
msg += "<p>Hello, " + parms.get("username") + "!</p>";
}
return newFixedLengthResponse(msg + "</body></html>\n");
}
} Compile and run the server:
If it started ok, point your browser at http://localhost:8080/ and enjoy a web server that asks your name and replies with a greeting. NanoletsNanolets are like servlets only that they have a extremely low profile. They offer an easy to use system for a more complex server application. This text has to be extended with an example, so for now take a look at the unit tests for the usage. https://github.com/NanoHttpd/nanohttpd/blob/master/nanolets/src/test/java/org/nanohttpd/junit/router/AppNanolets.java StatusWe are currently in the process of stabilizing NanoHTTPD from the many pull requests and feature requests that were integrated over the last few months. The next release will come soon, and there will not be any more "intended" major changes before the next release. If you want to use the bleeding edge version, you can clone it from Github, or get it from sonatype.org (see "Maven dependencies / Living on the edge" below). Project structureNanoHTTPD project currently consist of four parts:
FeaturesCore
Websocket
Webserver
CORS argument examples
Maven dependenciesNanoHTTPD is a Maven based project and deployed to central. Most development environments have means to access the central repository. The coordinates to use in Maven are:
(Replace The coordinates for your development environment should correspond to these. When looking for an older version take care because we switched groupId from com.nanohttpd to org.nanohttpd in mid 2015. Next it depends what you are using NanoHTTPD for, there are three main usages. Gradle dependenciesIn gradle you can use NanoHTTPD the same way because gradle accesses the same central repository:
(Replace Just replace the name with the artifact id of the module you want to use and gradle will find it for you. Develop your own specialized HTTP serviceFor a specialized HTTP (HTTPS) service you can use the module with artifactId nanohttpd.
Here you write your own subclass of org.nanohttpd.NanoHTTPD to configure and to serve the requests. Develop a websocket based serviceFor a specialized websocket service you can use the module with artifactId nanohttpd-websocket.
Here you write your own subclass of org.nanohttpd.NanoWebSocketServer to configure and to serve the websocket requests. A small standard echo example is included as org.nanohttpd.samples.echo.DebugWebSocketServer. You can use it as a starting point to implement your own services. Develop a custom HTTP file serverFor a more classic approach, perhaps to just create a HTTP server serving mostly service files from your disk, you can use the module with artifactId nanohttpd-webserver.
The included class org.nanohttpd.SimpleWebServer is intended to be used as a starting point for your own implementation but it also can be used as is. Starting the class as is will start a HTTP server on port 8080 and publishing the current directory. Living on the edgeThe latest Github master version can be fetched through sonatype.org:
generating an self signed SSL certificateJust a hint how to generate a certificate for localhost.
This will generate a keystore file named 'keystore.jks' with a self signed certificate for a host named localhost with the IP address 127.0.0.1 . Now you can use:
Before you start the server to make NanoHTTPD serve HTTPS connections, when you make sure 'keystore.jks' is in your classpath. Thank you to everyone who has reported bugs and suggested fixes. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论