文件上传,两个 spring 引导应用程序之间的通信
File upload, communication between two spring boot application
我有两个spring启动应用程序,一个是'AngularApp'(localhost:8870)支持我的前端,另一个是'batchApp'(localhost:8871)运行 一些批次。
我想将文件从我的 'Front' 上传到 'AngularApp',然后再上传到 'batchApp',如下图所示。
现在我完成了从 'Front' 到 'AngularApp' 的上传,基本上使用 REST API 和 'AngularApp' 中的一个控制器和服务。
@PostMapping("/post")
public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file)
效果很好,可以将文件上传到特定文件夹 'upload-dir'。
现在我想让'AngularApp'和'batchApp'沟通,这样'AngularApp'就可以把上传的文件给他,但是我不知道怎么办。休息 API ?有什么想法吗?
使用spring框架库解决这个问题的更好方法,请参考
以下 spring 框架组件使它变得容易。
Zuul – 提供动态路由、监控、弹性、安全等的网关服务
Ribbon – 客户端负载均衡器
Feign – 声明式 REST 客户端
Eureka – 服务注册和发现
Sleuth – 通过日志进行分布式跟踪
Zipkin – 具有请求可视化的分布式跟踪系统。
在这里你会找到我的工作解决方案,使用 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");
}
我有两个spring启动应用程序,一个是'AngularApp'(localhost:8870)支持我的前端,另一个是'batchApp'(localhost:8871)运行 一些批次。
我想将文件从我的 'Front' 上传到 'AngularApp',然后再上传到 'batchApp',如下图所示。
现在我完成了从 'Front' 到 'AngularApp' 的上传,基本上使用 REST API 和 'AngularApp' 中的一个控制器和服务。
@PostMapping("/post")
public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file)
效果很好,可以将文件上传到特定文件夹 'upload-dir'。
现在我想让'AngularApp'和'batchApp'沟通,这样'AngularApp'就可以把上传的文件给他,但是我不知道怎么办。休息 API ?有什么想法吗?
使用spring框架库解决这个问题的更好方法,请参考
以下 spring 框架组件使它变得容易。
Zuul – 提供动态路由、监控、弹性、安全等的网关服务
Ribbon – 客户端负载均衡器
Feign – 声明式 REST 客户端
Eureka – 服务注册和发现
Sleuth – 通过日志进行分布式跟踪
Zipkin – 具有请求可视化的分布式跟踪系统。
在这里你会找到我的工作解决方案,使用 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");
}