我正在尝试建立两个micros之间的通信,其中第一个micros接收来自前端的请愿书,并与另一个micros联系,后者创建一个excel文件并将其发送回第一个micros,开始下载该文件。
首先,我尝试打印一个简单的字符串响应来检查micros和之间的通信是否正常,但同样的事情对文件不起作用。
下面是我遵循的代码:对于控制器A:
@GetMapping(path="/test")
public void getEntities2(HttpServletRequest request, HttpServletResponse response) throws IOException {
ResponseEntity<Workbook> responseEntity = new RestTemplate()
.getForEntity("http://localhost:8080/proceso/excel", Workbook.class);
assertThat(responseEntity.getStatusCode(), equalTo(HttpStatus.OK));
response.setHeader("Content-disposition", "attachment; filename=" + "test.xls");
Workbook workbook = responseEntity.getBody();
workbook.write(response.getOutputStream());
workbook.close();
}第二个控制器(Excel文件生成器):
@GetMapping(path="/excel")
public ResponseEntity<Workbook> test3(HttpServletResponse response) throws Exception {
HashMap<String, ArrayList<String>> test = new HashMap<String, ArrayList<String>>();
Workbook workbook = CreateExcel.excelCreator(test);
Workbook responseFile = workbook;
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + "test.xls")
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(responseFile);
}然后出现以下错误:
org.springframework.web.client.HttpClientErrorException$NotAcceptable: 406 null我只想接收第一个控制器中的文件,并从localhost下载Excel。有什么办法可以做到吗?谢谢大家。
发布于 2019-05-28 13:59:12
我发现了问题。excel文件是一个八位位流文件,因此需要将其作为byte[]数组接收。这是控制器A:
@GetMapping(path="/")
public ResponseEntity<byte[]> getEntities() throws IOException {
ResponseEntity<byte[]> responseEntity = new RestTemplate()
.getForEntity("http://localhost:8080/excel", byte[].class);
assertThat(responseEntity.getStatusCode(), equalTo(HttpStatus.OK));
return responseEntity;
}和控制器B:
@GetMapping(path="/excel")
public void getExcel(HttpServletResponse response) throws Exception {
Iterable<InstancesAggregate> list = instancesAggregate.myfindAll(); //Creating a list
HashMap<String, ArrayList<String>> test = CreateExcel.setExcelProceso(list);
Workbook workbook = CreateExcel.excelCreator(test);
response.setHeader("Content-disposition", "attachment; filename=" + "test.xls");
workbook.write(response.getOutputStream());
workbook.close();
}这对我来说很好,希望有人能找到有用的信息。
https://stackoverflow.com/questions/56303898
复制相似问题