Springboot讀取Jar文件中的resource


如題,碰到了問題.

事情是這樣的. 一個導入模板, 因為比較少, 所以就直接放在后台的resources中了.調試的時候是下載沒有問題的.

等到發布后,下載就出問題了. 

參照:

 
         

***.jar!\BOOT-INF\classes!\***.xml沒有此文件
https://blog.csdn.net/weixin_43229107/article/details/85318551
通過 this.getClass().getResourceAsStream("/jdbcType.xml");

 
         

java工程內部文件路徑讀取問題jar:file:\No such file or directory
https://blog.csdn.net/ccmedu/article/details/78783248
URL classPath = Thread.currentThread().getContextClassLoader().getResource("xxxxx");

 
         


還有直接注入Resource的
@Value("classpath:thermopylae.txt")
private Resource res;
http://zetcode.com/articles/springbootloadres/

 
         


還有使用ResourceLoader來做的
@Autowired
private ResourceLoader resourceLoader;

 
         

Resource resource = resourceLoader.getResource("classpath:GeoLite2-Country.mmdb");

 
         

https://smarterco.de/java-load-file-from-classpath-in-spring-boot/
http://zetcode.com/articles/springbootloadres/
https://howtodoinjava.com/spring-core/how-to-load-external-resources-files-into-spring-context/

 

 

其實問題的關鍵在, jar中的文件訪問不能使用resource.getFile, 而必須使用getInputStream.

訪問磁盤可以用前者, 而訪問jar內文件, 必須使用getInputStream().

不管是通過getClass().getxxx還是使用classLoader的getXXX也好, 都是要使用getInputStream這種.

 

另外發現一個問題點: 以前把文件讀進來, 然后要寫入outputStream, 使用了

byte[] data = new byte[fis.available()];

fis.available()這個東西給壞了事情.

try (BufferedInputStream fis = new BufferedInputStream(res.getInputStream())) {

                int offset = 0;
                int bytesRead = 0;
                byte[] data = new byte[fis.available()];
                while ((bytesRead = fis.read(data, offset, data.length - offset))
                        != -1) {
                    offset += bytesRead;
                    if (offset >= data.length) {
                        break;
                    }
                }
                //String str = new String(data, 0, offset, "UTF-8");
                response.setHeader("Content-Disposition", "attachment; filename=" + res.getFilename());
                response.setHeader("Access-Control-Allow-Origin", "*");
                //response.setContentType("application/vnd.ms-read; charset=utf-8");
                response.setContentType("application/octet-stream");
                try (OutputStream outStream = new BufferedOutputStream(response.getOutputStream())) {
                    outStream.write(data);
                    outStream.flush();
                }
            }

看了一下JDK里inputstream的注釋
    /**
     * Returns an estimate of the number of bytes that can be read (or
     * skipped over) from this input stream without blocking by the next
     * invocation of a method for this input stream. The next invocation
     * might be the same thread or another thread.  A single read or skip of this
     * many bytes will not block, but may read or skip fewer bytes.
     *
 * <p> Note that while some implementations of {@code InputStream} will return * the total number of bytes in the stream, many will not. It is * never correct to use the return value of this method to allocate * a buffer intended to hold all data in this stream.
     *
     * <p> A subclass' implementation of this method may choose to throw an
     * {@link IOException} if this input stream has been closed by
     * invoking the {@link #close()} method.
     *
     * <p> The {@code available} method for class {@code InputStream} always
     * returns {@code 0}.
     *
     * <p> This method should be overridden by subclasses.
     *
     * @return     an estimate of the number of bytes that can be read (or skipped
     *             over) from this input stream without blocking or {@code 0} when
     *             it reaches the end of the input stream.
     * @exception  IOException if an I/O error occurs.
     */
    public int available() throws IOException {
        return 0;
    }

里面有顏色的字, 意思大致是, 有人的實現會放進一個total number of bytes, 但很多不是....

看來JDK作者對大家的各種實現還是做了很多調查的...比較無奈, 大家都沒有統一步調實現, (有人偷懶了,但是很有名,不能說, 推測, 所以含蓄地指出來)

 

這次我的這個文件比較小, 所以也就不用buffer, 直接暴力, 把inputstream轉到byte array, 寫入outputstream中.多么簡單地代碼!

最后代碼就這么簡單

            Resource res = new ClassPathResource(xxxxxxxxx, this.getClass());
            logger.info(res.getURL().toString());
            try (InputStream fis = res.getInputStream()) {
                logger.info("available_length:" + fis.available());
                byte[] data = IOUtils.toByteArray(fis);
                logger.info("data_length:" + data.length);
                response.setHeader("Content-Disposition", "attachment; filename=" + res.getFilename());
                response.setHeader("Access-Control-Allow-Origin", "*");
                response.setContentType("application/octet-stream");
                try (OutputStream outStream = new BufferedOutputStream(response.getOutputStream())) {
                    outStream.write(data);
                    outStream.flush();
                }
            }
IOUtils.toByteArray(fis) 是apache common的方法.
解決!

這次, 在解決的過程中了解到了多種取Resource的方法, 我這里用的是class 的newClassPathResource("xxxxx",this.getClass())
這里的classPath默認是相對this.getClass的路徑.如果需要絕對路徑, 就需要加個/, 代表從根開始找.


另外,還參考了MyBatis加載Mapper.xml的過程, 使用的加載方式很獨特, 特意用了一把, 挺不錯!
大致是這樣寫的, 看樣子很不錯, 因為用的是 classpath*:, 瞬間好像高大上了, 和mybatis作者平起平坐了~~~~
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources("classpath*:abc/sadfsa/ssss.xxx");
Resource res = resources[0];

讀了一下PathMatchingResourcePatternResolver相關的源代碼, 感覺不錯~~~,以后有時間在深究一下.





 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM