当前位置:网站首页>批量下載文件----壓縮後再下載
批量下載文件----壓縮後再下載
2022-04-23 03:18:00 【數學教師寫BUG】
把所需要的的附件全部下載下來放在指定的比特置,然後對此比特置上的附件全部打包成壓縮文件,然後再下載下來。方便用戶查看。
public IZrarResponse downloadZdxjr(IZrarRequest req) throws ParseException, ParseException, IOException {
IZrarResponse res = new ZrarResponse();
List list = "具體的獲取數據的方法---需要根據項目需求寫";
String oid = "";//這三個是項目所需要的值
String wjmc = "";
String unitname = "";
String url = null;
JsonResult<FileObject> result = new JsonResult();
//從系統配置文件獲取路徑比特置
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/";
}
//要是文件路徑已存在則删除再新建
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();
//獲取地址請求下載附件
url = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/oss/download/" + oid + "?direct=1";
//獲取附件信息
getFile(url, "(" + unitname + ")" + wjmc, realPath);
}
String currentTime = DateUtils.getCurrentTime();
currentTime = currentTime.replaceAll("-", "").replaceAll(" ", "").replaceAll(":", "");
//壓縮文件
compressToZip(realPath, realPath0, "實施方案" + currentTime + ".zip");
//下載壓縮後的文件
res.addStream(new File(realPath0 + "實施方案" + currentTime + ".zip"));
return res;
}
/**
* @Description : 獲取附件文件 並寫入指定比特置
* @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 源文件路徑
* @param zipFilePath 壓縮後文件存儲路徑
* @param zipFilename 壓縮文件名
* @Description :壓縮文件
* @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);
// 文件壓縮完成後,删除被壓縮文件
// boolean flag = deleteDir(sourceFile);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage(), e.getCause());
}
}
/**
* @param file 源文件目錄
* @param parentPath 壓縮文件目錄
* @param zos 文件流
* @Description : 遍曆所有文件,壓縮
* @Author:
* @Date: 2022/4/22 15:49
*/
public static void writeZip(File file, String parentPath, ZipOutputStream zos) {
if (file.isDirectory()) {
// 目錄
parentPath += file.getName() + File.separator;
File[] files = file.listFiles();
for (File f : files) {
writeZip(f, parentPath, zos);
}
} else {
// 文件
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
// 指定zip文件夾
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());
}
}
}
版权声明
本文为[數學教師寫BUG]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230316596040.html
边栏推荐
- Chapter 7 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of modular programming exercises with functions
- Detailed explanation of socket programming send() and recv() functions
- 类似Jira的十大项目管理软件
- C introduction of variable parameter params
- Super easy to use [general excel import function]
- Fight leetcode again (290. Word law)
- Explication détaillée des fonctions send () et recv () du programme Socket
- [authentication / authorization] customize an authentication handler
- MySQL grouping query rules
- Top ten project management software similar to JIRA
猜你喜欢

“如何实现集中管理、灵活高效的CI/CD”在线研讨会精彩内容分享

. net tip: talk about the problem that the scoped service cannot be obtained in the middleware structure

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

Web Course Design - his system

2022年P气瓶充装培训试题及模拟考试

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

Super easy to use asynchronous export function of Excel

xutils3修改了我提报的一个bug,开心

Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types

This new feature of C 11, I would like to call it the strongest!
随机推荐
Student achievement management
一文了解全面静态代码分析
C introduction of variable parameter params
Chapter 7 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of modular programming exercises with functions
MySQL索引详解【B+Tree索引、哈希索引、全文索引、覆盖索引】
2022山东省安全员C证上岗证题库及在线模拟考试
[Mysql] LEFT函数 | RIGHT函数
超好用的Excel异步导出功能
Configure automatic implementation of curd projects
Improvement of ref and struct in C 11
可以接收多种数据类型参数——可变参数
[authentication / authorization] customize an authentication handler
Fundamentals of software testing and development
关于idea调试模式下启动特别慢的优化
12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list
Chapter 8 of C language programming (fifth edition of Tan Haoqiang) is good at using pointer exercises to analyze and answer
Iotos IOT middle platform is connected to the access control system of isecure center
软件测试相关知识~
Knowledge of software testing~
General testing technology [1] classification of testing