我正在尝试使用NanoHTTPD在桌面上提供一个小文件。服务器启动正常但由于某些未知原因,它无法提供文件。相同的程序在Android中运行良好。谁能给我一些指示?这已经超过一个小时,但我一点也不清楚。这是我的桌面版NanoHTTPD服务器:
package com.desktopserver;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLConnection;
import java.util.Map;
import com.desktopserver.NanoHTTPD.Response.Status;
public class MainClass {
static int PORT = 8080;
static WebServer MyServer;
static FileInputStream fis;
static BufferedInputStream bis;
public static void main(String[] args) {
MyServer = new WebServer();
try {
MyServer.start();
System.out.println("Webserver Started @ PORT:8080");
} catch (IOException e) {
e.printStackTrace();
}
}
public static class WebServer extends NanoHTTPD {
String MIME_TYPE;
public WebServer() {
super(PORT);
}
@Override
public Response serve(String uri, Method method,
Map<String, String> header, Map<String, String> parameters,
Map<String, String> files) {
try {
File file=new File("/home/evinish/Music/Meant_to_live.mp3");
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
MIME_TYPE= URLConnection.guessContentTypeFromName(file.getName());
System.out.println("\nMIME TYPE: "+MIME_TYPE);
System.out.println("\nFILE NAME: "+file.getName());
} catch (IOException ioe) {
System.out.println("File IO Exception");
}
return new NanoHTTPD.Response(Status.OK, MIME_TYPE, bis);
}
}
}
我确实得到了这个输出,但就是这样:
Webserver Started @ PORT:8080
我在这里缺少什么?非常感谢你的帮助。
答案 0 :(得分:0)
因为您没有使用&#34; ServerRunner&#34;类。 ServerRunner将服务器保持到任何键按下。
但在实际应用中,您无需在NanoHTTPd文件中进行一些更改
第196行到
myThread.setDaemon(false);