Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 保存图像的正确位置(spring/jelastic)_Java_Spring_Jelastic - Fatal编程技术网

Java 保存图像的正确位置(spring/jelastic)

Java 保存图像的正确位置(spring/jelastic),java,spring,jelastic,Java,Spring,Jelastic,我正在尝试实现用户配置文件功能,首先我找到了一个上传照片的示例,它将照片保存到应用程序文件夹,如home\jelastic\app\my_folder,但有两个问题: 1) 如果我需要更新应用程序并上传新版本,我想保存这些图像,但我丢失了这些数据 2) 我无法读取照片(由于某种原因,输入流为空) 那么,在jelastic上保存/读取照片的正确方法是什么呢?为什么不看一看呢。这正是你想要做的 假设您使用Spring数据存储每个用户配置文件,您可以将其添加到项目中,如下所示: pom.xml Use

我正在尝试实现用户配置文件功能,首先我找到了一个上传照片的示例,它将照片保存到应用程序文件夹,如
home\jelastic\app\my_folder
,但有两个问题:

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

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


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

为什么不看一看呢。这正是你想要做的

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

pom.xml

User.java

UserContentStore.java

@StoreRestResource(path=“userProfileImages”)
公共接口UserContentStore扩展了ContentStore{
}
这就是获取REST端点所需的全部操作,REST端点将允许您存储和检索与每个用户关联的内容。这实际上是如何工作的,非常类似于Spring数据。当应用程序启动时,Spring内容将看到
Spring内容fs boot starter
依赖项,并知道您希望将内容存储在文件系统上。它还将注入基于文件系统的
UserContentStore
接口实现。它还将看到
spring内容rest启动程序
,并将注入与内容存储接口通信的rest端点。这意味着您不必自己编写任何代码

例如:

curl-xpost/userProfileImages/{userId}-F“file=@/path/to/image.jpg”

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

curl/userProfileImages/{userId}

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


您还可以通过将
spring content fs boot starter
依赖项替换为相应的spring内容存储模块,决定将内容存储在其他位置,如数据库中(如有人评论的),或S3中。每种存储类型的示例如下。

我会将其作为blob存储在数据库中,谢谢。我已经考虑过了,但是看起来更好的方法是使用文件存储:好的,按照这个建议,使用数据库来存储图像的元数据。
public @ResponseBody byte[] getImageWithMediaType() throws IOException {
        InputStream in = getClass()
                .getResourceAsStream(storageService.getPath() + "/1544717586066.jpeg");
        return ByteStreams.toByteArray(in);
    }
   <!-- 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>
@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());
    }
}
@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";

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