public void downloadWaterPack(@PathVariable String year, @PathVariable Long id,
HttpServletResponse response,
HttpServletRequest request,
ModelMap modelMap) throws Exception {
String dirNoData = RuoYiConfig.getUploadPath() + "/noData";
File dirNoDataFile = new File(dirNoData);
if (!dirNoDataFile.exists()) {
dirNoDataFile.mkdirs();
}
String fileNoDataAbs = dirNoDataFile + "/" + "暂无数据.txt";
File fileNoData = new File(fileNoDataAbs);
if (!fileNoData.exists()) {
fileNoData.createNewFile();
}
if (id == -1) {
downloadNoData(response, fileNoDataAbs);
return;
}
NyysFarmer farmer = new NyysFarmer();
farmer.setDeptId(id);
farmer.getParams().put("year", year);
String uploadDir = RuoYiConfig.getUploadPath() + "/quotaPdf/" + id + "/" + year + "/";
File dir = new File(uploadDir);
if (!dir.exists()) {
dir.mkdirs();
}
List<NyysFarmerExt> list = nyysFarmerService.selectNyysFarmerQuotaList(farmer);
if (list.size() == 0) {
downloadNoData(response, fileNoDataAbs);
return;
}
List<String> uploadFileNameAbsList = new ArrayList<>();
for (NyysFarmerExt nyysFarmerExt : list) {
nyysFarmerExt.setTotalUnit("");
nyysFarmerExt.setYear(year);
String fileName = nyysFarmerExt.getYear() + "-用水权证-" + nyysFarmerExt.getFarmerName() + "-" + nyysFarmerExt.getFarmerId() + ".pdf";
String uploadFileNameAbs = uploadDir + fileName;
uploadFileNameAbsList.add(uploadFileNameAbs);
FileOutputStream fos = new FileOutputStream(uploadFileNameAbs);
drawPdf(nyysFarmerExt, fos);
}
SysDept sysDept = deptService.selectDeptById(id);
String zipName = year + "-用水权证-" + sysDept.getDeptName() + "-打包.zip";
String zipDir = RuoYiConfig.getUploadPath() + "/quotaZip/" + id + "/" + year + "/";
File zipDirFileObj = new File(zipDir);
if (!zipDirFileObj.exists()) {
zipDirFileObj.mkdirs();
}
String zipFileNameAbs = zipDir + zipName;
ZipOutputStream zipOutput = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFileNameAbs)));
BufferedInputStream bis = null;
for (String pdfFileNameAbs : uploadFileNameAbsList) {
File fileTemp = new File(pdfFileNameAbs);
ZipEntry zEntry = new ZipEntry(fileTemp.getName());
zipOutput.putNextEntry(zEntry);
bis = new BufferedInputStream(new FileInputStream(fileTemp));
byte[] buffer = new byte[1024];
int read = 0;
while((read = bis.read(buffer)) != -1){
zipOutput.write(buffer, 0, read);
}
}
bis.close();
zipOutput.close();
ServletOutputStream sos = response.getOutputStream();
FileInputStream in = new FileInputStream(zipFileNameAbs);
response.setHeader("Content-Type","application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(zipName, "UTF-8"));
byte[] buffer2 = new byte[1024];
int read2 = 0;
while((read2 = in.read(buffer2)) != -1){
sos.write(buffer2, 0, read2);
}
File zipFile = new File(zipDir + zipName);
if (zipFile.exists()) {
zipFile.delete();
}
in.close();
}
private void downloadNoData(HttpServletResponse response, String fileNameAbs) throws Exception {
ServletOutputStream sos = response.getOutputStream();
FileInputStream in = new FileInputStream(fileNameAbs);
response.setHeader("Content-Type","application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("暂无数据.txt", "UTF-8"));
int len = 0;
while((len = in.read()) != -1){
sos.write(len);
}
sos.close();
in.close();
}
void toHttpServletResponse(HttpServletResponse response, NyysFarmerExt farmerData) throws Exception {
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment;fileName=" + farmerData.getYear() + "-" + URLEncoder.encode("用水权证", StandardCharsets.UTF_8.name()) + "-" + URLEncoder.encode(farmerData.getFarmerName(), StandardCharsets.UTF_8.name()) + ".pdf");
ServletOutputStream outputStream = response.getOutputStream();
drawPdf(farmerData, outputStream);
}