java17使用http2的示例
时间: 2023-07-11 13:11:09 浏览: 51
Java 17 中已经内置支持 HTTP/2,可以使用 JDK 自带的 HttpServer 来创建一个 HTTP/2 的服务端。下面是一个简单的示例代码:
```java
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpsConfigurator;
import com.sun.net.httpserver.HttpsServer;
import com.sun.net.httpserver.HttpsParameters;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.security.KeyStore;
public class Http2Server {
public static void main(String[] args) throws Exception {
int port = 8443;
String keystorePath = "/path/to/keystore.jks";
String keystorePassword = "password";
SSLContext sslContext = SSLContext.getInstance("TLS");
// Load the keystore
KeyStore keyStore = KeyStore.getInstance("JKS");
InputStream inputStream = new FileInputStream(keystorePath);
keyStore.load(inputStream, keystorePassword.toCharArray());
// Initialize the key manager factory
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(keyStore, keystorePassword.toCharArray());
// Initialize the trust manager factory
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(keyStore);
// Initialize the SSL context
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
// Create the HTTPS server
HttpsServer httpsServer = HttpsServer.create(new InetSocketAddress(port), 0);
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext) {
public void configure(HttpsParameters params) {
try {
// Initialize the SSL context
SSLContext sslContext = SSLContext.getDefault();
SSLEngine engine = sslContext.createSSLEngine();
params.setNeedClientAuth(false);
params.setCipherSuites(engine.getEnabledCipherSuites());
params.setProtocols(engine.getEnabledProtocols());
// Get the default parameters
SSLParameters defaultSSLParameters = sslContext.getDefaultSSLParameters();
params.setSSLParameters(defaultSSLParameters);
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
// Create the HTTP server
HttpServer httpServer = HttpServer.create(new InetSocketAddress(port), 0);
// Set the HTTP handler
httpServer.createContext("/", new MyHttpHandler());
// Set the HTTPS handler
httpsServer.createContext("/", new MyHttpsHandler());
// Start the servers
httpServer.start();
httpsServer.start();
System.out.println("Server started on port " + port);
}
static class MyHttpHandler implements HttpHandler {
public void handle(HttpExchange exchange) throws IOException {
String response = "Hello, HTTP/2!";
exchange.sendResponseHeaders(200, response.getBytes().length);
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
static class MyHttpsHandler implements HttpsHandler {
public void handle(HttpExchange exchange) throws IOException {
String response = "Hello, HTTPS/2!";
exchange.sendResponseHeaders(200, response.getBytes().length);
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
}
```
在该示例中,我们使用 HttpServer 和 HttpsServer 类来创建一个 HTTP/2 的服务端。我们通过加载一个 keystore 来设置 HTTPS 服务的 SSL 上下文,并且将 HTTP 和 HTTPS 的请求分别交给 MyHttpHandler 和 MyHttpsHandler 来处理。当收到请求时,这两个处理器会分别返回 "Hello, HTTP/2!" 和 "Hello, HTTPS/2!" 的响应。