Java Spring Boot-如何将POST表单数据多部分文件发送到Flask服务器
我想使用restTemplate将由MultipartFile包装的3个图像发送到Flask服务器 以下是我的Flash服务器代码:Java Spring Boot-如何将POST表单数据多部分文件发送到Flask服务器,java,python,spring-boot,flask,Java,Python,Spring Boot,Flask,我想使用restTemplate将由MultipartFile包装的3个图像发送到Flask服务器 以下是我的Flash服务器代码: @app.route('/id-card-analyze', methods=['POST']) def analyze(): print(request.files) img_data = request.files img1 = open_image(BytesIO((img_data['file1'].read()))) im
@app.route('/id-card-analyze', methods=['POST'])
def analyze():
print(request.files)
img_data = request.files
img1 = open_image(BytesIO((img_data['file1'].read())))
img2 = open_image(BytesIO((img_data['file2'].read())))
img3 = open_image(BytesIO((img_data['file3'].read())))
images = [img1, img2, img3]
prediction = [str(learn.predict(img)[0]) for img in images]
return jsonify(type=prediction)
我使用Postman进行测试,它可以工作:
以下是我的Java代码:
public ResultDTO validateIdCard(MultipartFile file1, MultipartFile file2, MultipartFile file3) throws IOException {
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
try {
parts.add("file1", new ByteArrayResource(file1.getBytes()));
parts.add("file2", new ByteArrayResource(file2.getBytes()));
parts.add("file3", new ByteArrayResource(file3.getBytes()));
} catch (IOException e) {
e.printStackTrace();
}
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(parts, headers);
String url = "http://localhost:5000/id-card-analyze";
// I've tried both requests but they're all 400
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
JSONObject json = restTemplate.postForEntity(url, requestEntity,JSONObject.class).getBody();
return new ResultDTO("wer");
}
2019-09-18 13:06:39.871 WARN 17109 --- [ XNIO-2 task-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused by handler execution: org.springframework.web.client.HttpClientErrorException: 400 BAD REQUEST
public resultto validateIdCard(多部分文件文件1、多部分文件文件2、多部分文件文件3)引发IOException{
MultiValueMap parts=新链接的MultiValueMap();
试一试{
parts.add(“file1”,新的ByteArrayResource(file1.getBytes());
parts.add(“file2”,新的ByteArrayResource(file2.getBytes());
parts.add(“file3”,新的ByteArrayResource(file3.getBytes());
}捕获(IOE异常){
e、 printStackTrace();
}
RestTemplate RestTemplate=新RestTemplate();
HttpHeaders=新的HttpHeaders();
headers.setContentType(MediaType.MULTIPART\u FORM\u DATA);
HttpEntity requestEntity=新的HttpEntity(部分、标题);
字符串url=”http://localhost:5000/id-卡片分析”;
//我试过两个请求,但都是400
ResponseEntity response=restemplate.exchange(url,HttpMethod.POST,requestEntity,String.class);
JSONObject json=restTemplate.postForEntity(url,requestEntity,JSONObject.class).getBody();
将新结果返回到(“wer”);
}
2019-09-18 13:06:39.871警告17109---[XNIO-2任务-1].m.a.ExceptionHandlerExceptionResolver:由处理程序执行引起的已解决异常:org.springframework.web.client.HttpClientErrorException:400错误请求
我试过两个请求,但都是400
在RestTemplate中实现此功能的正确方法是什么?您找到解决方案了吗?有完全相同的问题。你找到解决办法了吗?有完全相同的问题。