Handling uploaded files in Spring Boot - spring

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"));

Related

Can we calculate checksum of the file as it is being uploaded by the user in a Spring MVC application?

I have a Spring MVC application that is deployed in Apache Tomcat 9 Webserver. Is it possible to calculate checksum as the file is being uploaded by the user? Where should I even begin looking if I wanted to do this?
To be specific, I understand that I can calculate checksum inside a #Controller class in Spring MVC. However, at this point in time, the file is completely uploaded to the temporary file upload directory. I am, specifically, asking if there is a way to calculate the checksum of the individual parts as a MultipartFile object is being uploaded/created. Do I have to override Apache Tomcat behaviour? Is it possible to do this without modifying Tomcat source code? If not, what is the potential impact of such a modification of Tomcat source code?
I do not understand the intricacies of webservers and would appreciate it if someone could tell me where to start looking.
Yes, you can't calculate the checksum before you have all the file content. Do you have access to the front end part? If you have, you can calculate the checksum via Javascript and put that information in your request header.
But in this case, you may not be able to use MultipartFile component in Spring, you might need to use apache upload library directly, you have more low level control to allow you to read header before you start to get the file content streaming.

Serving list of files via a spring boot application

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.

How to access directory outside the application using spring mvc and jsp?

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/.

Retrieve the user response saved in a file in an app hosted on Cloudbees

I have hosted a Tomcat application on CloudBees which allows users to edit some XML and saves them. I need to download and save these files locally for my personal usage. However I could not find a way to do this. I tried the 'download source' option but it downloads the original files that I had uploaded and not the edited versions. However my application is able to access the edited versions (and so clearly everything is being saved all right). Getting these files back is extremely critical and necessary for me and is, in fact, the whole motive of this app. Kindly tell if there is some way to get back the files in CloudBees or any other free Java hosting site which would allow me to do it.
It's not very clear from your question how your app is currently dealing with these files, but I'll take a swing at providing some general info.
To support editing and downloading of files, your app design would need to address the following issues:
How do users edit/upload the changed XML?
Where does your app store the changed XML?
How does your app retrieve the edited XML and make it available for download?
For #1, you will need to provide an edit or upload interface in your app for manipulating the XML files. I'm assuming this is something your app has already solved using a form of some kind.
For #2, you need to pick an approach for storing the files that is appropriate for app's needs and the runtime environment where your app will be deployed. For instance, on CloudBees (or most other CLoud platforms), it's important to understand that the local filesystem of the app can be used for temporary storage, but it is not clustered and it will be wiped away each time the app is updated or restarted. If these XML files need to be available forever, you will need to store them in a persistent location that is external to the application's runtime instance. Most developers use databases (such as the CloudBees MySQL service) to store persistent data in this way. In general, your app can store these files anywhere, but your app needs to manage how to store them, and how to retrieve them later.
For #3, to allow a user to download the changed files, you will need to implement your own mechanism for retrieving the file from its persistent location, and then send it back to the user's browser. If you want something like right-click "Save As" to work, then your app will just need to support a URL that can display the edited XML file directly in the browser. If your app then provides a link to that URL, users can download it using RightClick+SaveAs. If you want the user to be able to click on a button/link and trigger a Save As dialog automatically, then you'd need to write a URL handler (Servlet) that serves the XML content up using a Content-Disposition header (see this StackOverflow article). This header will tell the browser that the file is supposed to be saved to disk, and allows you to provide a default file name.

Upload pdf on a server with Spring MVC

i try to create a web site where it will provide an option to the user to upload a pdf file. I want this pdf to be saved on the server with specific url. For example:
The user select the title.
The user upload the file.
I want after these actions the file to be accessible via a specific url/name_the_user_give.
I saw that the Spring MVC provides very easy interface for file uploading.I already see many tutorial about this but my problem is how can i save the paper into a server.Also i want the pdf to open by the browser's tools.Can anyone give me directions?
To save the file on server path you simply needs to write the file stream to the path described by
request.getServletContext().getRealPath("your directory name here");
and then create the file using the real path you get.
And to show the pdf file using browser plugin, you should use following in your controller method.
#RequestMapping(value="url to map", produces={"application/pdf"})
Hope this helps you.
Cheers.

Resources