Download file from rest






















Thanks for visiting DZone today,. Edit Profile. Sign Out View Profile. Over 2 million developers have joined DZone. This article demonstrates how to use a Spring Boot application and MySQL to maintain a database of uploaded files and their metadata.

Like Join the DZone community and get the full member experience. I have also specified the JDK version I will be using for our application.

File download example using Spring REST Controller does not many dependencies and you need to add only spring boot started web for downloading a file. I am creating below application. I do not need to create any datasource explicitly as I am using the standard naming conventions for database configurations and Spring Boot will automatically create one for us.

Create below entity class that maps to database table. Our entity class has only three fields employee id, employee first name and employee last name. I am creating entity class because I want to create a file from our database record and finally download the file using Spring REST service. You can build any type of file, such as excel file, csv file etc. So create below Spring Data JPA Repository which provides most of our built-in methods to query database for fetching data.

I fetch all employees from the database using the below service layer code. I annotate the service layer class using Service. I create service layer class to process our business logic to avoid any tight coupling between the layers. I use the data type as array of byte stream in the response to download the file. File may be downloaded from a selected location using the server side code or you may generated a report file from database and download the report file using the Spring REST Controller.

End ; The error thats returned is on the line response. GGesheva GGesheva 2 2 silver badges 11 11 bronze badges. I'm assuming your application is a website? TransmitFile is to transmit a file to the client from your servers's filesystem, not from an url — Robbe Roels.

Yes, that is right, my application is a website. I would say you could load the file from the rest api in memory and pass the stream to your client — Robbe Roels. Add a comment. Active Oldest Votes. TransmitFile expects a file path, not a URL. You will need to download the file first, then write to the response stream. ReadAsStreamAsync ; invoiceStream. CopyTo response. OutputStream ; response. Viewed 1k times. Improve this question. Add a comment. Active Oldest Votes. Below endpoint is for file downloading.

Improve this answer. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password.



0コメント

  • 1000 / 1000