一、什麼是nanohttpd
nanohttpd是一個輕量級的Java HTTP服務器,它可以在Java應用程序中嵌入並提供Web服務。
nanohttpd的主要特點包括:
1、非常小巧,代碼量不到2000行。
2、支持HTTPS。
3、支持WebSocket。
4、可以方便地擴展。
5、易於嵌入到任何應用中。
二、如何使用nanohttpd
我們來看一個簡單的例子,實現在瀏覽器上輸出”Hello World”:
import java.io.IOException;
import fi.iki.elonen.NanoHTTPD;
public class SimpleServer extends NanoHTTPD {
public SimpleServer(int port) {
super(port);
}
@Override
public Response serve(IHTTPSession session) {
String response = "Hello World!";
return newFixedLengthResponse(response);
}
public static void main(String[] args) {
SimpleServer server = new SimpleServer(8080);
try {
server.start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
} catch (IOException e) {
e.printStackTrace();
}
}
}
運行程序,打開瀏覽器,訪問http://localhost:8080/,就能看到”Hello World”了。
三、使用nanohttpd處理GET和POST請求
為了處理GET和POST請求,我們需要根據IHTTPSession中的參數來判斷請求的類型。
下面是一個處理GET和POST請求的例子:
import java.io.IOException;
import java.util.Map;
import fi.iki.elonen.NanoHTTPD;
public class GetPostServer extends NanoHTTPD {
public GetPostServer(int port) {
super(port);
}
@Override
public Response serve(IHTTPSession session) {
String response = "";
Method method = session.getMethod();
switch (method) {
case GET:
Map getParms = session.getParms();
if (getParms.containsKey("name")) {
String name = getParms.get("name");
response += "Hello " + name + "!
";
} else {
response += "\n" +
" \n" +
" \n" +
" \n" +
"";
}
break;
case POST:
Map postParms = new HashMap();
try {
session.parseBody(postParms);
} catch (IOException e) {
e.printStackTrace();
} catch (ResponseException e) {
e.printStackTrace();
}
if (postParms.containsKey("name")) {
String name = postParms.get("name");
response += "Hello " + name + "!
";
} else {
response += "\n" +
" \n" +
" \n" +
" \n" +
"";
}
break;
default:
response += "Unsupported method: " + method + "
";
break;
}
response += "";
return newFixedLengthResponse(response);
}
public static void main(String[] args) {
GetPostServer server = new GetPostServer(8080);
try {
server.start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
} catch (IOException e) {
e.printStackTrace();
}
}
}
運行程序,瀏覽器訪問http://localhost:8080/,輸入名字並提交,就能看到”Hello xxx!”了。
四、nanohttpd的擴展
nanohttpd的擴展非常方便,只需要在serve方法中進行相應的處理即可。
下面是一個例子:在瀏覽器上上傳文件,並將上傳的文件保存到本地。
import fi.iki.elonen.NanoHTTPD;
import java.io.File;
import java.io.IOException;
public class UploadServer extends NanoHTTPD {
public UploadServer(int port) {
super(port);
}
@Override
public Response serve(IHTTPSession session) {
String response = "";
Method method = session.getMethod();
switch (method) {
case GET:
response += "\n" +
" \n" +
" \n" +
"";
break;
case POST:
try {
session.parseBody(new HashMap());
} catch (IOException e) {
e.printStackTrace();
} catch (ResponseException e) {
e.printStackTrace();
}
Map files = session.getParms();
Iterator it = files.values().iterator();
while (it.hasNext()){
File file = new File(it.next().toString());
if (file.exists()) {
response += "File uploaded: " + file.getAbsolutePath() + "
";
} else {
response += "File not uploaded: " + file.getAbsolutePath() + "
";
}
}
break;
default:
response += "Unsupported method: " + method + "
";
break;
}
response += "";
return newFixedLengthResponse(response);
}
public static void main(String[] args) {
UploadServer server = new UploadServer(8080);
try {
server.start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
} catch (IOException e) {
e.printStackTrace();
}
}
}
運行程序,瀏覽器上傳文件後,文件將被保存到程序所在目錄下,並在瀏覽器上輸出上傳的文件路徑。
五、nanohttpd的使用範圍
nanohttpd是一個輕量級的Java HTTP服務器,它可以方便地嵌入到任何Java應用程序中提供Web服務。
它適用於以下場景:
1、輕量級Web服務:如果您需要一個簡單的Web服務,nanohttpd是一個非常好的選擇。
2、嵌入式Web應用程序:如果您需要將Web服務嵌入到Java應用程序中,nanohttpd是非常適合的。
3、測試用例:如果您需要針對某些Web服務編寫測試用例,nanohttpd可以為您提供一個Web服務的測試環境。
六、小結
nanohttpd是一個輕量級的Java HTTP服務器,它可以方便地嵌入到任何Java應用程序中提供Web服務,並且支持HTTPS和WebSocket。
在使用nanohttpd時,我們可以方便地處理GET和POST請求,並且可以通過簡單的擴展實現諸如上傳文件等功能。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/190640.html
微信掃一掃
支付寶掃一掃