public void downloadFiles (solicitação HttpServletRequest, resposta HttpServletResponse) lança 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"); Documento doc = null; tente { doc = getFilePath (docId); // 此 方法 获取 文件 的 路径, 这个 不 显示 } catch (SQLException ex) { ex.printStackTrace (); } Lista de elementos <Element> = doc.
Listar arquivos <File> = new ArrayList <File> ();
índice interno = 0;
long fileLength = 0;
for (Elemento ele: elementos)
{ String filePath = FormatUtil.tryGetItemText (ele, "FILE_PATH", "") + FormatUtil.tryGetItemText (ele, "FILE_NAME", ""); Arquivo arquivo = novo arquivo (filePath); files.add (arquivo); fileLength + = file.length (); index ++; } String fileName = UUID.randomUUID (). ToString () + ".zip"; // 在 服务器 端 创建 打包 下载 的 临时 文件 String outFilePath = "C: \\" + fileName; Arquivo arquivo = novo arquivo (outFilePath); // 文件 输出 流
FileOutputStream outStream = new FileOutputStream (arquivo);
// 压缩 流
ZipOutputStream toClient = new ZipOutputStream (outStream);
toClient.setEncoding ("gbk");
zipFile (arquivos, toClient);
toClient.close ();
outStream.close ();
this.downloadZip (arquivo, resposta);
}
/ **
* 压缩 压缩 列表 中 的 文件
* @param files
* @param
outputStream * @throws IOException
* /
public static void zipFile (Listar arquivos, ZipOutputStream outputStream) aciona IOException, ServletException
{ try { int size = files.size ( ); // 压缩 列表 中 的 文件
para (int i = 0; i <tamanho; i ++)
{ Arquivo arquivo = (Arquivo) arquivos.get (i); zipFile (arquivo, outputStream); } } catch (IOException e) { throw e; } } / ** * 将 文件 写入 到 zip 文件 中 * @param inputFile * @param outputstream * @throws Exception * / public static void zipFile (File inputFile, ZipOutputStream outputstream) lança 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 (entrada); final int MAX_BYTE = 10 * 1024 * 1024; / O maior fluxo tem 10 M de comprimento streamTotal = 0; // A capacidade do fluxo recebido int streamNum = 0; // O número de fluxos que precisam ser separados int leaveByte = 0; // O número de caracteres restantes no arquivo byte [] inOutbyte; // byte Array aceita dados de arquivo
streamTotal = bInStream.available (); // Obtenha o número máximo de caracteres no fluxo por meio do método disponível
streamNum = (int) Math.floor (streamTotal / MAX_BYTE); // Obtenha o número de arquivos de fluxo que precisam ser separados
leaveByte = (int) streamTotal% MAX_BYTE ; // Depois de dividir o arquivo, o número restante
if (streamNum> 0)
{ for (int j = 0; j <streamNum; ++ j) { inOutbyte = new byte [MAX_BYTE]; // Ler o stream e salvá-lo em byte Array bInStream.read (inOutbyte, 0, MAX_BYTE); outputstream.write (inOutbyte, 0, MAX_BYTE); // escrever o fluxo }
}
// 写出 剩下 的 流 数据
inOutbyte = new byte [leaveByte];
bInStream.read (inOutbyte, 0, leaveByte);
outputstream.write (inOutbyte);
outputstream.closeEntry (); // Fecha a entrada ZIP atual e posiciona o fluxo para escrever a próxima entrada
bInStream.close (); //
关闭 inStream.close ();
}
}
else
{ lançar novo ServletException ("文件 不 存在!"); } } catch (IOException e) { throw e; } }
/ **
* 下载 打包 的 的 文件
* @param file
* @param response
* /
public void downloadZip (File file, HttpServletResponse response) { try { // 以 流 的 形式 下载 文件。 BufferedInputStream fis = new BufferedInputStream (novo FileInputStream (arquivo .getPath ())); byte [] buffer = novo byte [fis.available ()]; fis.read (buffer); fis.close (); // 清空 resposta response.reset (); OutputStream toClient = new BufferedOutputStream (response.getOutputStream ()); response.setContentType ("application / octet-stream");
response.setHeader ("Content-Disposition", "attachment; filename =" + file.getName ());
toClient.write (buffer);
toClient.flush ();
toClient.close ();
file.delete (); / / Exclua o arquivo gerado do lado do servidor
}
catch (IOException ex) { ex.printStackTrace (); } }
Download do pacote de vários arquivos em segundo plano JAVA
Acho que você gosta
Origin blog.csdn.net/FV8023/article/details/100132233
Recomendado
Clasificación