代码之家  ›  专栏  ›  技术社区  ›  Siarhei Oliver Drotbohm

保存图像的正确位置(Spring/Jelastic)

  •  2
  • Siarhei Oliver Drotbohm  · 技术社区  · 6 年前

    我正在努力实现用户配置文件功能,在firts中我找到了一个上传照片的示例,它将其保存到app文件夹中,比如 home\jelastic\app\my_folder 但有两个问题:

    1)如果我需要更新应用程序并上载新版本,我想保存此图像,但我会丢失此数据

    2)无法读取照片(由于某种原因输入流为空)

    public @ResponseBody byte[] getImageWithMediaType() throws IOException {
            InputStream in = getClass()
                    .getResourceAsStream(storageService.getPath() + "/1544717586066.jpeg");
            return ByteStreams.toByteArray(in);
        }
    

    那么,在Jelasti上保存/读取照片的正确方法是什么?

    2 回复  |  直到 6 年前
        1
  •  3
  •   Paul Warren    6 年前

    你为什么不看看呢 Spring Content . 这是为了做你想做的事情。

    假设您使用Spring数据存储每个用户配置文件,您可以将其添加到项目中,如下所示:

    XML

       <!-- Java API -->
       <dependency>
          <groupId>com.github.paulcwarren</groupId>
          <artifactId>spring-content-fs-boot-starter</artifactId>
          <version>0.4.0</version>
       </dependency>
    
       <!-- REST API -->
       <dependency>
          <groupId>com.github.paulcwarren</groupId>
          <artifactId>spring-content-rest-boot-starter</artifactId>
          <version>0.4.0</version>
       </dependency>
    

    文件系统配置.java

    @Configuration
    public class FilesystemConfiguration {
    
        @Bean
        File filesystemRoot() {
            try {
                return new File("/path/to/your/user/profile/image/store");
            } catch (IOException ioe) {}
            return null;
        }
    
        @Bean
        FileSystemResourceLoader fileSystemResourceLoader() {
            return new FileSystemResourceLoader(filesystemRoot().getAbsolutePath());
        }
    }
    

    Java语言

    @Entity
    public class User {
       @Id
       @GeneratedValue
       private long id;
    
       ...other existing fields...
    
       @ContentId
       private String contentId;
    
       @ContentLength
       private long contentLength = 0L;
    
       @MimeType
       private String mimeType = "text/plain";
    
       ...
    }
    

    用户内容存储.java

    @StoreRestResource(path="userProfileImages")
    public interface UserContentStore extends ContentStore<User, String> {
    }
    

    这是获得REST端点所需要做的全部工作,这些端点将允许您存储和检索与每个用户关联的内容。这实际上是如何工作的,非常类似于Spring数据。当应用程序启动时,Spring内容将看到 spring-content-fs-boot-starter 依赖关系,并知道要在文件系统上存储内容。它还将注入一个基于文件系统的 UserContentStore 接口。它还将看到 spring-content-rest-boot-starter 并将注入与内容存储接口对话的REST端点。也就是说,您不必自己编写任何代码。

    例如:

    curl -X POST /userProfileImages/{userId} -F "file=@/path/to/image.jpg"

    将图像存储在文件系统上,并将其与ID为的用户实体关联 userId .

    curl /userProfileImages/{userId}

    将再次获取它等等…实际上也支持完整的crud和视频流。

    您还可以通过交换 Spring Content FS启动程序 依赖于适当的Spring内容存储模块。每种存储类型的示例包括 here .

        2
  •  2
  •   Ruslan    6 年前

    如果你 redeploy the whole container 下面的文章有助于组织媒体文件的存储: 5 Ways to Store Data with Containers Data Storage Container Overview . 一般的想法是制作一个卷(本地或远程)。

    如果您通过重新部署应用程序 Deployment Manager 例如,只需将文件存储在应用程序文件夹之外 /home/jelastic/myfiles .但是,如果要存储大量数据,最好使用单独的数据存储容器。