当前位置:网站首页>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
边栏推荐
- 移植tslib时ts_setup: No such file or directory、ts_open: No such file or director
- 2022G2电站锅炉司炉考试题库及在线模拟考试
- Knowledge of software testing~
- Optimization of especially slow startup in idea debugging mode
- After the mobile phone is connected to the computer, how can QT's QDIR read the mobile phone file path
- Explication détaillée des fonctions send () et recv () du programme Socket
- C language to achieve address book - (static version)
- . NETCORE sets the API post mode, which can accept parameters directly in parentheses
- There is no index in the database table. When inserting data, SQL statements are used to prevent repeated addition (Reprint)
- . net core current limiting control - aspnetcoreratelimit
猜你喜欢
超好用的【通用Excel导入功能】
“如何实现集中管理、灵活高效的CI/CD”在线研讨会精彩内容分享
Detailed description of MySQL index [B + tree index, hash index, full-text index, overlay index]
可以接收多种数据类型参数——可变参数
General test technology [II] test method
Use of slice grammar sugar in C #
Tencent video price rise: earn more than 7.4 billion a year! Pay attention to me to receive Tencent VIP members, and the weekly card is as low as 7 yuan
OLED multi-level menu record
《C语言程序设计》(谭浩强第五版) 第9章 用户自己建立数据类型 习题解析与答案
[vs Code] solve the problem that the jupyter file displays exceptions in vs code
随机推荐
Blazor University (12) - component lifecycle
TCP three handshakes and four waves
MySQL keyword group_ Concat, combined connection query
ASP. Net 6 middleware series - execution sequence
【VS Code】解决jupyter文件在vs code中显示异常的问题
场景题:A系统如何使用B系统的页面
js 中,为一个里面带有input 的label 绑定事件后在父元素绑定单机事件,事件执行两次,求解
Fight leetcode again (290. Word law)
C language to achieve address book - (static version)
Chapter 7 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of modular programming exercises with functions
C syntax pattern matching [switch expression]
2022年P气瓶充装培训试题及模拟考试
yes. Net future
. net tip: talk about the problem that the scoped service cannot be obtained in the middleware structure
oracle 查询外键含有逗号分隔的数据
This new feature of C 11, I would like to call it the strongest!
Use of metagroup object tuple in C
js递归树结构计算每个节点的叶子节点的数量并且输出
Oracle query foreign keys contain comma separated data
C WPF UI framework mahapps switching theme