当前位置:网站首页>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
边栏推荐
- Comprehensive calculation of employee information
- Do you really understand hashcode and equals???
- Log4net is in Net core usage
- Experiment 5 components and event handling
- Node configuration environment CMD does not take effect
- MySQL keyword group_ Concat, combined connection query
- IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统
- How to achieve centralized management, flexible and efficient CI / CD online seminar highlights sharing
- Xamarin effect Chapter 22 recording effect
- 《C语言程序设计》(谭浩强第五版) 第7章 用函数实现模块化程序设计 习题解析与答案
猜你喜欢

关于idea调试模式下启动特别慢的优化

【VS Code】解决jupyter文件在vs code中显示异常的问题

Optimization of especially slow startup in idea debugging mode

12.<tag-链表和常考点综合>-lt.234-回文链表

How to achieve centralized management, flexible and efficient CI / CD online seminar highlights sharing

Comprehensive calculation of employee information
![Detailed description of MySQL index [B + tree index, hash index, full-text index, overlay index]](/img/1a/a22b4a35d3c083438d0f766a5ecb08.png)
Detailed description of MySQL index [B + tree index, hash index, full-text index, overlay index]

一套组合拳,打造一款 IDEA 护眼方案

Fight leetcode again (290. Word law)
![[MySQL] left function | right function](/img/26/82e0f2280de011636c26931a74e749.png)
[MySQL] left function | right function
随机推荐
[MySQL] left Function | Right Function
Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types
Yes Redis using distributed cache in NE6 webapi
幂等性实践操作,基于业务讲解幂等性
poi根据数据创建导出excel
《C语言程序设计》(谭浩强第五版) 第7章 用函数实现模块化程序设计 习题解析与答案
ASP. Net 6 middleware series - execution sequence
This new feature of C 11, I would like to call it the strongest!
[MySQL] left function | right function
Flink real-time data warehouse project - Design and implementation of DWS layer
Peut recevoir plusieurs paramètres de type de données - paramètres variables
2022a special equipment related management (elevator) work license question bank and simulation examination
. net tip: talk about the problem that the scoped service cannot be obtained in the middleware structure
be based on. NETCORE development blog project starblog - (1) why do you need to write your own blog?
WinForm allows the form form to switch between the front and active states
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
Experiment 5 components and event handling
Student achievement management
Fiddler use
2022年度Top9的任务管理系统