第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

作為jar運(yùn)行時(shí)找不到類路徑資源

作為jar運(yùn)行時(shí)找不到類路徑資源

冉冉說(shuō) 2019-11-07 12:45:53
在Spring Boot 1.1.5和1.1.6中都存在此問題-我正在使用@Value注釋加載類路徑資源,當(dāng)我從STS(3.6.0,Windows)中運(yùn)行應(yīng)用程序時(shí),它的工作正常。但是,當(dāng)我運(yùn)行mvn程序包,然后嘗試運(yùn)行jar時(shí),出現(xiàn)FileNotFound異常。資源message.txt位于src / main / resources中。我已經(jīng)檢查了jar,并確認(rèn)它在頂層(與application.properties相同)包含文件“ message.txt”。這是應(yīng)用程序:@Configuration@ComponentScan@EnableAutoConfigurationpublic class Application implements CommandLineRunner {    private static final Logger logger = Logger.getLogger(Application.class);    @Value("${message.file}")    private Resource messageResource;    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }    @Override    public void run(String... arg0) throws Exception {        // both of these work when running as Spring boot app from STS, but        // fail after mvn package, and then running as java -jar        testResource(new ClassPathResource("message.txt"));        testResource(this.messageResource);    }    private void testResource(Resource resource) {        try {            resource.getFile();            logger.debug("Found the resource " + resource.getFilename());        } catch (IOException ex) {            logger.error(ex.toString());        }    }}例外:c:\Users\glyoder\Documents\workspace-sts-3.5.1.RELEASE\classpath-resource-problem\target>java -jar demo-0.0.1-SNAPSHOT.jar  .   ____          _            __ _ _ /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/  ___)| |_)| | | | | || (_| |  ) ) ) )  '  |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot ::        (v1.1.5.RELEASE)
查看完整描述

3 回答

?
吃雞游戲

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊

resource.getFile()期望資源本身在文件系統(tǒng)上可用,即不能嵌套在jar文件中。這就是為什么當(dāng)您在STS中運(yùn)行應(yīng)用程序時(shí)它可以工作,但是一旦您構(gòu)建了應(yīng)用程序并從可執(zhí)行jar中運(yùn)行它后,它就無(wú)法工作。建議不要使用getFile()來(lái)訪問資源的內(nèi)容getInputStream()。這樣一來(lái),無(wú)論資源位于何處,您都可以閱讀其內(nèi)容。


查看完整回答
反對(duì) 回復(fù) 2019-11-07
?
蠱毒傳說(shuō)

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超3個(gè)贊

如果您使用的是Spring框架,那么使用Spring框架的讀ClassPathResource入String是非常簡(jiǎn)單的FileCopyUtils:


String data = "";

ClassPathResource cpr = new ClassPathResource("static/file.txt");

try {

    byte[] bdata = FileCopyUtils.copyToByteArray(cpr.getInputStream());

    data = new String(bdata, StandardCharsets.UTF_8);

} catch (IOException e) {

    LOG.warn("IOException", e);

}


查看完整回答
反對(duì) 回復(fù) 2019-11-07
?
慕后森

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊

如果要使用文件:


ClassPathResource classPathResource = new ClassPathResource("static/something.txt");


InputStream inputStream = classPathResource.getInputStream();

File somethingFile = File.createTempFile("test", ".txt");

try {

    FileUtils.copyInputStreamToFile(inputStream, somethingFile);

} finally {

    IOUtils.closeQuietly(inputStream);

}


查看完整回答
反對(duì) 回復(fù) 2019-11-07
  • 3 回答
  • 0 關(guān)注
  • 1073 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)