当前位置:网站首页>批量下載文件----壓縮後再下載
批量下載文件----壓縮後再下載
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
边栏推荐
- Fundamentals of software testing and development
- 关于idea调试模式下启动特别慢的优化
- Configure automatic implementation of curd projects
- ThreadLocal test multithreaded variable instance
- 你真的懂hashCode和equals吗???
- 全新的ORM框架——BeetlSQL介绍
- Mysql database, inconsistent index character set, slow SQL query, interface timeout
- Top ten project management software similar to JIRA
- Comprehensive calculation of employee information
- Scenario Title: how does system a use the page of system B
猜你喜欢

Huawei mobile ADB devices connection device is empty

be based on. NETCORE development blog project starblog - (1) why do you need to write your own blog?
![[MySQL] left function | right function](/img/26/82e0f2280de011636c26931a74e749.png)
[MySQL] left function | right function

PID debugging of coding motor (speed loop | position loop | follow)

12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list

Impact of AOT and single file release on program performance

研讨会回放视频:如何提升Jenkins能力,使其成为真正的DevOps平台
![[Mysql] LEFT函数 | RIGHT函数](/img/26/82e0f2280de011636c26931a74e749.png)
[Mysql] LEFT函数 | RIGHT函数

ASP. Net 6 middleware series - Custom middleware classes

C语言实现通讯录----(静态版本)
随机推荐
Fight leetcode again (290. Word law)
Why is bi so important to enterprises?
Detailed explanation of socket programming send() and recv() functions
OLED多级菜单记录
你真的懂hashCode和equals吗???
2022g2 boiler stoker examination question bank and online simulation examination
ASP. Net and ASP NETCORE multi environment configuration comparison
超好用的Excel异步导出功能
Aspnetcore configuration multi environment log4net configuration file
Mysql database, inconsistent index character set, slow SQL query, interface timeout
MySQL keyword group_ Concat, combined connection query
Fundamentals of software testing and development
Explication détaillée des fonctions send () et recv () du programme Socket
js 中,为一个里面带有input 的label 绑定事件后在父元素绑定单机事件,事件执行两次,求解
关于idea调试模式下启动特别慢的优化
Log4net is in Net core usage
After the mobile phone is connected to the computer, how can QT's QDIR read the mobile phone file path
2022年P气瓶充装培训试题及模拟考试
General testing technology [1] classification of testing
12.<tag-链表和常考点综合>-lt.234-回文链表