Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.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显示用户刚刚上载的图像,但未重新启动服务器_Java_Spring_Spring Mvc - Fatal编程技术网

Java Spring显示用户刚刚上载的图像,但未重新启动服务器

Java Spring显示用户刚刚上载的图像,但未重新启动服务器,java,spring,spring-mvc,Java,Spring,Spring Mvc,我试图创建一个函数,让用户上传他的个人资料图片,然后他可以在发布或评论时显示它,我使用的是SpringMVC、SpringSecurity、hibernate和Tomcat9 当我添加一张最近上传到项目根路径的图片并尝试在网站中显示它时,我必须重新启动它才能工作。 我无法让服务器在每次有人上传他的个人资料图片来显示它时重新启动,是否有任何类似于监听器的东西会立即看到在根路径中添加了一张图片,并在 <img src="${pageContext.request.contextPath}/st

我试图创建一个函数,让用户上传他的个人资料图片,然后他可以在发布或评论时显示它,我使用的是SpringMVC、SpringSecurity、hibernate和Tomcat9

当我添加一张最近上传到项目根路径的图片并尝试在网站中显示它时,我必须重新启动它才能工作。 我无法让服务器在每次有人上传他的个人资料图片来显示它时重新启动,是否有任何类似于监听器的东西会立即看到在根路径中添加了一张图片,并在

<img src="${pageContext.request.contextPath}/static/images/basic-profile-pic.jpeg">
是否可以在不重新启动根目录的情况下上载并显示它 或者最好不要将它放在根路径中,然后在其他地方更改它, 以及如何让这两种方式都起作用

谢谢

试试看。它可以方便地存储和检索图像、视频、文档等内容

将这些依赖项添加到pom.xml中:

    <dependency>
        <groupId>com.github.paulcwarren</groupId>
        <artifactId>spring-content-fs</artifactId>
        <version>0.0.11</version>
    </dependency>
    <dependency>
        <groupId>com.github.paulcwarren</groupId>
        <artifactId>spring-content-rest</artifactId>
        <version>0.0.11</version>
    </dependency>
以及以下bean:

    @Bean
    File filesystemRoot() {
      try {
        return new File("/path/to/your/images/outside/webapp");
      } catch (IOException ioe) {}
      return null;
    }

    @Bean
    FileSystemResourceLoader fileSystemResourceLoader() {
      return new 
         FileSystemResourceLoader(filesystemRoot().getAbsolutePath());
    }
添加以下存储:

    @StoreRestResource(path="profilePics")
    public interface ProfilePicStore extends Store<String> {
    }
将文件存储在/path/to/your/images/outside/webapp/profile1.jpg中

以及:

卷曲http://localhost:8080//profilePics/profile1.jpg

或:

我将再次检索它。无需重新启动

支持多种不同的商店;文件系统,db,s3,gridfs。只需将SpringContentFS依赖项替换为您想要使用的一个存储即可


Spring内容还可以与Spring数据相结合,以将内容与实体相关联。我提到,因为听起来您可能正在数据库中存储用户。有一个入门指南

你是如何上传图像的?通常,静态资产会在您添加后立即被提取,无需重新启动。通过在jsp文件中使用此选项上载:file name:并使用controller中的方法将其保存在根路径static/imagesd中,我是否需要使用ProfilePicStore接口存储以再次访问它?或者我可以简单地将您所说的添加到我的项目中,然后使用MultipartFile retrieve使用我的控制器方法上传图像,然后保存它吗?在这里,返回新文件/path/to/your/images,我应该返回项目的完整路径吗?像C:\Users\{user}\Desktop\{Spring Project}\webapp\static\images?还是什么else@AhmedMoSpringContent将看到ProfilePicStore和SpringContentFS依赖项,并为您注入此接口的实现。所以你不需要自己提供。由于@StoreRestResource注释,它还将注入一个@Controller实现,因此您也不需要提供它。该路径应该是您要将上载图像放置到的绝对路径。没有必要——事实上我建议——你把它放在你的web应用程序之外。感谢你的快速回复,我已经尝试了你刚才告诉我的东西,制作了一个界面,并将其与控制器包一起放置,并提供了保存图像的绝对路径{}但是我得到了这个错误,源服务器没有找到目标资源的当前表示,或者不愿意透露存在一个表示。我不需要一个方法来处理这个操作,因为idea告诉我它找不到那个操作处理程序
    @Bean
    File filesystemRoot() {
      try {
        return new File("/path/to/your/images/outside/webapp");
      } catch (IOException ioe) {}
      return null;
    }

    @Bean
    FileSystemResourceLoader fileSystemResourceLoader() {
      return new 
         FileSystemResourceLoader(filesystemRoot().getAbsolutePath());
    }
    @StoreRestResource(path="profilePics")
    public interface ProfilePicStore extends Store<String> {
    }
<form:form action="${pageContext.request.contextPath}/profilePics/profile1.jpg" method="post" enctype="multipart/form-data">
    <input type ="file" name ="file">
    <input type ="submit" value ="submit">
</form:form>
<img src="${pageContext.request.contextPath}/profilePics/profile1.jpg">