当前位置:网站首页>Batch download of files ---- compressed and then downloaded
Batch download of files ---- compressed and then downloaded
2022-04-23 03:19:00 【Math teacher writes bug】
Download all the required attachments and put them in the designated location , Then all the attachments in this location are packaged into compressed files , Then download it . Convenient for users to view .
public IZrarResponse downloadZdxjr(IZrarRequest req) throws ParseException, ParseException, IOException {
IZrarResponse res = new ZrarResponse();
List list = " Specific methods of obtaining data --- It needs to be written according to the project requirements ";
String oid = "";// These three are the values required for the project
String wjmc = "";
String unitname = "";
String url = null;
JsonResult<FileObject> result = new JsonResult();
// Get the path location from the system configuration file
String realPath0 = PropertyManager.getProperty("CommonUploadFileUrl", new String[0]);
String realPath = realPath0.replace("\\", "/");
if (realPath.lastIndexOf("/") == realPath.length() - 1) {
realPath = realPath + "ssfa/";
} else {
realPath = realPath + "/ssfa/";
}
// If the file path already exists, delete it and then create a new one
File f = new File(realPath);
if (f.exists()) {
FileUtils.deleteDirectory(f);
f.mkdirs();
} else {
f.mkdirs();
}
for (int i = 0; i < list.size(); i++) {
Map map = (Map) list.get(i);
oid = (String) map.get("oid");
wjmc = (String) map.get("wjmc");
unitname = (String) map.get("unitname");
if (StringUtil.isNull(oid)) {
result.setErrorMsg("oid can not be null");
return res.addResHtml(result.toJson());
}
HttpServletRequest request = req.getHttpServletRequest();
// Get the address and request to download the attachment
url = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/oss/download/" + oid + "?direct=1";
// Get attachment information
getFile(url, "(" + unitname + ")" + wjmc, realPath);
}
String currentTime = DateUtils.getCurrentTime();
currentTime = currentTime.replaceAll("-", "").replaceAll(" ", "").replaceAll(":", "");
// Compressed files
compressToZip(realPath, realPath0, " Implementation plan " + currentTime + ".zip");
// Download the compressed file
res.addStream(new File(realPath0 + " Implementation plan " + currentTime + ".zip"));
return res;
}
/**
* @Description : Get attachment file And write to the specified location
* @Author:
* @Date: 2022/4/22 15:42
*/
private boolean getFile(String urlstr, String filename, String filepath) {
InputStream in = null;
HttpURLConnection conn = null;
FileOutputStream os = null;
try {
URL url = new URL(urlstr);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setConnectTimeout(5000);
conn.setReadTimeout(60000);
in = conn.getInputStream();
os = new FileOutputStream(filepath + filename);
byte[] buf = new byte[8 * 1024];
int len;
while ((len = in.read(buf)) != -1) {
os.write(buf, 0, len);
}
os.flush();
if (os instanceof FileOutputStream) {
((FileOutputStream) os).getFD().sync();
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
}
}
if (null != conn) {
conn.disconnect();
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
}
}
}
}
/**
* @param sourceFilePath Source file path
* @param zipFilePath Compressed file storage path
* @param zipFilename Compressed file name
* @Description : Compressed files
* @Author:
* @Date: 2022/4/22 15:48
*/
public static void compressToZip(String sourceFilePath, String zipFilePath, String zipFilename) {
File sourceFile = new File(sourceFilePath);
File zipPath = new File(zipFilePath);
if (!zipPath.exists()) {
zipPath.mkdirs();
}
File zipFile = new File(zipPath + File.separator + zipFilename);
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
writeZip(sourceFile, "", zos);
// After the file is compressed , Delete compressed files
// boolean flag = deleteDir(sourceFile);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage(), e.getCause());
}
}
/**
* @param file Source directory
* @param parentPath Compressed file directory
* @param zos File stream
* @Description : Traverse all files , Compress
* @Author:
* @Date: 2022/4/22 15:49
*/
public static void writeZip(File file, String parentPath, ZipOutputStream zos) {
if (file.isDirectory()) {
// Catalog
parentPath += file.getName() + File.separator;
File[] files = file.listFiles();
for (File f : files) {
writeZip(f, parentPath, zos);
}
} else {
// file
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
// Appoint zip Folder
ZipEntry zipEntry = new ZipEntry(parentPath + file.getName());
zos.putNextEntry(zipEntry);
int len;
byte[] buffer = new byte[1024 * 10];
while ((len = bis.read(buffer, 0, buffer.length)) != -1) {
zos.write(buffer, 0, len);
zos.flush();
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage(), e.getCause());
}
}
}
版权声明
本文为[Math teacher writes bug]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230316596040.html
边栏推荐
- poi根据数据创建导出excel
- Xamarin effect Chapter 22 recording effect
- LoadRunner - performance testing tool
- PID debugging of coding motor (speed loop | position loop | follow)
- . net core current limiting control - aspnetcoreratelimit
- 2022 P cylinder filling training test questions and simulation test
- MySQL installation pit
- yes. Net future
- Improvement of ref and struct in C 11
- Explication détaillée des fonctions send () et recv () du programme Socket
猜你喜欢
![Idea view history [file history and project history]](/img/b2/3128105eca7449c55146ce3b9e5c2e.png)
Idea view history [file history and project history]

《C语言程序设计》(谭浩强第五版) 第7章 用函数实现模块化程序设计 习题解析与答案

C language to achieve address book - (static version)

ASP. Net 6 middleware series - execution sequence

Quartz. Www. 18fu Used in net core

IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统

《C语言程序设计》(谭浩强第五版) 第9章 用户自己建立数据类型 习题解析与答案

Fight leetcode again (290. Word law)

OLED多级菜单记录

Blazor University (12) - component lifecycle
随机推荐
OLED多级菜单记录
be based on. NETCORE development blog project starblog - (2) environment preparation and creation project
Seminar playback video: how to improve Jenkins' ability to become a real Devops platform
2022年度Top9的任务管理系统
This new feature of C 11, I would like to call it the strongest!
Blazor University (12) - component lifecycle
[MySQL] left Function | Right Function
【无标题】
MySQL keyword group_ Concat, combined connection query
研讨会回放视频:如何提升Jenkins能力,使其成为真正的DevOps平台
Impact of AOT and single file release on program performance
How does Microsoft solve the problem of multiple programs on PC side -- internal implementation
[Mysql] LEFT函數 | RIGHT函數
“如何实现集中管理、灵活高效的CI/CD”在线研讨会精彩内容分享
There is no index in the database table. When inserting data, SQL statements are used to prevent repeated addition (Reprint)
WinForm allows the form form to switch between the front and active states
《C语言程序设计》(谭浩强第五版) 第9章 用户自己建立数据类型 习题解析与答案
Use of metagroup object tuple in C
MySQL grouping query rules
The most easy to understand service container and scope of dependency injection