我正在使用 NanoHTTPD 作为我的应用程序服务器,并且在 IDE 中一切正常。当我运行相同的代码形式 jar(使用 maven-assembly-plugin 生成)时,资源未加载。
我的应用服务器代码:
public class HintTestAppServer extends NanoHTTPD {
private HintTestAppServer() throws IOException {
super(8000);
start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
System.out.println("Running on port " + super.getListeningPort() + "\n");
}
public static void main(String[] args) {
try {
new HintTestAppServer();
} catch (IOException ioe) {
System.err.println("Couldn't start server:\n" + ioe);
}
}
@Override
public Response serve(IHTTPSession session) {
//below line always return null fot the buffered input stream
BufferedInputStream bufferedInputStream = (BufferedInputStream)
Thread.currentThread().getContextClassLoader().getResourceAsStream("web/" + session.getUri());
String mimeType;
try {
mimeType = URLConnection.guessContentTypeFromStream(bufferedInputStream);
} catch (IOException e) {
e.printStackTrace();
return newFixedLengthResponse("ERROR");
}
try {
return newFixedLengthResponse(Response.Status.OK, mimeType, bufferedInputStream, bufferedInputStream.available());
} catch (IOException e) {
e.printStackTrace();
return newFixedLengthResponse("ERROR");
}
}
}
我的 pom.xml 程序集插件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>server.HintTestAppServer</mainClass>
</manifest>
</archive>
</configuration>
慕尼黑8549860
相关分类