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
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.
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.
Related
I have just implemented the HAL Browser bundled with spring-data-rest. It is accessible from my API root and is working fine. But I have found no way to customize anything about it. Is it possible to change the browser look and feel mainly for production use? Also is it possible to put it on a different url? Is it recommended for production in the first place? I am using Spring Boot 2.0.
I would not recommend having HAL browser enabled in production. Why would you need it? It is a tool to easily traverse a HATEOAS API within a browser. You can use it in dev/test environment. Production APIs can be referred with Postman, Insomnia like tools.
Having said that, it is possible to modify the look of HAL browser. All HAL browser related web files are located in META-INF/spring-data-rest/hal-browser. To modify any of these files, you can override them by creating the same folder structure inside src/main/resources directory, copy the files that you want to modify and update it.
I am making a spring boot application and I want to be able to upload image files in a form, save them somewhere on the server and the display these images. I managed to get the files using MultipartFile into my controller, however I am not sure how to save the files locally so that I can use them later.
I am guessing I have to somehow save the files in the resources directory so I can access them in my views. I will also be hosting my application on heroku and I am not sure what kind of access to the file system I have there.
Can somebody point me towards a solution?
Once you have your MultipartFile you can use FileCopyUtils.copy static method to upload it to a specified directory:
FileCopyUtils.copy(multipartFile.getBytes(), new File("src/main/resources"));
My application is allowed the customer to upload their images. So I am thinking to store those images some where and in my local disk, for instance /home/images/, and save the image path into the database. The problem I have is I am lacking of knowledge how to access the directory outside the application. Thank you in advance.
To save the files you have to use the java.nio.Files API or any other that allows you to create files. To read the images you can use serving static content via Spring MVC. The configuration in XML looks like that:
<mvc:resources mapping="/resources/images/**" location="file:/home/images/"/>
It makes the application look for the files (and folders) in your /home/images/ folder for all requests you make for the /resources/images/**.
Notice the file: prefix. It refers to the absolute path. When you want to set the location of your files relative to the application just leave the prefix out, e.g. /WEB-INF/images/.
I'm developing the custom payment method for Magento involving the SOAP requests. The service provided the wsdl schema that I should use. But I'm struggling to decide, what would be the best place to put it following Magento module structure.
Right now I'm considering to use etc directory, but I have some concerns on that point, since this directory is supposed to have the xml config files only. Please let me know if someone has faced such issue already, and what do you consider the best architecture approach.
I know, it's not the big deal what placement I will end up with, but I would rather follow the existing pattern, if it exists...
You can refer to the magento core modules: app/code/core/Mage/Usa/etc/wsdl/FedEx
Etc directory is used for storing all configuration and service data, so you can add your wsdl file to the etc directory too.
in my home page I want to display user guide in two different format ( pdf and word) these documents were already created by technical writers and I want to show the download links to these documents in home page after the user successfully logged in. I can achieve this easily by putting these two documents in one folder up to the 'WEB-INF' but it will enable anyone can download these files(without logging in). Could you advise whats the best way to handle this in spring mvc 2.5
I guess you already have some security support, so you can check whether user is logged in inside a controller code.
Then you can put your files into /WEB-INF folder and create a special controller for serving these files to the logged users. This controller will check that user is logged in and then forward a request to the target file. In typical Spring MVC configurations you can forward a request by returning something like forward:/WEB-INF/myFile.pdf as a view name.
Alternatively, if you use some security library, such as Spring Security, you can use its features to secure access to your files. In that case you don't need to put them into /WEB-INF and implement a specifal controller for accessing them.