Spring Boot File Upload / Download Example using Multipart File

Most of the times file upload task are not simple as it looks like in any language . If you want to do in Java world with spring boot file upload and download .

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 our  files using Spring Boot using  REST API .

Spring boot has inbuilt interface known as MultipartFile . So , Multipart is one of the most efficient way to handle large file uploads and downloads in spring . Spring boot multiple file uploads are easily handled by spring MultipartFile in java.

We are also going to check How to download files with Spring Boot and  spring boot rest services.

Technologies Used

  • Spring Boot 2.0
  • Maven 3.0
  • Java 1.8

Project Structure

spring boot 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.

Create REST API for uploading files in Spring Boot 

Let’s write our rest api and understand how we can handle multipart file and understand step by step.

@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

spring boot multipart file upload