看着各位大佬寫的博客,質量非常高,很羨慕,人嘛,不分貴賤,博文也一樣,大佬們寫他們擅長牛逼的一塊,那我就把一些開發中小的問題補充上,大佬帶高手玩,那麼我就帶小白玩,言歸正傳,第一次實在項目中寫生成二維碼的接口,明明已經生成流寫到對象裏,爲什麼偏偏在頁面上沒有顯示出來,第二次在項目寫下載模板的接口,也是明明流已經寫了。爲什麼在頁面沒有任何反應,明明請求狀態是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的 內容寫到客戶端瀏覽器,這樣它想不顯示,都不行。約到這種問題直接刷新。不用糾結。