Commit a57e8ed0 by wangjian

2025-08-07 规范建设 V5

parent 6e2070d1
...@@ -15,6 +15,8 @@ import com.qianhe.common.utils.ip.IpUtils; ...@@ -15,6 +15,8 @@ import com.qianhe.common.utils.ip.IpUtils;
import com.qianhe.domain.GgFjb; import com.qianhe.domain.GgFjb;
import com.qianhe.domain.SjZdsc; import com.qianhe.domain.SjZdsc;
import com.qianhe.service.IGgFjbService; import com.qianhe.service.IGgFjbService;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import com.qianhe.common.annotation.Log; import com.qianhe.common.annotation.Log;
...@@ -115,33 +117,102 @@ public class SjZdscController extends BaseController ...@@ -115,33 +117,102 @@ public class SjZdscController extends BaseController
* 批量导出 * 批量导出
* 导出zip * 导出zip
*/ */
// @RequestMapping(value = "/exportZip", method = {RequestMethod.GET, RequestMethod.POST})
// public void exportZip(HttpServletResponse response, @RequestBody SjZdsc sjZdsc) throws IOException
// {
// Long id = Long.parseLong(sjZdsc.getIds());
// SjZdsc en = sjZdscService.selectSjZdscById(id);
// String zdmc = en.getZdmc();
// String bb = en.getBb();
// List<GgFjb> fjs = en.getFileList1();
//
// String ip = IpUtils.getIpAddr();
//
// // 设置响应头
// response.setContentType("application/zip");
//// response.setContentType("application/octet-stream");
// response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(zdmc + " " + bb + ".zip", "UTF-8"));
// response.setCharacterEncoding("UTF-8");
//
//// Workbook exl = null;
//// OutputStream out = null;
//// InputStream in = null;
//
// // 创建ZIP输出流
// try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) {
//
// // 临时变量用于验证是否有有效文件被添加
// boolean hasValidFile = false;
//
// for(GgFjb fj:fjs) {
// String fjlj = fj.getFJDZ();//附件路径
// String fjmc = fj.getFJMC();//附件名称
//
// String localPath = RuoYiConfig.getProfile();
// String downloadPath = localPath + StringUtils.substringAfter(fjlj, Constants.RESOURCE_PREFIX);
//
// // 1. 验证文件存在性和可读性
// File file = new File(downloadPath);
// // 验证文件
// if (!file.exists() || !file.isFile() || !file.canRead() || file.length() == 0) {
// System.out.println("跳过无效文件: " + downloadPath);
// continue;
// }
//
// try (FileInputStream fis = new FileInputStream(file)) {
// // 3. 处理文件名(避免特殊字符)
// String safeFileName = fjmc.replaceAll("[\\\\/:*?\"<>|]", "_");
// ZipEntry entry = new ZipEntry(safeFileName);
// zos.putNextEntry(entry);
//
// // 使用缓冲区流式传输文件内容
// byte[] buffer = new byte[4096];
// int bytesRead;
// while ((bytesRead = fis.read(buffer)) != -1) {
// zos.write(buffer, 0, bytesRead);
// }
// zos.closeEntry();
// hasValidFile = true;
// } catch (Exception e) {
// System.out.println("处理文件失败: " + downloadPath + " - " + e.getMessage());
// // 确保关闭当前条目
// try {
// zos.closeEntry();
// } catch (IOException ex) {
// // 忽略关闭异常
// }
// }
// }
//
//
// if (!hasValidFile) {
// throw new BusinessException("没有可导出的有效文件");
// }
// } catch (BusinessException e) {
// throw e;
// } catch (Exception e) {
// throw new BusinessException("导出文件失败: " + e.getMessage());
// }
// }
@RequestMapping(value = "/exportZip", method = {RequestMethod.GET, RequestMethod.POST}) @RequestMapping(value = "/exportZip", method = {RequestMethod.GET, RequestMethod.POST})
public void exportZip(HttpServletResponse response, @RequestBody SjZdsc sjZdsc) throws IOException public void exportZip(HttpServletResponse response, @RequestBody SjZdsc sjZdsc) throws IOException {
{
Long id = Long.parseLong(sjZdsc.getIds()); Long id = Long.parseLong(sjZdsc.getIds());
SjZdsc en = sjZdscService.selectSjZdscById(id); SjZdsc en = sjZdscService.selectSjZdscById(id);
String zdmc = en.getZdmc(); String zdmc = en.getZdmc();
String bb = en.getBb(); String bb = en.getBb();
List<GgFjb> fjs = en.getFileList1(); List<GgFjb> fjs = en.getFileList1();
String ip = IpUtils.getIpAddr();
// 设置响应头 // 设置响应头
// response.setContentType("application/zip"); response.setContentType("application/zip");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(zdmc + " " + bb + ".zip", "UTF-8")); response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(zdmc + " " + bb + ".zip", "UTF-8"));
response.setCharacterEncoding("UTF-8");
// Workbook exl = null; Workbook exl = null;
// OutputStream out = null; OutputStream out = null;
// InputStream in = null; InputStream in = null;
// 创建ZIP输出流 // 创建ZIP输出流
try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) { try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) {
// 临时变量用于验证是否有有效文件被添加
boolean hasValidFile = false;
for(GgFjb fj:fjs) { for(GgFjb fj:fjs) {
String fjlj = fj.getFJDZ();//附件路径 String fjlj = fj.getFJDZ();//附件路径
String fjmc = fj.getFJMC();//附件名称 String fjmc = fj.getFJMC();//附件名称
...@@ -149,66 +220,86 @@ public class SjZdscController extends BaseController ...@@ -149,66 +220,86 @@ public class SjZdscController extends BaseController
String localPath = RuoYiConfig.getProfile(); String localPath = RuoYiConfig.getProfile();
String downloadPath = localPath + StringUtils.substringAfter(fjlj, Constants.RESOURCE_PREFIX); String downloadPath = localPath + StringUtils.substringAfter(fjlj, Constants.RESOURCE_PREFIX);
// 1. 验证文件存在性和可读性
File file = new File(downloadPath);
if (!file.exists()) {
System.out.println("文件不存在: {}" + downloadPath);
continue;
}
if (!file.canRead()) {
System.out.println("文件不可读: {}" + downloadPath);
continue;
}
if (file.length() == 0) {
System.out.println("文件为空: {}" + downloadPath);
continue;
}
// 2. 读取文件内容(改进版) // 将Excel转换为PDF
byte[] fileBytes; byte[] pdfBytes = getFileBytes(downloadPath);
try (FileInputStream fis = new FileInputStream(file)) {
fileBytes = new byte[(int) file.length()];
int bytesRead = fis.read(fileBytes);
// 验证读取的字节数是否与文件大小一致
if (bytesRead != file.length()) {
System.out.println("文件读取不完整,预期: {},实际: {}" + file.length() + bytesRead);
continue;
}
hasValidFile = true;
} catch (Exception e) {
System.out.println("读取文件异常: {}" + downloadPath + e);
continue;
}
// 3. 处理文件名(避免特殊字符)
String safeFileName = fjmc.replaceAll("[\\\\/:*?\"<>|]", "_"); String safeFileName = fjmc.replaceAll("[\\\\/:*?\"<>|]", "_");
// 创建ZIP条目
ZipEntry entry = new ZipEntry(safeFileName); ZipEntry entry = new ZipEntry(safeFileName);
zos.putNextEntry(entry); zos.putNextEntry(entry);
// 写入PDF文件到ZIP条目
// 4. 写入文件内容(带验证) zos.write(pdfBytes);
zos.write(fileBytes);
zos.flush(); // 强制刷新
zos.closeEntry(); zos.closeEntry();
} }
}catch (Exception e){
e.getMessage();
throw new BusinessException("导出Excel失败,请联系网站管理员!");
}finally {
if (exl != null) {
try {
exl.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
/**
* 根据文件路径获取字节数组
* @param filePath 文件绝对路径
* @return 文件内容的字节数组,若文件不存在或读取失败则返回null
*/
public static byte[] getFileBytes(String filePath) {
File file = new File(filePath);
// 验证文件是否存在且为正常文件
if (!file.exists() || !file.isFile()) {
System.out.println("文件不存在或不是有效文件: " + filePath);
return null;
}
// 验证文件大小(避免过大文件导致内存溢出)
if (file.length() > Integer.MAX_VALUE) {
System.out.println("文件过大,不适合转为byte[]: " + filePath);
return null;
}
try (FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
// 5. 确保至少有一个有效文件被添加 byte[] buffer = new byte[8192]; // 8KB缓冲区
if (!hasValidFile) { int bytesRead;
throw new BusinessException("没有可导出的有效文件");
// 循环读取文件内容到字节数组输出流
while ((bytesRead = bis.read(buffer)) != -1) {
baos.write(buffer, 0, bytesRead);
} }
// 6. 完成ZIP文件(关键步骤) // 强制刷新缓冲区,确保所有数据都被写入
zos.finish(); baos.flush();
response.flushBuffer(); // 确保所有数据发送到客户端
return baos.toByteArray();
} catch (BusinessException e) { } catch (FileNotFoundException e) {
System.out.println("导出业务异常" + e); System.out.println("文件未找到: " + filePath + ",错误: " + e.getMessage());
throw e; } catch (IOException e) {
} catch (Exception e) { System.out.println("读取文件失败: " + filePath + ",错误: " + e.getMessage());
System.out.println("导出ZIP文件异常" + e);
throw new BusinessException("导出文件失败,请联系管理员");
} }
return null;
} }
/** /**
* 批量提交 * 批量提交
*/ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment