[JAVA] File upload with Spring Boot (do not use Multipart File)

The following functions will be implemented this time. ・ Create an API to upload photos -Receive data in binary without using MultipartFile -Output the file to the specified directory via Java -File names are automatically numbered by year, month, day, hour, minute, and second (the name of the upload source is not used) -Obtain binary information and get the compression format (extension) of the image For this part, I referred to the following information. → Determine the type of image from binary data

File to modify

1.png

Controller

java:com.example.UsersController.java


@RequestMapping(path = "/users/upload", method = RequestMethod.POST)
public void upload(InputStream req) throws IOException {

	ByteArrayOutputStream byteos = new ByteArrayOutputStream();
	byte[] buf = new byte[1024];
	int size = 0;
	while ((size = req.read(buf, 0, buf.length)) != -1) {
		byteos.write(buf, 0, size);
	}

	String filename = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS").format(LocalDateTime.now());
	Path uploadfile = Paths.get("Users/demo-kusa/image" + filename
			+ "." + getFormat(byteos.toByteArray()));

	try (OutputStream os = Files.newOutputStream(uploadfile, StandardOpenOption.CREATE)) {
		os.write(byteos.toByteArray());
	} catch (IOException ex) {
		System.err.println(ex);
	}
}

** See the linked article for ImageTypeCheck.java ** (I've made some modifications, but it worked as it is) ImageType.java

When sending binary data, there are several methods such as http client commands such as curl, javascript, and REST client, but this time I used Postman.

2.png

After writing so far, I noticed that IOException is tried and throws in Controller. I will refactor it at a later date. Although it has a purpose, it wastes resources by going through ByteArrayOutputStream, so review it. .. github kaikusakari/spring_crud

Recommended Posts

File upload with Spring Boot (do not use Multipart File)
File upload with Spring Boot
Use Spring JDBC with Spring Boot
Use Basic Authentication with Spring Boot
Use cache with EhCashe 2.x with Spring Boot
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to use built-in h2db with spring boot
Download with Spring Boot
I tried to implement file upload with Spring MVC
Generate barcode with Spring Boot
Hello World with Spring Boot
Implement GraphQL with Spring Boot
Get started with Spring boot
Hello World with Spring Boot!
iOS: File upload with SFTP
Spring Boot starting with copy
Spring Boot starting with Docker
Hello World with Spring Boot
Set cookies with Spring Boot
Add module with Spring Boot
Getting Started with Spring Boot
Create microservices with Spring Boot
Send email with spring boot
Extract SQL to property file with jdbcTemplate of spring boot
Spring boot temporary upload location [/ tmp / tomcat ・ ・ ・ is not valid]
Use thymeleaf3 with parent without specifying spring-boot-starter-parent in Spring Boot
gRPC on Spring Boot with grpc-spring-boot-starter
Create an app with Spring Boot 2
Hot deploy with Spring Boot development
Database linkage with doma2 (Spring boot)
How to realize huge file upload with TERASOLUNA 5.x (= Spring MVC)
Spring Boot programming with VS Code
Until "Hello World" with Spring Boot
Inquiry application creation with Spring Boot
[Java] Do not use "+" in append!
Get validation results with Spring Boot
Implement file download with Spring MVC
(Intellij) Hello World with Spring Boot
Create an app with Spring Boot
Google Cloud Platform with Spring Boot 2.0.0
Use DBUnit for Spring Boot test
Check date correlation with Spring Boot
How to use ModelMapper (Spring boot)
I tried GraphQL with Spring Boot
[Java] LINE integration with Spring Boot
Programs that use io_uring do not work with Docker on CentOS 8
I tried Flyway with Spring Boot
Automatic file upload with old Ruby gem What to do with Watir
How to realize huge file upload with Rest Template of Spring
Message cooperation started with Spring Boot
Spring Boot gradle build with Docker
What is a Spring Boot .original file?
Do not use instance variables in partial
Hello World with Eclipse + Spring Boot + Maven
Static file access priority in Spring boot
Send regular notifications with LineNotify + Spring Boot
Local file download memorandum in Spring Boot
HTTPS with Spring Boot and Let's Encrypt
Try using Spring Boot with VS Code
Start web application development with Spring Boot
Launch Nginx + Spring Boot application with docker-compose