我正在使用 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
提交
取消