Skip to content

Software Development at Program Tom LTD

Place for coding, programming, development and software in general.

Menu
  • Blog
  • PDF Booklets
  • Dev Utils & Content
  • Java Spring Boot Or Web Apps
  • English
    • български
    • English
    • Español
    • Português
    • हिन्दी
    • Русский
    • Deutsch
    • Français
    • Italiano
    • العربية
  • About Us
Menu
File Upload with Spring Boot RestTemplate

File Upload with Spring Boot RestTemplate with Parameters

Posted on December 4, 2024 by Toma Velev

Here’s an example of how you can use Spring Boot ‘s RestTemplate to Upload a File with additional parameters.

First, let’s create a simple REST endpoint that accepts a file and some additional parameters. We’ll use Spring MVC to create this endpoint.

FileUploadController.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FileUploadController {

    @PostMapping("/upload")
    public String uploadFile(@RequestParam("file") FileSystemResource file,
                              @RequestParam("name") String name,
                              @RequestParam("age") int age) {

        // Process the file and parameters here
        System.out.println("File: " + file.getFilename());
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);

        return "File uploaded successfully";
    }
}

Now, let’s use RestTemplate to upload a file with additional parameters.

FileUploadClient.java

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

public class FileUploadClient {

    private final RestTemplate restTemplate;
    private final String baseUrl = "http://localhost:8080/upload";

    public FileUploadClient(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public String uploadFile(String filePath, String name, int age) throws Exception {
        FileSystemResource file = new FileSystemResource(filePath);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        HttpEntity<FileSystemResource> entity = new HttpEntity<>(file, headers);

        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("name", name);
        map.add("age", age);

        HttpEntity<MultiValueMap<String, Object>> additionalParams = new HttpEntity<>(map, headers);

        return restTemplate.postForObject(baseUrl, entity, String.class);
    }
}

Usage

public class Main {
    public static void main(String[] args) throws Exception {
        RestTemplate restTemplate = new RestTemplate();

        FileUploadClient client = new FileUploadClient(restTemplate);

        String filePath = "path_to_your_file.txt";
        String name = "John Doe";
        int age = 30;

        System.out.println(client.uploadFile(filePath, name, age));
    }
}

In this example, we’re using RestTemplate to upload a file with additional parameters. The uploadFile method in the client class takes three parameters: the path to the file, the name of the person, and their age. It creates a FileSystemResource object from the file path and sets up an HTTP entity with this resource. It also creates a MultiValueMap to hold the additional parameters and sets up another HTTP entity with this map.

The postForObject method is then called to send the file and parameters to the server. The server-side code in FileUploadController will receive these parameters and process them accordingly.

Note that this example assumes you’re using Spring MVC on the server-side. If you’re using a different framework or technology, you’ll need to adjust the code accordingly.

Also note that this is just a basic example and doesn’t include any error handling or other features you might need in a real-world application.

If you are interested you could check out my Java & Spring Boot

  • Articles https://programtom.com/dev/category/software-development/spring/
  • Products https://programtom.com/dev/product-category/technologies/spring-boot-framework/?orderby=date-desc
  • Jenkins SCP File Upload to Remote Server
  • Essential Programming Books – Principles & Flutter
  • Social Media Platforms 🌍
  • Strategies to prevent review regressions
  • How to set Google Map Theme in a Flutter App

Categories

  • Apps (22)
  • ChatGPT (23)
  • Choosing a Framework (38)
  • Flutter (269)
  • Graphical User Interface (14)
  • Marketing (117)
  • Software Development (286)
  • Spring (45)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (15)

Tags

Algorithms (9) crypto (29) flutterdev (39) General (86) Java (7) QR & Bar Codes (3) Software Dev Choices (33) Spring Boot (1) standards (1) Theme (3) User Authentication & Authorization (9) User Experience (10) Utilities (19) WordPress (11)

Product categories

  • All Technologies (84)
    • Flutter Apps (24)
    • GPT (4)
    • Java (38)
    • Native Android (3)
    • PHP (9)
    • Spring (Boot) / Quarkus (35)
    • Utils (15)
    • Vaadin 24+ (27)
    • Vaadin 8 (1)
  • Apps (18)
    • Employees DB (1)
    • Notes (6)
    • Personal Budget (1)
    • Recipes Book (1)
    • Stuff Organizer (1)
    • To-Do (2)
  • PDF Books (3)
  • Source Code Generators (8)

Recent Posts

  • Jenkins SCP File Upload to Remote Server
  • Essential Programming Books – Principles & Flutter
  • Social Media Platforms 🌍
  • Strategies to prevent review regressions
  • How to set Google Map Theme in a Flutter App

Post Categories

  • Apps (22)
  • ChatGPT (23)
  • Choosing a Framework (38)
  • Flutter (269)
  • Graphical User Interface (14)
  • Marketing (117)
  • Software Development (286)
  • Spring (45)
  • StartUp (22)
  • Uncategorized (14)
  • Uncategorized (4)
  • Vaadin (15)