当前位置:网站首页>Téléchargement en vrac de fichiers - téléchargement après compression
Téléchargement en vrac de fichiers - téléchargement après compression
2022-04-23 03:19:00 【Le professeur de maths écrit bug】
Téléchargez toutes les pièces jointes nécessaires et placez - les à l'endroit indiqué,Les pièces jointes à cet endroit sont ensuite emballées dans un fichier compressé,Puis télécharger.Facile à voir pour les utilisateurs.
public IZrarResponse downloadZdxjr(IZrarRequest req) throws ParseException, ParseException, IOException {
IZrarResponse res = new ZrarResponse();
List list = "Méthodes spécifiques d'obtention des données---Doit être écrit en fonction des besoins du projet";
String oid = "";//Ces trois valeurs sont nécessaires pour le projet
String wjmc = "";
String unitname = "";
String url = null;
JsonResult<FileObject> result = new JsonResult();
//Obtenir l'emplacement du chemin à partir du profil du système
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/";
}
// Si le chemin du fichier existe déjà, supprimer puis créer
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();
// Obtenir l'adresse demander le téléchargement de la pièce jointe
url = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/oss/download/" + oid + "?direct=1";
//Obtenir des renseignements sur les pièces jointes
getFile(url, "(" + unitname + ")" + wjmc, realPath);
}
String currentTime = DateUtils.getCurrentTime();
currentTime = currentTime.replaceAll("-", "").replaceAll(" ", "").replaceAll(":", "");
//Compresser le fichier
compressToZip(realPath, realPath0, "Programme de mise en œuvre" + currentTime + ".zip");
// Télécharger le fichier compressé
res.addStream(new File(realPath0 + "Programme de mise en œuvre" + currentTime + ".zip"));
return res;
}
/**
* @Description : Obtenir les fichiers joints Et écrire à l'endroit indiqué
* @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 Chemin du fichier source
* @param zipFilePath Chemin de stockage des fichiers compressés
* @param zipFilename Nom du fichier compressé
* @Description :Compresser le fichier
* @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);
// Une fois le fichier compressé , Supprimer les fichiers compressés
// boolean flag = deleteDir(sourceFile);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage(), e.getCause());
}
}
/**
* @param file Répertoire des fichiers sources
* @param parentPath Compresser le Répertoire de fichiers
* @param zos Flux de fichiers
* @Description : Traverser tous les fichiers,Compression
* @Author:
* @Date: 2022/4/22 15:49
*/
public static void writeZip(File file, String parentPath, ZipOutputStream zos) {
if (file.isDirectory()) {
// Table des matières
parentPath += file.getName() + File.separator;
File[] files = file.listFiles();
for (File f : files) {
writeZip(f, parentPath, zos);
}
} else {
// Documentation
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
// DésignationzipDossiers
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());
}
}
}
版权声明
本文为[Le professeur de maths écrit bug]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230316596040.html
边栏推荐
- . net tip: talk about the problem that the scoped service cannot be obtained in the middleware structure
- Web Course Design - his system
- Chapter 7 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of modular programming exercises with functions
- Iotos IOT middle platform is connected to the access control system of isecure center
- 数据库表中不建索引,在插入数据时,通过sql语句防止重复添加(转载)
- JS recursive tree structure calculates the number of leaf nodes of each node and outputs it
- be based on. NETCORE development blog project starblog - (2) environment preparation and creation project
- Yes Redis using distributed cache in NE6 webapi
- 可以接收多種數據類型參數——可變參數
- 批量下载文件----压缩后再下载
猜你喜欢
OLED multi-level menu record
It can receive multiple data type parameters - variable parameters
类似Jira的十大项目管理软件
《C语言程序设计》(谭浩强第五版) 第8章 善于利用指针 习题解析与答案
Data mining series (3)_ Data mining plug-in for Excel_ Estimation analysis
2022山东省安全员C证上岗证题库及在线模拟考试
2022a special equipment related management (elevator) work license question bank and simulation examination
This new feature of C 11, I would like to call it the strongest!
Explanation keyword of MySQL
The most understandable life cycle of dependency injection
随机推荐
Is it difficult to choose binary version control tools? After reading this article, you will find the answer
《C语言程序设计》(谭浩强第五版) 第8章 善于利用指针 习题解析与答案
Eight elder brothers chronicle [4]
. 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 - (2) environment preparation and creation project
Yes Redis using distributed cache in NE6 webapi
Use of metagroup object tuple in C
IDEA查看历史记录【文件历史和项目历史】
MySQL keyword group_ Concat, combined connection query
Knowledge of software testing~
数据库表中不建索引,在插入数据时,通过sql语句防止重复添加(转载)
Quartz. Www. 18fu Used in net core
MySql关键字GROUP_CONCAT,组合连接查询
C syntax sugar empty merge operator [?] And null merge assignment operator [? =]
[MySQL] left function | right function
C introduction of variable parameter params
数据挖掘系列(3)_Excel的数据挖掘插件_估计分析
Cefsharp stores cookies and reads cookies
How does Microsoft solve the problem of multiple programs on PC side -- internal implementation
《C语言程序设计》(谭浩强第五版) 第7章 用函数实现模块化程序设计 习题解析与答案