How to Upload File Using Spring Boot and REST

FrugalisMinds

Author - FrugalisMinds

How to Upload File Using SpringBoot and REST:-

In this example, we are going to check how to Upload File Using Spring Boot and REST.We are going to use Spring MultipartFile  to upload file using Spring Boot and REST.

We are also going to check How to Download using Spring Boot rest services.

Technologies Used:-

  • Spring Boot 2.0
  • Maven 3.0
  • Java 1.8

Project Structure:-

Spring Boot Rest File Upload

Create File Upload Controller:-

We are going to upload a single file and upload it using  MultipartFile .

Let’s configure our Spring Boot application to enable Multipart file uploads and return the name of the uploaded file. Let's write a request mapping and write a basic REST Controller. This is a sample controller and we will be writing logic later in the section.

package com.frugalis.SpringRestFileUpload;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Random;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class FileUploadController {

	String UPLOAD\_DIR = "D://upload//";
	
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public @ResponseBody String handleFileUpload(@RequestParam(value = "file") MultipartFile file) throws IOException {
}	
	@RequestMapping(value = "/upload/{galleryId}", method = RequestMethod.GET)
	public ResponseEntity<byte\[\]> getFile(@PathVariable("galleryId")String galleryId) throws IOException {
	
	}
	
	private String getRandomString() {
		return new Random().nextInt(999999) + "\_" + System.currentTimeMillis();
	}

	private File getTargetFile(String fileExtn, String fileName) {
		File targetFile = new File(UPLOAD\_DIR + fileName + fileExtn);
		return targetFile;
	}

	private String getFileExtension(MultipartFile inFile) {
		String fileExtention = inFile.getOriginalFilename().substring(inFile.getOriginalFilename().lastIndexOf('.'));
		return fileExtention;
	}

} 

We are configuring endpoints as /**upload** and /upload/{imageId} . We written methods to return random string as well as the file format as we are going to need those while saving file.

Upload Logic:-

@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public @ResponseBody String handleFileUpload(@RequestParam(value = "file") MultipartFile file) throws IOException {

		String fileExtension = getFileExtension(file);
		String filename = getRandomString();

		File targetFile = getTargetFile(fileExtension, filename);

		byte\[\] bytes = file.getBytes();
		file.transferTo(targetFile);
		String UploadedDirectory = targetFile.getAbsolutePath();

		return filename;
}

getTargetFile()creates a file object using UPLOAD_DIR, filename, and file extension.We are using MultipartFile.getBytes()to save the target file and return target file path in response.

Testing Code:-

Upload File using Spring Boot and REST

Note:-   Visit this Site to Understand More about Basics Of Java and Collections.

Some Must Read Posts

  1. React JS Tutorials
  2. Spring Cloud Tutorials
  3. Spring Boot Tutorials
  4. CRUD Rest Service With Spring Boot and JPA
  5. Spring Boot Profiling – Configure for Different Environments

Download Code