我只想使用内存中远程 URL 的 zip 文件,使用 spring Resource
类:
UrlResource url = new UrlResource("https://path.to.TheFile.zip");
System.out.println(url.getFile().lastModified()); //=0, so probably it did not work
结果:
Caused by: java.io.FileNotFoundException: https://path.to.TheFile.zip (The syntax for the filename, directory or media is invalid)
at java.util.zip.ZipFile.open(Native Method) ~[?:1.7.0_51]
at java.util.zip.ZipFile.<init>(ZipFile.java:215) ~[?:1.7.0_51]
at java.util.zip.ZipFile.<init>(ZipFile.java:145) ~[?:1.7.0_51]
at java.util.zip.ZipFile.<init>(ZipFile.java:159) ~[?:1.7.0_51]
这里可能出了什么问题?
我无法使用 url.getInputStream()
,因为我无法将 IS 传递到 ZipFile:
ZipFile(url.getInputStream())//错误
最佳答案
根据 getFile()
的 javadoc : (强调我的)
This implementation returns a File reference for the underlying URL/URI, provided that it refers to a file in the file system.
您应该改用 getInputStream()
。然后,您可以将字节数组存储到本地内存数据结构中(例如,使用 StreamUtils.copyToByteArray
)并对其进行处理(查看 How can I convert byte array to ZIP file )。
为了按照已编辑问题中的建议使用 ZipFile
,您需要将字节数组写入临时本地文件(提示:File.createTempFile 和 FileCopyUtils.copy()
),然后直接从中实例化 ZipFile。
记得在必要时关闭 Stream,并在用完后删除临时文件。
关于java - 如何使用 spring 资源从远程 URL 读取 zip 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28498139/