0

I have two spring boot application, one is 'AngularApp' (localhost:8870) supporting my front and the other one is 'batchApp'(localhost:8871) running some batches.

I would like to upload a file from my 'Front' to 'AngularApp', then to 'batchApp' as illustrated below.

project structure

Right now I did the upload from 'Front' to 'AngularApp', basically using REST API with one controller and service in 'AngularApp'.

@PostMapping("/post")
public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file)

It works well and upload the file into a specific folder 'upload-dir'.

Now I want 'AngularApp' and 'batchApp' to communicate so 'AngularApp' can give him the file uploaded, but I have no idea about how to do it. REST API ? Any ideas?

Logan Wlv
  • 3,274
  • 5
  • 32
  • 54
  • 3
    Write a controller similar to the one you wrote above in batchApp. And in AnglarApp use RestTemplate to upload. An example here https://stackoverflow.com/questions/26964688/multipart-file-upload-using-spring-rest-template-spring-web-mvc – pvpkiran Jan 15 '18 at 12:56

2 Answers2

0

For the better approach to solve this problem using spring-framework libraries, please refer

https://piotrminkowski.wordpress.com/2017/02/05/part-1-creating-microservice-using-spring-cloud-eureka-and-zuul/

Below spring framework components make it easy.

Zuul – gateway service that provides dynamic routing, monitoring, resiliency, security, and more

Ribbon – client side load balancer

Feign – declarative REST client

Eureka – service registration and discovery

Sleuth – distributed tracing via logs

Zipkin – distributed tracing system with request visualization.

Fahad
  • 749
  • 6
  • 12
0

Here you'll find my working solution, with pvpkiran advice and following this method multipart upload with HttpClient4 :

In AngularApp, http post request :

public void batchAppUploadFile(String fileName) {
    log.i("Creating HTTP POST Request to upload a file on batchApp server");
    HttpClient client = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(myFile_URL);
    File file = new File(Paths.get("upload-dir").resolve(fileName).toString());
    FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    builder.addPart("file", fileBody);
    HttpEntity entity = builder.build();
    post.setEntity(entity);

    log.i("Executing HTTP Request...");
    try {
        HttpResponse response = client.execute(post);
        log.i("The request went well !");
        ResponseEntity.status(HttpStatus.OK).body("SUCESS BS upload");
    } catch (Exception e) {
        log.i("The request failed !");
        ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body("FAIL BS upload");
    }
}

My controller in batchApp :

@PostMapping("/uploadfile")
    public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file) {
        try {
            Path uploadPath = Paths.get(getUploadDirectory(file));
            Files.copy(file.getInputStream(), uploadPath.resolve(file.getOriginalFilename()));
            log.i(file.getOriginalFilename() + " upload complete !");
        } catch (Exception e) {
            throw new RuntimeException("FAIL!");
        }
        return ResponseEntity.status(HttpStatus.OK).body("Uploaded on batchApp");
    }
Logan Wlv
  • 3,274
  • 5
  • 32
  • 54