1

I have a spring boot application. my js files and other static files are under /static and when i type something like "http://application ip:port/file1.txt and file1.txt" is under /static, i am able to see it.

But i want to create a directotry say "/static/mydownloads" and want the list of files to be displayed that user can download say "http://application ip:port/static/mydownloads" must display list of files i can download "file1d.txt` file2d.txt file3d.txt" and person can download it by clicking on it. I tried resolvers and other random things but it did not work

curiousengineer
  • 2,196
  • 5
  • 40
  • 59

2 Answers2

1

I suppose you want something like most HTTP daemons have, a directory listing. Well, if you want something like that in Spring Boot, you'll have to develop it by yourself.

First of all, you need a controller. You'll have to loop over all files in a specific directory manually, probably using java.io.File. More information about that can be found in this answer:
How do I iterate through the files in a directory in Java?

Then you'll have to convert the file listing to a model, and use Spring MVC to display a listing of those files.

Community
  • 1
  • 1
g00glen00b
  • 41,995
  • 13
  • 95
  • 133
  • thanks. so it seems like there is no default straightforward way to do it. Thanks again for the info. – curiousengineer Aug 18 '15 at 16:12
  • @virtualgod Indeed, Spring Boot is just Spring, it's a framework for web applications, not a webserver for static file serving. If you want such a feature, you'll still have to write it yourself, but honestly, I don't think it's that hard. – g00glen00b Aug 19 '15 at 05:57
0

Spring boot lists /static as a resource folder automatically so it will have to override or extend that functionality to have /static be handled by multiple controllers. https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot

The easiest is probably to hook up a different controller for /mydownloads instead and let it list all the static resources available in /static/mydownloads.

NicoE
  • 186
  • 7
  • ok i am willing to add a different controller but i am still not clear how will i do that and if that will list all files in a particular directory. can you share any more clues. I will very much appreciate it – curiousengineer Aug 18 '15 at 04:58
  • i tried the resource handler stuff by adding my own path but it is not working. i can share the snippet if that helps – curiousengineer Aug 18 '15 at 04:59