我正在使用 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); }}@Overridepublic 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>
1 回答
慕尼黑8549860
TA贡献1818条经验 获得超11个赞
找出解决方案是:
InputStream in = this.getClass().getClassLoader().getResourceAsStream("web" + session.getUri());
添加回答
举报
0/150
提交
取消