Unparalleled suite of productivity-boosting Web APIs & cloud-based micro-service applications for developers and companies of any size.

API

RESTful API Tutorial: How to Upload Files to a Server

File Upload is an application that helps us upload files to the server. With File Upload, it is possible to upload files of the type allowed by the system, such as pictures, excel, pdf, to applications. File Upload control, on the other hand, enables us to perform file upload operations within the criteria we specify within our application. For example, we can load an excel file, read and list the records in it, and save it to the database. Or we can upload an image and save it in the database. Then we can perform operations with these uploaded files. Even though the file exists on our server while uploading the files, we eliminate the risk of overwriting any file when we make the name of the uploaded file something unique during the saving process. If we store the path of the uploaded file in the database or elsewhere, we can save it with the name we created.

In this article, we will perform file upload with a REST API that we will create with Java in just a few small steps. So let’s start.

REST API File Upload with Java

First of all, let’s create a Spring Boot project that will speed up our work and save us from many manual configurations. Let’s create a file named “FileController.java” in the Spring Boot project we have created and paste the following codes.

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@RestController
public class FileController {
@PostMapping(“/api/fileUpload”)
public ResponseEntity<String> file(@RequestPart(“file”) MultipartFile file) {
try {
byte[] fileToBytes = file.getBytes();
Path filePath = Paths.get(file.getOriginalFilename());
Files.write(filePath, fileToBytes);
System.out.println(“Uploaded file name is: “.concat(String.valueOf(filePath.getFileName())));
} catch (Exception e) {
System.out.println(e.getMessage());
}
return new ResponseEntity<>(HttpStatus.CREATED);
}
}

In the above code block, we have created an endpoint of type HTTP Post that is waiting for file. This endpoint prints the name of the external file to the console of the application.

That’s all we’ll do for File Upload on the code side. Let’s run the application and test the REST API via Postman.

Let’s enter the Postman application, select the file as follows and press the Send button.

After pressing the Send button, the console output of the application is as follows.

Conclusion

That’s all for activating the File Upload process, which is needed in applications for many reasons today. When setting up the File Upload flow for your applications with the REST API, do not forget to add security measures to your code and logics to check whether the file that comes with File Upload is harmful.

 

Related posts
API

How To Ensure Security When Developing APIs

API

What Should You Know About Open Weather APIs?

API

Is A Web Service The Same As An API?

API

Phone Lookup API: Everything You Need To Know

Leave a Reply

Your email address will not be published.