当前位置:网站首页>批量下載文件----壓縮後再下載
批量下載文件----壓縮後再下載
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
边栏推荐
- MySQL之explain关键字详解
- be based on. NETCORE development blog project starblog - (2) environment preparation and creation project
- C language to achieve address book - (static version)
- IDEA查看历史记录【文件历史和项目历史】
- [untitled]
- Explication détaillée des fonctions send () et recv () du programme Socket
- Comprehensive calculation of employee information
- Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types
- 2022a special equipment related management (elevator) work license question bank and simulation examination
- 【VS Code】解决jupyter文件在vs code中显示异常的问题
猜你喜欢
xutils3修改了我提报的一个bug,开心
2022 P cylinder filling training test questions and simulation test
[Mysql] LEFT函數 | RIGHT函數
C WPF UI framework mahapps switching theme
General testing technology [1] classification of testing
How to achieve centralized management, flexible and efficient CI / CD online seminar highlights sharing
This new feature of C 11, I would like to call it the strongest!
Utgard connection opcserver reported an error caused by: org jinterop. dcom. common. JIRuntimeException: Access is denied. [0x800
LoadRunner - performance testing tool
Super easy to use [general excel import function]
随机推荐
Xutils3 corrected a bug I reported. Happy
JS inheritance
2022g2 boiler stoker examination question bank and online simulation examination
yes. Net future
[untitled]
The website JS in. Net core cefsharp chromium WebBrowser calls the C method in winfrom program
[Mysql] LEFT函數 | RIGHT函數
【VS Code】解决jupyter文件在vs code中显示异常的问题
[Mysql] LEFT函数 | RIGHT函数
Configure automatic implementation of curd projects
Iotos IOT middle platform is connected to the access control system of isecure center
[MySQL] left function | right function
Fiddler use
PID debugging of coding motor (speed loop | position loop | follow)
Blazor University (12) - component lifecycle
Scenario Title: how does system a use the page of system B
Swap the left and right of each node in a binary tree
通过 zxing 生成二维码
ThreadLocal 测试多线程变量实例
A set of combination boxing to create an idea eye protection scheme