文件上传,两个spring boot应用程序之间的通讯 [英] File upload, communication between two spring boot application

查看:351
本文介绍了文件上传,两个spring boot应用程序之间的通讯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Spring Boot应用程序,一个是支持我的前端的'AngularApp'(localhost:8870),另一个是运行某些批处理的'batchApp'(localhost:8871).

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.

我想将文件从"Front"上传到"AngularApp",然后再上传到"batchApp",如下所示.

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

现在,我基本上是使用REST API和"AngularApp"中的一个控制器和服务从"Front"上传到"AngularApp".

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)

它运行良好,可以将文件上传到特定的文件夹"upload-dir".

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

现在,我希望"AngularApp"和"batchApp"进行通信,以便"AngularApp"可以将上传的文件交给他,但是我不知道该怎么做. REST API?有什么想法吗?

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?

推荐答案

在这里,您将找到我的工作解决方案,其中包含pvpkiran的建议并遵循此方法

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

在AngularApp中,http发布请求:

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");
    }
}

我在batchApp中的控制器:

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");
    }

这篇关于文件上传,两个spring boot应用程序之间的通讯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆