如题,碰到了问题.
事情是这样的. 一个导入模板, 因为比较少, 所以就直接放在后台的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相关的源代码, 感觉不错~~~,以后有时间在深究一下.