Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Q
qianhe-ydsj
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
jiangyun
qianhe-ydsj
Commits
a57e8ed0
Commit
a57e8ed0
authored
Aug 07, 2025
by
wangjian
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
2025-08-07 规范建设 V5
parent
6e2070d1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
150 additions
and
59 deletions
+150
-59
qianhe-ydsj/src/main/java/com/qianhe/controller/SjZdscController.java
+150
-59
No files found.
qianhe-ydsj/src/main/java/com/qianhe/controller/SjZdscController.java
View file @
a57e8ed0
...
...
@@ -15,6 +15,8 @@ import com.qianhe.common.utils.ip.IpUtils;
import
com.qianhe.domain.GgFjb
;
import
com.qianhe.domain.SjZdsc
;
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.web.bind.annotation.*
;
import
com.qianhe.common.annotation.Log
;
...
...
@@ -115,33 +117,102 @@ public class SjZdscController extends BaseController
* 批量导出
* 导出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
})
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
());
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
.
setContentType
(
"application/zip"
);
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;
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
();
//附件名称
...
...
@@ -149,66 +220,86 @@ public class SjZdscController extends BaseController
String
localPath
=
RuoYiConfig
.
getProfile
();
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. 读取文件内容(改进版)
byte
[]
fileBytes
;
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
;
}
// 将Excel转换为PDF
byte
[]
pdfBytes
=
getFileBytes
(
downloadPath
);
// 3. 处理文件名(避免特殊字符)
String
safeFileName
=
fjmc
.
replaceAll
(
"[\\\\/:*?\"<>|]"
,
"_"
);
// 创建ZIP条目
ZipEntry
entry
=
new
ZipEntry
(
safeFileName
);
zos
.
putNextEntry
(
entry
);
// 4. 写入文件内容(带验证)
zos
.
write
(
fileBytes
);
zos
.
flush
();
// 强制刷新
// 写入PDF文件到ZIP条目
zos
.
write
(
pdfBytes
);
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. 确保至少有一个有效文件被添加
if
(!
hasValidFile
)
{
throw
new
BusinessException
(
"没有可导出的有效文件"
);
byte
[]
buffer
=
new
byte
[
8192
];
// 8KB缓冲区
int
bytesRead
;
// 循环读取文件内容到字节数组输出流
while
((
bytesRead
=
bis
.
read
(
buffer
))
!=
-
1
)
{
baos
.
write
(
buffer
,
0
,
bytesRead
);
}
// 6. 完成ZIP文件(关键步骤)
zos
.
finish
();
response
.
flushBuffer
();
// 确保所有数据发送到客户端
// 强制刷新缓冲区,确保所有数据都被写入
baos
.
flush
();
return
baos
.
toByteArray
();
}
catch
(
BusinessException
e
)
{
System
.
out
.
println
(
"导出业务异常"
+
e
);
throw
e
;
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
"导出ZIP文件异常"
+
e
);
throw
new
BusinessException
(
"导出文件失败,请联系管理员"
);
}
catch
(
FileNotFoundException
e
)
{
System
.
out
.
println
(
"文件未找到: "
+
filePath
+
",错误: "
+
e
.
getMessage
());
}
catch
(
IOException
e
)
{
System
.
out
.
println
(
"读取文件失败: "
+
filePath
+
",错误: "
+
e
.
getMessage
());
}
return
null
;
}
/**
* 批量提交
*/
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment