java下载txt前端后端代码_【Java】POI 在后端处理后数据,如何让前端自动下载呢?下载前端接受的乱码,而不是文件...

现在的流程是这样的,在前端简单地发个请求,然后,后端处理数据,生成了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;

}

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

cc45bf96fe7d739a6a9860f1a32a1175.png

e56bd9e3ace6e8a4c9248d53980d6d84.png

内容是乱码:

fd79ce83c3799d7c4c90d359b146ca47.png

前端的请求代码:

bfdfeb7f3662e6248876e82cc5f5cddc.png

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

怎么在前端接收呢??

回答

大致看了下,你这是要下载对吧,直接把你生成的excel 数据 用流用前段写不就是下载了,请求头你不是都设置好了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值