Search code examples
javafilespring-mvcthymeleaf

Java Spring MVC - Uploading and storing file inside "webapp" folder is bad idea? Why is it?


When it comes to uploading a file in Spring MVC, I found many articles suggesting that you should store it into the file-system folder rather than folder inside of a project.

But My web-app render HTML(view)page using a file from a folder inside webapp folder.

├── 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

in my View part, I use files from resources folder to render the HTML page.

.
.
<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;
}
.
.
.

I want to make functionality that uploading a file into the resources folder and using it for rendering HTML page.

Is this a bad idea? Could you explain why?

If so, what's the best approach I can take for my task? (The Best location that for uploading files)


Solution

  • If you undeploy the application, what will happen to all files uploaded to the resources folder? I believe they will be deleted along with the rest of the application. If that doesn't matter to you, then you are fine to upload to that direct. If instead you want the files to persist between deployments, you should either:

    1. Upload them to a different directory on the file system. One that isn't managed by a server.
    2. Upload them to a database table as a BLOB.

    In either case, you will need to make a controller that returns the image as a file (because they are no longer accessible as resources).