Follow
below steps to upload a file with other input fields.
public class UserDto {
private String firstName;
private String lastName;
private MultipartFile resume;
......
......
}
Step 2: Map the request payload to model object Using ModelAttribute annotation.
@PostMapping("/profile")
public ResponseEntity<UserResponseDto> createProfile(@ModelAttribute UserDto userDto)
throws IOException {
MultipartFile resume = userDto.getResume();
if (resume == null || resume.isEmpty()) {
throw new RuntimeException("Resume must not be empty");
}
//String originalFileName = resume.getOriginalFilename();
InputStream inputStream = resume.getInputStream();
String content = convertToString(inputStream);
UserResponseDto responseDto = new UserResponseDto();
responseDto.setFirstName(userDto.getFirstName());
responseDto.setLastName(userDto.getLastName());
responseDto.setResume(content);
return new ResponseEntity<>(responseDto, HttpStatus.CREATED);
}
Step 1: Create new maven project ‘springFileUpload’. Maven project looks like below.
Step 2:
Define model class
UserDto to map request payload.
package com.sample.app.model;
import org.springframework.web.multipart.MultipartFile;
public class UserDto {
private String firstName;
private String lastName;
private MultipartFile resume;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public MultipartFile getResume() {
return resume;
}
public void setResume(MultipartFile resume) {
this.resume = resume;
}
}
Step 3:
Define UserResponseDto
to map response.
package com.sample.app.model;
public class UserResponseDto {
private String firstName;
private String lastName;
private String resume;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getResume() {
return resume;
}
public void setResume(String resume) {
this.resume = resume;
}
}
Step 4:
Define
UserController.java
package com.sample.app.controller;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.sample.app.model.UserDto;
import com.sample.app.model.UserResponseDto;
@RestController
public class UserController {
@PostMapping("/profile")
public ResponseEntity<UserResponseDto> createProfile(@ModelAttribute UserDto userDto) throws IOException {
MultipartFile resume = userDto.getResume();
if (resume == null || resume.isEmpty()) {
throw new RuntimeException("Resume must not be empty");
}
// String originalFileName = resume.getOriginalFilename();
InputStream inputStream = resume.getInputStream();
String content = convertToString(inputStream);
UserResponseDto responseDto = new UserResponseDto();
responseDto.setFirstName(userDto.getFirstName());
responseDto.setLastName(userDto.getLastName());
responseDto.setResume(content);
return new ResponseEntity<>(responseDto, HttpStatus.CREATED);
}
private static String convertToString(InputStream in) throws IOException {
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
return result.toString("UTF-8");
}
}
Step 4:
Define App.java
package com.sample.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springFileUpload</groupId>
<artifactId>springFileUpload</artifactId>
<version>1</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<name>springbootApp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
Total project
structure looks like below.
Run App.java.
How to
upload a file via Postman?
Open
Postman.
Select the
request as POST, url as ‘http://localhost:8080/profile’.
Go to Body
section. Select form-data.
Add the
fields firstName, lastName and resume.
For the
field resume, select the option File. Once you select File option, browse
button is enabled to upload a file. Upload a file. Click on Send button to send
POST request.
You will
get user details along with the file content in response.
You can
download complete working application from this link.
No comments:
Post a Comment