运行 jar 时无法将资源作为流读取

Cannot read resource as stream when running jar

我使用 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>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

我用于 运行 jar 的命令是:

java -cp target/hint-test-1.0.1-jar-with-dependencies.jar server.HintTestAppServer

我还尝试通过以下方式加载资源:(在 IDE 上工作但不在 jar 中)

BufferedInputStream bufferedInputStream = (BufferedInputStream)
getClass().getClassLoader().getResourceAsStream("web/" + session.getUri());

当我解压缩 jar 文件时,我在根文件夹中看到了包含我所有文件的 Web 文件夹。

请协助

找出解决方案是:

InputStream in = this.getClass().getClassLoader().getResourceAsStream("web" + session.getUri());