dev-resources.site
for different kinds of informations.
[Micronaut] Receiving multipart/form-data
Published at
3/20/2024
Categories
java
micronaut
Author
masanori_msl
Author
12 person written this
masanori_msl
open
Intro
This time, I will try receiving files as "multipart/form-data".
As same as last time, I will use Undertow as a web server.
Receiving files
When using Undertow, I can't receive files using "io.micronaut.http.server.multipart.MultipartBody" as I wrote last time.
So I will use "@Part" annotations in this time.
FileController.java
package jp.masanori;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.micronaut.http.HttpHeaders;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Part;
import io.micronaut.http.annotation.Post;
import io.micronaut.http.multipart.CompletedFileUpload;
@Controller("/files")
public class FileController {
private final Logger logger;
public FileController() {
this.logger = LoggerFactory.getLogger(FileController.class);
}
@Post(uri="/sample", consumes = MediaType.MULTIPART_FORM_DATA, produces = MediaType.TEXT_PLAIN)
public String uploadSampleFiles(HttpHeaders headers, @Part("file1") CompletedFileUpload file1) {
logger.info("FileName: " + file1.getFilename() + " size: " + file1.getSize());
return file1.getFilename();
}
}
To use the "@Part" annotation, I have to set "micronaut.server.multipart" enabled.
application.yml
micronaut:
application:
name: micronaut-web-app
router:
static-resources:
default:
enabled: true
paths: classpath:static
server:
port: 8086
multipart:
# add this line
enabled: true
max-file-size: 20971520
index.page.ts
window.Page = {
send() {
const file1Input = document.getElementById("upload_file_1") as HTMLInputElement;
const file1 = getFile(file1Input.files);
const form = new FormData();
// Must set "file1" even if there is no data
if(file1 == null) {
form.append("file1", new Blob());
} else {
form.append("file1", file1);
}
fetch("http://sample.masanori.jp:8086/files/sample", {
method: "POST",
mode: "cors",
body: form
}).then((res) => res.text())
.then((res) => console.log(res))
.catch(err => console.error(err));
}
}
function getFile(files: FileList|null): File|null {
if(files == null) {
return null;
}
if(files.length <= 0 || files[0] == null) {
return null;
}
return files[0];
}
If the form data does't have the "file1" parameter, a "Bad Request" error will occur.
{
"_links":{
"self":[{"href":"/files/sample","templated":false}]},
"_embedded":{"errors":[{
"message":"Required Part [file1] not specified","path":"/file1"
}]},
"message":"Bad Request"
}
I can receive two or more files like below.
FileController.java
...
@Post(uri="/sample", consumes = MediaType.MULTIPART_FORM_DATA, produces = MediaType.TEXT_PLAIN)
public String uploadSampleFiles(HttpHeaders headers, @Part("file1") CompletedFileUpload file1,
@Part("file2") CompletedFileUpload file2) {
...
}
...
micronaut Article's
30 articles in total
Pub-sub Redis in Micronaut
read article
Choosing the Right Java Microservices Framework: Spring Boot, Quarkus, Micronaut, and Beyond
read article
Micronaut vs. Spring Boot: A Detailed Comparison
read article
[Micronaut] Receiving Japanese(Shift-JIS) data as HTTPResponse
read article
[Micronaut] Try Cookie
read article
[Micronaut] Generating and downloading files as ZIP or JSON
read article
Sending files from Micronaut applications
read article
[Micronaut] Receiving multipart/form-data
currently reading
Try Micronaut and Doma2
read article
[Micronaut] Accessing SQL Server 2
read article
Serving static assets with Micronaut
read article
Run a Micronaut application on Tomcat
read article
[Micronaut] Accessing SQL Server 3
read article
[Micronaut] Accessing SQL Server 1
read article
Try Micronaut
read article
Adding AI to your Micronaut search service
read article
Swagger-Operator, let groovy operate your cluster
read article
Jugando con Mono y Flux (de Reactor)
read article
Meeting & Mastering Java
read article
Integration testing in Micronaut
read article
mapping OpenAPI formats
read article
Connecting Micronaut to MongoDB database using Kotlin
read article
🥉 Micronaut: Top 5 Server-Side Frameworks for Kotlin in 2022
read article
AWS Lambda SnapStart - Part 2 Measuring Java 11 Lambda cold starts with Micronaut framework
read article
Integration Tests with Micronaut and Kotlin
read article
Mapping with SCHEMA
read article
annotation mapping & bean-validation
read article
Can Micronaut replace Spring Boot? Let's take a look at an example.
read article
Cloud Native Java: Integrating YugabyteDB with Spring Boot, Quarkus, and Micronaut
read article
Micronaut Pulsar
read article
Featured ones: