看着各位大佬写的博客,质量非常高,很羡慕,人嘛,不分贵贱,博文也一样,大佬们写他们擅长牛逼的一块,那我就把一些开发中小的问题补充上,大佬带高手玩,那么我就带小白玩,言归正传,第一次实在项目中写生成二维码的接口,明明已经生成流写到对象里,为什么偏偏在页面上没有显示出来,第二次在项目写下载模板的接口,也是明明流已经写了。为什么在页面没有任何反应,明明请求状态是200,先说解决方式。那就是调用response对象的flushBuffer()方法,贴一段代码
/**
* 下载模板
*/
@ApiOperation(value = "下载模板")
@ApiImplicitParam(paramType = "path", dataType = "String", name = "encrypted", value = "加密", required = true)
@RequestMapping(value = "/excel/template/{encrypted}", method = RequestMethod.GET)
public void template(@PathVariable String encrypted) {
GoodsSupplierExcelExportTemplateByStoreIdRequest request =
new GoodsSupplierExcelExportTemplateByStoreIdRequest();
request.setStoreId(commonUtil.getStoreId());
String file = goodsSupplierExcelProvider.supplierExportTemplate(request).getContext().getFile();
if(StringUtils.isNotBlank(file)){
try {
String fileName = URLEncoder.encode("商品导入模板.xls", "UTF-8");
HttpUtil.getResponse().setHeader("Content-Disposition", String.format("attachment;filename=\"%s\";filename*=\"utf-8''%s\"", fileName, fileName));
HttpUtil.getResponse().getOutputStream().write(new BASE64Decoder().decodeBuffer(file));
HttpUtil.getResponse().flushBuffer();
}catch (Exception e){
throw new SbcRuntimeException(CommonErrorCode.FAILED);
}
}
}
重点是flushBuffer(),response有个buffer,flushBuffer()会强行把Buffer的 内容写到客户端浏览器,这样它想不显示,都不行。约到这种问题直接刷新。不用纠结。