文件上传,两个spring boot应用程序之间的通信

时间:2018-01-15 12:46:56

标签: spring angular rest spring-boot backend

我有两个spring boot应用程序,一个是'AngularApp'(localhost:8870)支持我的前端,另一个是'batchApp'(localhost:8871)运行一些批次。

我想将文件从我的'Front'上传到'AngularApp',然后上传到'batchApp',如下图所示。

project structure

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

ColumnNames.Count = ValueNames.Count

效果很好,并将文件上传到特定文件夹'upload-dir'。

现在我希望'AngularApp'和'batchApp'进行通信,因此'AngularApp'可以为他提供上传的文件,但我不知道如何做到这一点。 REST API?有什么想法吗?

2 个答案:

答案 0 :(得分:0)

有关使用spring-framework库解决此问题的更好方法,请参阅

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

在Spring框架组件下面可以轻松实现。

Zuul - 提供动态路由,监控,弹性,安全性等的网关服务

功能区 - 客户端负载均衡器

Feign - 声明式REST客户端

Eureka - 服务注册和发现

侦探 - 通过日志分发跟踪

Zipkin - 带有请求可视化的分布式跟踪系统。

答案 1 :(得分:0)

在这里,您将找到我的工作解决方案,并提供pvpkiran建议并遵循此方法multipart upload with HttpClient4

在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中的控制器:

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