现在的流程是这样的,在前端简单地发个请求,然后,后端处理数据,生成了excle文件,然后返回数据给前端。

但是前端怎么自动弹出下载框自动下载文件呢?现在接收的是乱码...

@RequestMapping(value = "/toExportData.req", method = RequestMethod.POST)

public ResponseEntity exportRequest(@RequestParam("array") String arr) {

String name = "export.xlsx";

ResponseEntity responseEntity = null;

ByteArrayOutputStream os = new ByteArrayOutputStream();

byte[] body = null;

InputStream is = null;

try {

createExcelFile().write(os);

body = os.toByteArray();

System.out.println("读取 输入流.....");

HttpHeaders headers = new HttpHeaders();

HttpStatus httpStatus = HttpStatus.OK;

System.out.println("设置headers。。。。");

headers.add("Content-Disposition", "attachment;filename=export.xlsx");

headers.setContentType(MediaType.valueOf("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));

System.out.println("headers is: " + headers);

responseEntity = new ResponseEntity(body, headers, httpStatus);

System.out.println("responseEntity:" + responseEntity);

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return responseEntity;

}

然后,请求时成功的,并且是有东西返回的:

967cf498a97f7f0d886fff6ee98b90bd.png

51e6c60d6f1a7b8568fe1953373abea5.png

内容是乱码:

cbbea5d99adc01abbf4503c952043a58.png

前端的请求代码:

d163de79f11e2a9c9dcb828909fb915c.png

理应是一个xlxs的文件才是!!!

怎么在前端接收呢??

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐