0

在 Spring MVC 中上传文件时,我发现很多文章建议您将其存储到文件系统文件夹中,而不是项目内部的文件夹中。

但我的网络应用程序使用来自webapp文件夹内的文件夹的文件呈现 HTML(视图)页面。

├── src
│   ├── main
│   │   ├── java
│   │   │   └── com   // Where my application's is.
│   │   ├── resources
│   │   │   ├── META-INF
│   │   │   ├── log4j.xml
│   │   │   ├── maildata.properties
│   │   │   └── persistence-mysql.properties
│   │   └── webapp
│   │       ├── WEB-INF
│   │       │   ├── classes
│   │       │   ├── spring
│   │       │   ├── templates // Where my view pages are. Thymeleaf template used.
│   │       │   │   ├── login.html
│   │       │   │   ├── index.html
│   │       │   │   └── ...html and More...
│   │       │   └── web.xml
│   │       └── resources // --> This is location that my app use for rendering.
│   │           ├── css
│   │           ├── fonts
│   │           ├── images  
│   │           ├── js
│   │           └── vendors
│   └── test
│       ├── java
│       │   └── com
│       └── resources
│           └── log4j.xml
└── target

在我的视图部分,我使用资源文件夹中的文件来呈现 HTML 页面。

.
.
<link rel="stylesheet" th:href="@{'/resources/css/style.css'}" type="text/css">
.
.

.outbox {
    background:
        url([[@{/resources/images/header.jpg}]]) #000
        55% 0 no-repeat;
    background-size: 140% auto;
    margin-top: -68px;
    width: 100%;
    padding-top: 42.25%;
    position: relative;
    display: block;
    z-index: -1;
}
.
.
.

我想制作将文件上传到资源文件夹并使用它来呈现 HTML 页面的功能。

这是一个坏主意吗?你能解释一下为什么吗?

如果是这样,我可以为我的任务采取的最佳方法是什么?(上传文件的最佳位置)

4

1 回答 1

0

如果取消部署应用程序,上传到资源文件夹的所有文件会发生什么情况?我相信它们将与应用程序的其余部分一起被删除。如果这对您来说无关紧要,那么您可以直接上传。相反,如果您希望文件在部署之间保留,您应该:

  1. 将它们上传到文件系统上的不同目录。一个不受服务器管理的。
  2. 将它们作为 BLOB 上传到数据库表。

在任何一种情况下,您都需要创建一个将图像作为文件返回的控制器(因为它们不再可以作为资源访问)。

于 2019-02-11T15:43:09.093 回答