@ResponseBody
@RequestMapping(value = "/printing")
public void printing(@RequestParam Map<String, String> queryParams, HttpServletResponse response) throws IOException, JRException {
String ClassPath=fileBasePath+"/ireport/template/";
// File file=new File(this.getClass().getResource("/").getPath());
//截取绝对路径,不能把项目放到绝对路径下
// String classPath=file.toString().substring(0,file.toString().length()-30)+"src/main/resources/META-INF/public/ymsweb/waybill/dispatchPrint/";
// URL url = Thread.currentThread().getContextClassLoader().getResource("META-INF/public/ymsweb/waybill/dispatchPrint/zhuancangdan.jasper");
// URL url2 = Thread.currentThread().getContextClassLoader().getResource("META-INF/public/ymsweb/waybill/dispatchPrint/chuhuodan.jasper");
String path =ClassPath+"zhuancangdan.jasper";
String path2 =ClassPath+"chuhuodan.jasper";
Map<String, Object> parameters = new HashMap<>();
parameters.put(JRParameter.REPORT_LOCALE, new Locale("zh_cn"));//设置打印简体或者繁体
ServletOutputStream outputStream = null;
List jasperPrintList = new ArrayList();
UserDetails userDetails = UserContextHolder.getCurrentUser();
String removeAllWorking = queryParams.get("vrdoNos").replace("[", "").replace("]", "");
String[] vrdoNos=null;
if (!removeAllWorking.isEmpty()) {
String[] arr = removeAllWorking.split(",");
vrdoNos = new String[arr.length];
for (int i = 0; i < arr.length; i++) {
String id = String.valueOf(arr[i]);
vrdoNos[i] = id;
}
}
//判断是托运单还是转仓单,true为转仓单,false为出货单
if(vrDispatchOrderService.orderType(vrdoNos[i])){
//用托运单去查询该托运单下的所有订单
List<PrintingZhuanCangDanBean> printingZhuanCangDanBeans =vrDispatchOrderService.printingZhuanCangDan(vrdoNos[i]);
if(EmptyUtils.isEmpty(printingZhuanCangDanBeans)){
response.setCharacterEncoding("UTF-8");
response.sendError(404,"该托运单号"+vrdoNos[i]+"没有数据");
}
for (int j = 0; j< printingZhuanCangDanBeans.size(); j++){
printingZhuanCangDanBeans.get(j).setPrintUser(userDetails.getUsername());
}
JRDataSource source=new JRBeanCollectionDataSource(printingZhuanCangDanBeans);
JasperPrint jasperPrint = JasperFillManager.fillReport(path, parameters, source);
jasperPrintList.add(jasperPrint);
}else{
List<PrintingChuHuoDanBean> printingChuHuoDanBeans=vrDispatchOrderService.printingChuHuoDan(vrdoNos[i]);
if(EmptyUtils.isEmpty(printingChuHuoDanBeans)){
response.setCharacterEncoding("UTF-8");
response.sendError(404,"该托运单号"+vrdoNos[i]+"没有数据");
}
for (int j = 0; j< printingChuHuoDanBeans.size(); j++){
printingChuHuoDanBeans.get(j).setPrintUser(userDetails.getUsername());
}
JRDataSource source=new JRBeanCollectionDataSource(printingChuHuoDanBeans);
JasperPrint jasperPrint = JasperFillManager.fillReport(path2, parameters, source);
jasperPrintList.add(jasperPrint);
// JRDataSource source=new JRBeanCollectionDataSource(printingDispatchOrderBeans);
// JasperPrint jasperPrint = JasperFillManager.fillReport(path, parameters, source);
// jasperPrintList.add(jasperPrint);
}
}
//获取输出字节流
outputStream = response.getOutputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST,jasperPrintList);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
exporter.exportReport();
byte[] bytes= baos.toByteArray();
response.setContentType("application/pdf;charset=UTF-8");
outputStream.write(bytes);
outputStream.flush();
outputStream.close();
}