【问题标题】:File upload, communication between two spring boot application文件上传,两个spring boot应用之间的通信
【发布时间】:2018-06-24 02:31:23
【问题描述】:

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

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

现在我从“Front”上传到“AngularApp”,基本上是在“AngularApp”中使用带有一个控制器和服务的 REST API。

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

它运行良好并将文件上传到特定文件夹'upload-dir'。

现在我希望“AngularApp”和“batchApp”进行通信,以便“AngularApp”可以将上传的文件提供给他,但我不知道该怎么做。休息API?有什么想法吗?

【问题讨论】:

标签: spring angular rest spring-boot backend


【解决方案1】:

使用spring-framework库解决这个问题的更好方法,请参考

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

下面的spring框架组件让它变得简单。

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

Ribbon – 客户端负载均衡器

Feign - 声明式 REST 客户端

Eureka – 服务注册和发现

Sleuth – 通过日志进行分布式跟踪

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

【讨论】:

    【解决方案2】:

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

    在AngularApp中,http post请求:

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

    【讨论】:

      猜你喜欢
      • 2018-07-10
      • 2015-12-03
      • 1970-01-01
      • 2015-12-01
      • 1970-01-01
      • 2018-05-05
      • 2016-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多