Local file download memorandum in Spring Boot

File download implementation procedure

Introduction

Scribble instead of memo for business. I will make a decent article later.

File download

Finally, just return the Resouce type file from Controller If you want to download a local file

AnyController.java


Resource resource = new FileSystemResource("File path here");
return resource;

I want to download the file with any name

Added HttpServeletResponse response to Controller argument. Set an appropriate value in the field variable (header) of response. You can leave the return etc. as it is. The Spring side will take care of it.

AnyController.java


//Argument description omitted
Resource resource = new FileSystemResource("File path here");
response.setHeader("Content-Disposition", "attachment; filename=" + "File name here");
return resource;

I want to use double-byte characters in the file name

It's OK if the file name is URL-encoded

AnyController.java


//Argument description omitted
Resource resource = new FileSystemResource("File path here");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("File name here", "UTF-8"));
return resource;

Recommended Posts

Local file download memorandum in Spring Boot
Spring Boot Memorandum
Static file access priority in Spring boot
Download with Spring Boot
How to bind to property file in Spring Boot
Set context-param in Spring Boot
Spring Boot 2 multi-project in Gradle
File upload with Spring Boot
Major changes in Spring Boot 1.5
NoHttpResponseException in Spring Boot + WireMock
Spring Boot Hello World in Eclipse
Spring Boot application development in Eclipse
Memorandum of understanding when Spring Boot 1.5.10 → Spring Boot 2.0.0
Write test code in Spring Boot
Implement file download with Spring MVC
Implement REST API in Spring Boot
What is @Autowired in Spring boot?
Implement Spring Boot application in Gradle
Thymeleaf usage notes in Spring Boot
Fitted in Spring Boot using a bean definition file named application.xml
What is a Spring Boot .original file?
Launch (old) Spring Boot project in IntelliJ
Build Spring Boot + Docker image in Gradle
Output Spring Boot log in json format
Create Java Spring Boot project in IntelliJ
Loosen Thymeleaf syntax checking in Spring Boot
[Practice! ] Display Hello World in Spring Boot
WebMvcConfigurer Memorandum of Understanding for Spring Boot 2.0 (Spring 5)
Use DynamoDB query method in Spring Boot
DI SessionScope Bean in Spring Boot 2 Filter
How to split Spring Boot message file
Change session timeout time in Spring Boot
File output bean as JSON in spring
Procedure to make the value of the property file visible in Spring Boot
My memorandum that I want to make ValidationMessages.properties UTF8 in Spring Boot
Test controller with Mock MVC in Spring Boot
Asynchronous processing with regular execution in Spring Boot
Run a Spring Boot project in VS Code
Output request and response log in Spring Boot
Challenge Spring Boot
Use Servlet filter in Spring Boot [Spring Boot 1.x, 2.x compatible]
How to add a classpath in Spring Boot
Java tips-Create a Spring Boot project in Gradle
Spring Boot Form
How to set environment variables in the properties file of Spring boot application
gae + spring boot
The attached file name was garbled in the Spring Boot email, so take measures
Annotations used in Spring Boot task management tool
[Spring Boot] How to refer to the property file
View the Gradle task in the Spring Boot project
Memorandum (Spring Web)
Specify the encoding of static resources in Spring Boot
Include external jar in package with Spring boot2 + Maven3
[Note] Configuration file when using Logback with Spring Boot
I checked asynchronous execution of queries in Spring Boot 1.5.9
A memorandum of addiction to Spring Boot2 x Doma2
How to create a Spring Boot project in IntelliJ
SSO with GitHub OAuth in Spring Boot 1.5.x environment
How to use CommandLineRunner in Spring Batch of Spring Boot
A memorandum when creating a REST service with Spring Boot
Test field-injected class in Spring boot test without using Spring container