代码样例,后台:
String fileName = "订单信息导出.xls"; // 设置下载文件名字
QueryGoodsOrderVo param = new QueryGoodsOrderVo();
param = JSONObject.parseObject(paramStr, QueryGoodsOrderVo.class);
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
try {
response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode(fileName, "utf-8"));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
InputStream in = webOrderService.exportOrderInfo(param);
System.out.println(in);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(in);
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bis.close();
bos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
页面:
var param = JSON.stringify($scope.searchParams);
window.location.href=CONTEXT.SYSTEM + "/order/exportOrderInfo?paramStr="+param;