最近我的项目,须要我调用另一个我的项目中的某个接口,接口的入参为 File 类型,要拿到此接口返回的数据
@ApiOperation("简历解析入库")
@ApiParam(name = "files", value = "files", required = true)
@RequestMapping(value = "/resumeAnalysis", method = RequestMethod.POST, consumes = {"multipart/*"}, headers = {"content-type=multipart/form-data"})
public ResultBody resumeAnalysis(@RequestParam("files") MultipartFile[] files){List jsonObject = sdkService.testResumeParser(files);
return ResultBody.success(jsonObject);
}
依据代码能够看出,headers = {“content-type=multipart/form-data”}),要通过 form-data 形式来传数据。办法如下
public static void main(String arg[]) {
String sResponse="";
try {
String url = "http://127.0.0.1:8080/rms/resume/resumeAnalysis";
String filePath="E: 简历 1.txt";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost uploadFile = new HttpPost(url);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
File files = new File(filePath);
// 把文件加到 HTTP 的 post 申请中
builder.addBinaryBody("files",new FileInputStream(files),ContentType.APPLICATION_OCTET_STREAM,files.getName());
HttpEntity multipart = builder.build();
uploadFile.setEntity(multipart);
CloseableHttpResponse response = httpClient.execute(uploadFile);
HttpEntity responseEntity = response.getEntity();
sResponse = EntityUtils.toString(responseEntity, "UTF-8");
}catch (Exception e){e.printStackTrace();
}
System.out.println(sResponse);
}
因为我这是写的 main 办法,所以参数还有 url 我都是本人定义了,失常状况下,以后端上传某个文件时,咱们须要先把文件解析,保留到本地,而后才能够应用此 main 办法执行上来。
强调一点,应用此办法时,传过来文件名可能会呈现乱码,导致解析数据的时候失败,此时须要在:MultipartEntityBuilder builder = MultipartEntityBuilder.create() 前面加上.setMode(HttpMultipartMode.RFC6532),残缺的为:
MultipartEntityBuilder builder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.RFC6532);
新手上路,欢送大佬领导。