Spring Boot: Download file directly from directory with file URI

Viewed 219

Assume that I have a directory for uploaded public files like images in /var/my-project/upload/public. I want to download files in public directory with its name. For example if there is a file named product-image.png in public directory with uri /var/my-project/upload/public/product-image.png, access to this file with this url: http://mysite/public/product-image.png. I know how to use a controller for this purpose, but I want to know is there a way to directly access these files without using a controller method?

1 Answers

The only acceptable way of doing it is with a ReSTful api. There are other methods such as ftp, but When you use ftp, Files can be added by anyone who knows how. With a ReST controller, you define what can be added beforehand. In fact, with a controller you define just about everything regarding that endpoint.

So the short answer is (especially if you are using Spring boot) use an API, lest you not regret it later.

When I was in College, our website had a built in "back door" to a certain endpoint via ftp. Some kids in one of my classes found out about that back door and let themselves inside. Needless to say' they didnt bother wiping their shoes on the mat on their way in.

The moral: Never assume that your resources aren't worth messing with. All you need to do is make it unsecure, and people will eventually mess with it.

Related