File Upload in Spring Boot Rest Api

Spring Boot 2 - File Upload and Download Residuum API Tutorial


In this tutorial, nosotros will larn how to upload and download a file using Spring Boot RESTful API.  Uploading and downloading files are very common tasks for which developers need to write code in their applications.

Larn and primary in Spring kick at Spring Boot Tutorial

Nosotros'll get-go build the REST APIs for uploading and downloading files, then test those APIs using Postman.

Let's go started.

Video Tutorial

This tutorial is explained with demo in below YouTube video. Subscribe to my youtube channel for future video updates at https://www.youtube.com/c/javaguides.

Tabular array of Contents

  1. Tools and Technologies Used
  2. Create and Import Bound Boot Project
  3. Projection Directory Structure
  4. The pom.xml File
  5. Configuring Server and File Storage Backdrop
  6. Automatically binding properties to a POJO class
  7. Writing APIs for File Upload and Download
  8. Service for Storing Files in the FileSystem and retrieving them
  9. Custom Exception Classes
  10. Running the Awarding and Testing the APIs via Postman

ane. Tools and Technologies Used

  • Spring Boot - ii.1.0.RELEASE
  • JDK - one.8 or afterward
  • Leap Framework - 5.i.2 RELEASE
  • Maven - three.two+
  • IDE - Eclipse or Leap Tool Suite (STS)

2. Create and Import Spring Boot Projection

Let'specify the following details:

  • Generate: Maven Project
  • Java Version: 1.8 (Default)
  • Spring Kick:2.1.0
  • Group: net.javaguides.springboot
  • Artifact: springboot-upload-download-file-residuum-api-case
  • Proper name: sspringboot-upload-download-file-balance-api-instance
  • Description: springboot-upload-download-file-residuum-api-example
  • Packet Name : cyberspace.guides.springboot.springbootfileupload
  • Packaging: jar (This is the default value)
  • Dependencies: Web

Once, all the details are entered, click on Generate Project button will generate a spring boot projection so download it. That'due south it! You may at present unzip the downloaded application archive and import information technology into your favorite IDE.

3. Project Directory Structure

Beneath, the diagram shows a project construction for reference:

4. The pom.xml File

<?xml                                      version=                    "1.0"                                                        encoding=                    "UTF-8"                  ?> <project                  xmlns=                    "http://maven.apache.org/POM/four.0.0"                                    xmlns                  :                  xsi=                    "http://world wide web.w3.org/2001/XMLSchema-case"                                    xsi:schemaLocation=                    "http://maven.apache.org/POM/four.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"                  >     <modelVersion>iv.0.0</modelVersion>     <groupId>net.javaguides.springboot</groupId>     <artifactId>springboot-upload-download-file-rest-api-instance</artifactId>     <version>0.0.i-SNAPSHOT</version>     <packaging>jar</packaging>     <proper name>springboot-upload-download-file-rest-api-example</name>     <description>Demo projection for Bound Boot</description>     <parent>         <groupId>org.springframework.boot</groupId>         <artifactId>leap-boot-starter-parent</artifactId>         <version>two.one.0.RELEASE</version>         <relativePath/>                                      <!--                    lookup parent from repository                    -->                                    </parent>     <properties>         <project.build.sourceEncoding>UTF-8</projection.build.sourceEncoding>         <project.reporting.outputEncoding>UTF-eight</project.reporting.outputEncoding>         <java.version>1.8</java.version>     </properties>     <dependencies>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>leap-boot-starter-web</artifactId>         </dependency>         <dependency>             <groupId>org.springframework.kicking</groupId>             <artifactId>spring-boot-starter-test</artifactId>             <scope>test</scope>         </dependency>         <dependency>             <groupId>org.springframework.kick</groupId>             <artifactId>jump-kick-configuration-processor</artifactId>             <optional>true</optional>         </dependency>     </dependencies>     <build>         <plugins>             <plugin>                 <groupId>org.springframework.kicking</groupId>                 <artifactId>leap-boot-maven-plugin</artifactId>             </plugin>         </plugins>     </build> </project>

v. Configuring Server and File Storage Properties

Let'due south configure our Spring Kick application to enable Multipart file uploads, and ascertain the maximum file size that can be uploaded. We'll also configure the directory into which all the uploaded files will be stored.

Open src/main/resources/awarding.properties file, and add the following properties to it -

                                      ## MULTIPART (MultipartProperties)                                      #                    Enable multipart uploads                  spring.servlet.multipart.enabled=truthful                                      #                    Threshold after which files are written to deejay.                  spring.servlet.multipart.file-size-threshold=2KB                                      #                    Max file size.                  jump.servlet.multipart.max-file-size=200MB                                      #                    Max Asking Size                  spring.servlet.multipart.max-request-size=215MB                                      ## File Storage Backdrop                  file.upload-dir=./uploads                  server.port=8081

six. Automatically binding properties to a POJO grade

Bound Boot has an awesome feature called @ConfigurationProperties using which you can automatically bind the properties divers in the application.backdrop file to a POJO course.

Let's define a POJO class called FileStorageProperties inside internet.javaguides.springboot.fileuploaddownload packet to bind all the file storage properties -

                  bundle                  net.javaguides.springboot.fileuploaddownload.property;                  import                  org.springframework.kick.context.properties.ConfigurationProperties;                  import                  org.springframework.stereotype.Component;                  @Component                  @ConfigurationProperties(prefix                  =                                      "file"                  )                  public                  class                  FileStorageProperties                  {                  individual                  String                  uploadDir;                  public                  Cord                  getUploadDir() {                  return                  uploadDir;     }                  public                  void                  setUploadDir(String                  uploadDir) {                  this                  .uploadDir                  =                  uploadDir;     } }

7. Writing APIs for File Upload and Download

File Upload Rest API

Permit's now write the Balance APIs for uploading single besides as multiple files. Create a new controller grade called FileUploadController inside cyberspace.javaguides.springboot.fileuploaddownload.controller package and add following code to information technology -

                  bundle                  internet.javaguides.springboot.fileuploaddownload.controller;                  import                  java.util.Arrays;                  import                  coffee.util.List;                  import                  java.util.stream.Collectors;                  import                  org.springframework.beans.factory.annotation.Autowired;                  import                  org.springframework.web.bind.annotation.PostMapping;                  import                  org.springframework.spider web.demark.annotation.RequestParam;                  import                  org.springframework.web.demark.annotation.RestController;                  import                  org.springframework.spider web.multipart.MultipartFile;                  import                  org.springframework.web.servlet.support.ServletUriComponentsBuilder;                  import                  net.javaguides.springboot.fileuploaddownload.payload.Response;                  import                  net.javaguides.springboot.fileuploaddownload.service.FileStorageService;                  @RestController                  public                  class                  FileUploadController                  {                  @Autowired                  individual                  FileStorageService                  fileStorageService;                  @PostMapping(                    "/uploadFile"                  )                  public                  Response                  uploadFile(@RequestParam(                    "file"                  )                  MultipartFile                  file) {                  String                  fileName                  =                  fileStorageService.storeFile(file);                  String                  fileDownloadUri                  =                  ServletUriComponentsBuilder                  .fromCurrentContextPath()             .path(                    "/downloadFile/"                  )             .path(fileName)             .toUriString();                  return                  new                  Response(fileName, fileDownloadUri,             file.getContentType(), file.getSize());     }                  @PostMapping(                    "/uploadMultipleFiles"                  )                  public                  List                  <                  Response                  >                  uploadMultipleFiles(@RequestParam(                    "files"                  )                  MultipartFile[]                  files) {                  return                  Arrays                  .asList(files)             .stream()             .map(file                  -                  >                  uploadFile(file))             .collect(Collectors                  .toList());     } }

File Download Rest API

Permit's now write the Rest API for downloading a file. Create a new controller form chosen FileDownloadController inside net.javaguides.springboot.fileuploaddownload.controller parcel and add following code to it -

                package                cyberspace.javaguides.springboot.fileuploaddownload.controller;                import                java.io.IOException;                import                javax.servlet.http.HttpServletRequest;                import                org.slf4j.Logger;                import                org.slf4j.LoggerFactory;                import                org.springframework.beans.factory.annotation.Autowired;                import                org.springframework.core.io.Resources;                import                org.springframework.http.HttpHeaders;                import                org.springframework.http.MediaType;                import                org.springframework.http.ResponseEntity;                import                org.springframework.spider web.bind.annotation.GetMapping;                import                org.springframework.web.bind.annotation.PathVariable;                import                org.springframework.web.bind.annotation.RestController;                import                net.javaguides.springboot.fileuploaddownload.service.FileStorageService;                @RestController                public                grade                FileDownloadController                {                private                static                final                Logger                logger                =                LoggerFactory                .getLogger(FileDownloadController                .class);                @Autowired                private                FileStorageService                fileStorageService;                @GetMapping(                  "/downloadFile/{fileName:.+}"                )                public                ResponseEntity                <                Resource                >                downloadFile(@PathVariable                String                fileName,                HttpServletRequest                request) {                                  //                  Load file as Resource                Resource                resource                =                fileStorageService.loadFileAsResource(fileName);                                  //                  Try to determine file's content type                String                contentType                =                null;                effort                {             contentType                =                request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());         }                catch                (IOException                ex) {             logger.info(                  "Could non determine file type."                );         }                                  //                  Fallback to the default content type if blazon could non be adamant                if                (contentType                ==                null) {             contentType                =                                  "application/octet-stream"                ;         }                return                ResponseEntity                .ok()             .contentType(MediaType                .parseMediaType(contentType))             .header(HttpHeaders                                  .CONTENT_DISPOSITION,                                  "attachment; filename=\"                  "                                +                resource.getFilename()                +                                  "                  \"                  "                )             .body(resources);     } }

Response

This Response form used to return response from the /uploadFile and /uploadMultipleFiles APIs.

Create Response class inside cyberspace.javaguides.springboot.fileuploaddownload.payload package with the post-obit contents -

                public                class                Response                {                private                String                fileName;                private                Cord                fileDownloadUri;                individual                String                fileType;                private                long                size;                public                Response(String                fileName,                String                fileDownloadUri,                String                fileType,                long                size) {                this                .fileName                =                fileName;                this                .fileDownloadUri                =                fileDownloadUri;                this                .fileType                =                fileType;                this                .size                =                size;     }                                  //                  Getters and Setters (Omitted for brevity)                }

8. Service for Storing Files in the FileSystem and retrieving them

Let's now write the service for storing files in the file organization and retrieving them. Create a new class called FileStorageService.java inside net.javaguides.springboot.fileuploaddownload.service with the post-obit contents -

                  package                  cyberspace.javaguides.springboot.fileuploaddownload.service;                  import                  java.io.IOException;                  import                  coffee.net.MalformedURLException;                  import                  coffee.nio.file.Files;                  import                  java.nio.file.Path;                  import                  java.nio.file.Paths;                  import                  coffee.nio.file.StandardCopyOption;                  import                  org.springframework.beans.factory.notation.Autowired;                  import                  org.springframework.cadre.io.Resource;                  import                  org.springframework.core.io.UrlResource;                  import                  org.springframework.stereotype.Service;                  import                  org.springframework.util.StringUtils;                  import                  org.springframework.web.multipart.MultipartFile;                  import                  net.javaguides.springboot.fileuploaddownload.exception.FileStorageException;                  import                  internet.javaguides.springboot.fileuploaddownload.exception.FileNotFoundException;                  import                  cyberspace.javaguides.springboot.fileuploaddownload.property.FileStorageProperties;                  @Service                  public                  grade                  FileStorageService                  {                  private                  final                  Path                  fileStorageLocation;                  @Autowired                  public                  FileStorageService(FileStorageProperties                  fileStorageProperties) {                  this                  .fileStorageLocation                  =                  Paths                  .get(fileStorageProperties.getUploadDir())             .toAbsolutePath().normalize();                  try                  {                  Files                  .createDirectories(this                  .fileStorageLocation);         }                  catch                  (Exception                  ex) {                  throw                  new                  FileStorageException(                    "Could not create the directory where the uploaded files volition be stored."                  , ex);         }     }                  public                  String                  storeFile(MultipartFile                  file) {                                      //                    Normalize file name                  String                  fileName                  =                  StringUtils                  .cleanPath(file.getOriginalFilename());                  attempt                  {                                      //                    Check if the file's proper name contains invalid characters                  if                  (fileName.contains(                    ".."                  )) {                  throw                  new                  FileStorageException(                    "Sorry! Filename contains invalid path sequence                    "                                    +                  fileName);             }                                      //                    Re-create file to the target location (Replacing existing file with the aforementioned name)                  Path                  targetLocation                  =                  this                  .fileStorageLocation.resolve(fileName);                  Files                  .copy(file.getInputStream(), targetLocation,                  StandardCopyOption                                      .REPLACE_EXISTING);                  return                  fileName;         }                  catch                  (IOException                  ex) {                  throw                  new                  FileStorageException(                    "Could not store file                    "                                    +                  fileName                  +                                      ". Please endeavour again!"                  , ex);         }     }                  public                  Resource                  loadFileAsResource(String                  fileName) {                  try                  {                  Path                  filePath                  =                  this                  .fileStorageLocation.resolve(fileName).normalize();                  Resource                  resource                  =                  new                  UrlResource(filePath.toUri());                  if                  (resource.exists()) {                  return                  resource;             }                  else                  {                  throw                  new                  FileNotFoundException(                    "File not constitute                    "                                    +                  fileName);             }         }                  catch                  (MalformedURLException                  ex) {                  throw                  new                  FileNotFoundException(                    "File not found                    "                                    +                  fileName, ex);         }     } }

9. Custom Exception Classes

The FileStorageService grade throws some exceptions in case of unexpected situations. Following are the definitions of those exception classes (All the exception classes become inside the package cyberspace.javaguides.springboot.fileuploaddownload.exception).

FileNotFoundException

                  parcel                  net.javaguides.springboot.fileuploaddownload.exception;                  import                  org.springframework.http.HttpStatus;                  import                  org.springframework.spider web.bind.note.ResponseStatus;                  @ResponseStatus(HttpStatus                                      .NOT_FOUND)                  public                  class                  FileNotFoundException                  extends                  RuntimeException                  {                  individual                  static                  final                  long                  serialVersionUID                  =                  1                  L;                  public                  FileNotFoundException(Cord                  message) {                  super(message);     }                  public                  FileNotFoundException(String                  message,                  Throwable                  cause) {                  super(message, cause);     } }

FileStorageException

                  bundle                  cyberspace.javaguides.springboot.fileuploaddownload.exception;                  public                  class                  FileStorageException                  extends                  RuntimeException                  {                  private                  static                  final                  long                  serialVersionUID                  =                  ane                  Fifty;                  public                  FileStorageException(String                  message) {                  super(bulletin);     }                  public                  FileStorageException(String                  message,                  Throwable                  cause) {                  super(message, crusade);     } }
To know more about exception handling in Spring kick then cheque out Spring Boot 2 Exception Treatment for Residue APIs

Note that, we've annotated the above exception form with @ResponseStatus(HttpStatus.NOT_FOUND). This ensures that Leap boot responds with a 404 Non Found status when this exception is thrown.

10. Running the Application and Testing the APIs via Postman

Nosotros're done developing our backend APIs. Let's run the application and exam the APIs via Postman. Type the post-obit command from the root directory of the project to run the application -

                mvn bound-kicking:run                              

i. Upload File

2. Upload Multiple Files

iii. Download File

Conclusion

That'southward it. We learned how to upload unmarried as well every bit multiple files via REST APIs written in Spring Boot. Nosotros too learned how to download files in Leap Boot.

I hope the post was helpful to you. You can download the unabridged code for the project that we built in this article from the GitHub repository.

Learn and master in Spring kicking at Leap Boot Tutorial

Gratis Spring Kick Tutorial | Full In-depth Course | Learn Leap Boot in 10 Hours


Picket this course on YouTube at Jump Kick Tutorial | Fee ten Hours Full Course

lancasteryoughlythers.blogspot.com

Source: https://www.javaguides.net/2018/11/spring-boot-2-file-upload-and-download-rest-api-tutorial.html

0 Response to "File Upload in Spring Boot Rest Api"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel