public void downloadFiles (requête HttpServletRequest, réponse HttpServletResponse) lève ServletException, IOException
{ request.setCharacterEncoding ("UTF-8"); String docId = new String (request.getParameter ("docId"). GetBytes ("ISO-8859-1"), "UTF-8"); String downloadType = new String (request.getParameter ("downloadType"). GetBytes ("ISO-8859-1"), "UTF-8"); Document doc = null; essayez { doc = getFilePath (docId); // 此 方法 获取 文件 的 路径 , 这个 不 显示 } catch (SQLException ex) { ex.printStackTrace (); } Liste des éléments <Element> = doc.
List <File> files = new ArrayList <File> ();
index int = 0;
long fileLength = 0;
for (Elément ele: elements)
{ String filePath = FormatUtil.tryGetItemText (ele, "FILE_PATH", "") + FormatUtil.tryGetItemText (ele, "FILE_NAME", ""); Fichier fichier = nouveau fichier (filePath); files.add (fichier); fileLength + = file.length (); index ++; } String fileName = UUID.randomUUID (). ToString () + ".zip"; // 在 服务器 端 创建 打包 下载 的 临时 文件 String outFilePath = "C: \\" + fileName; Fichier fichier = nouveau fichier (outFilePath); // 文件 输出 流
FileOutputStream outStream = nouveau FileOutputStream (fichier);
// 压缩 流
ZipOutputStream toClient = new ZipOutputStream (outStream);
toClient.setEncoding ("gbk");
zipFile (fichiers, toClient);
toClient.close ();
outStream.close ();
this.downloadZip (fichier, réponse);
}
/ **
* 压缩 文件 列表 中 的 文件
* @param files
* @param
outputStream * @throws IOException
* /
public static void zipFile (Liste des fichiers, ZipOutputStream outputStream) jette IOException, ServletException
{ try { int size = files.size ( ); // 压缩 列表 中 的 文件
for (int i = 0; i <size; i ++)
{ File file = (File) files.get (i); zipFile (fichier, outputStream); } } catch (IOException e) { throw e; } } / ** * 将 文件 写入 到 zip 文件 中 * @param inputFile * @param outputstream * @throws Exception * / public static void zipFile (File inputFile, ZipOutputStream outputstream) jette IOException, ServletException { try { if (inputFile. existe ()) { if (inputFile.isFile ())
{ FileInputStream inStream = new FileInputStream (inputFile); BufferedInputStream bInStream = new BufferedInputStream (inStream); ZipEntry entry = new ZipEntry (inputFile.getName ()); outputstream.putNextEntry (entrée); final int MAX_BYTE / 1024 * 10 / Le plus grand flux fait 10M de long streamTotal = 0; // La capacité du flux reçu int streamNum = 0; // Le nombre de flux à séparer int LeaveByte = 0; // Le nombre de caractères restants dans le fichier byte [] inOutbyte; // byte Array accepte les données de fichier
streamTotal = bInStream.available (); // Obtient le nombre maximum de caractères du flux via la méthode disponible
streamNum = (int) Math.floor (streamTotal / MAX_BYTE); // Obtient le nombre de fichiers de flux séparés
LeaveByte = (int) streamTotal% MAX_BYTE ; // Après avoir divisé le fichier, le nombre restant
if (streamNum> 0)
{ for (int j = 0; j <streamNum; ++ j) { inOutbyte = new byte [MAX_BYTE]; // Lire le flux et le sauvegarder en octet Array bInStream.read (inOutbyte, 0, MAX_BYTE); outputstream.write (inOutbyte, 0, MAX_BYTE); // écrire le flux }
}
// 写出 剩下 的 流 数据
inOutbyte = nouvel octet [congéByte];
bInStream.read (inOutbyte, 0, LeaveByte);
outputstream.write (inOutbyte);
outputstream.closeEntry (); // Ferme l'entrée ZIP actuelle et positionne le flux pour écrire l'entrée suivante
bInStream.close (); // 关闭
inStream.close ();
}
}
else
{ throw new ServletException ("文件 不 存在!"); } } catch (IOException e) { throw e; } }
/ **
* 下载 打包 的 文件
* @param file
* @param response
* /
public void downloadZip (File file, HttpServletResponse response) { try { // 以 流 的 形式 下载 文件。 BufferedInputStream fis = new BufferedInputStream (new FileInputStream (file .getPath ())); byte [] buffer = nouvel octet [fis.available ()]; fis.read (tampon); fis.close (); // 清空 réponse response.reset (); OutputStream toClient = new BufferedOutputStream (response.getOutputStream ()); response.setContentType ("application / flux d'octets");
response.setHeader ("Content-Disposition", "attachment; filename =" + file.getName ());
toClient.write (buffer);
toClient.flush ();
toClient.close ();
file.delete (); / / Supprimer le fichier côté serveur généré
}
catch (IOException ex) { ex.printStackTrace (); } }
Téléchargement du package multi-fichier d'arrière-plan JAVA
Je suppose que tu aimes
Origine blog.csdn.net/FV8023/article/details/100132233
conseillé
Classement