亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

運行 jar 時無法將資源作為流讀取

運行 jar 時無法將資源作為流讀取

紅糖糍粑 2023-05-10 14:14:06
我正在使用 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());


查看完整回答
反對 回復 2023-05-10
  • 1 回答
  • 0 關注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號