使用swgger2、Restlet等接口工具有bug導致導出失敗,測試直接使用瀏覽器
//導出列表-新
@UserRoleJudgment(authpos = SystemControllerLog.AUTH_PARAM, paraname = "auth")
@SystemControllerLog(module = "管理", description = "導出列表",authpos = SystemControllerLog.AUTH_PARAM, paraname = "auth")
@RequestMapping(value = "/exportContractExcelN", method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "導出列表", httpMethod = "GET", notes = "導出列表,需要授權")
public ReponseResult<Boolean> exportContractExcelN(
@ApiParam(value = "編號,參數形式7564cb720a0000151bcbee13209899a0,75522b6b0a000015160873e6fa5dec29", required = true) @RequestParam String[] ids,
HttpServletRequest request,
HttpServletResponse response) throws IOException {
if (ids == null || ids.length == 0||auth==null) {
return ReponseResult.error(CodeMsg.PARAMETER_ISNULL);
}
List<String> idsn = java.util.Arrays.asList(ids);
List<ContractModel> list = null;
try {
list = contractService.exportDataList1(idsn);
} catch (Exception e) {
e.printStackTrace();
}
if(list==null||list.size()==0){
return ReponseResult.error(new CodeMsg(-1, "列表為空!"));
}
// 導出表格
HSSFWorkbook wb = contractService.exportBatch1(list);
OutputStream os = null;
try {
// 創建一個普通輸出流
os = response.getOutputStream();
String fileName = "file.xls";
// 請求瀏覽器打開下載窗口
response.reset();
response.setCharacterEncoding("UTF-8");
// Content-disposition 告訴瀏覽器以下載的形式打開
String header = request.getHeader("User-Agent").toUpperCase();
if (header.contains("MSIE") || header.contains("TRIDENT") || header.contains("EDGE")) {
fileName = URLEncoder.encode(fileName, "utf-8");
fileName = fileName.replace("+", "%20"); // IE下載文件名空格變+號問題
} else {
fileName = new String(fileName.getBytes(), "ISO8859-1");
}
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);// 要保存的文件名
response.setContentType("application/vnd.ms-excel");
// 直接用數組緩沖輸出流輸出
wb.write(os);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return ReponseResult.success(true);
}