Senio,如果您使用的是 Spring Boot,那么有一个名为 Spring Content 的项目将允许您用很少的代码行创建一个图像存储。
您需要做的就是添加以下 Spring Content 依赖项(假设是 maven):-
<dependencies>
<!-- Standard Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.3</version>
</dependency>
<!-- Spring Content -->
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-fs-boot-starter</artifactId>
<version>0.0.10</version>
</dependency>
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-rest-boot-starter</artifactId>
<version>0.0.10</version>
</dependency>
</dependencies>
在您的 Spring Boot Application 类中,创建一个 ImageStore 接口。将其注释为 REST 资源。这会导致 Spring Content 注入一个实现(文件系统的此接口)以及 REST 端点,从而使您不必自己编写任何此代码:-
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@StoreRestResource(path="images")
public interface ImageStore extends Store<String> {}
}
默认情况下,Spring Content FS 会在 java.io.tmpdir 下创建一个 store。因此,您还需要将 SPRING_CONTENT_FS_FILESYSTEM_ROOT 环境变量设置为指向“存储”的根目录; c:\临时\资源\图像。
启动应用程序,您将能够通过 POST(或 PUTting)将图像上传到:-
/images/some/path/image-1.jpg
(这也支持GET(下载)和DELETE。)
您会在 c:\temp\resources\images\some\path\image-1.jpg 下找到上传的图片
HTH