如何使用 Spring 加载 UrlResource?

How to load an UrlResource with Spring?

我正在尝试使用 UrlResource:

加载 HTTP zip 文件
new UrlResource("url:http://www.my-path.to/file.zip")

结果。

Caused by: java.io.FileNotFoundException: URL [http://www.my-path.to/file.zip] cannot be resolved to absolute file path because it does not reside in the file system.

使用 Resource 接口加载 HTTP 文件的正确方法是什么?

稍后我想将该资源添加到 Spring 批处理的 ItemReader

您需要使用 http: 前缀而不是 url: 来引用带有 UrlResource 的 HTTP 资源。

引用自Spring documentation

All URLs have a standardized String representation, such that appropriate standardized prefixes are used to indicate one URL type from another. This includes file: for accessing filesystem paths, http: for accessing resources via the HTTP protocol, ftp: for accessing resources via FTP, etc.

因此,您将只有 new UrlResource("http://www.my-path.to/file.zip").