关于springmvc下服务器文件打包成zip格式下载功能
生活随笔
收集整理的這篇文章主要介紹了
关于springmvc下服务器文件打包成zip格式下载功能
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
關(guān)于springmvc下服務(wù)器文件打包成zip格式下載功能
2016年09月21日 11:22:14 toxic_guantou 閱讀數(shù):5731更多 個(gè)人分類: 技術(shù)點(diǎn)存儲(chǔ) 版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。 https://blog.csdn.net/toxic_guantou/article/details/52605920近期,項(xiàng)目要求把服務(wù)器存儲(chǔ)的上傳文件,批量下載到本地.參考網(wǎng)上資料,實(shí)現(xiàn)了服務(wù)器文件打包成壓縮文件然后down到本地功能.
具體代碼實(shí)現(xiàn):
1、在服務(wù)器端創(chuàng)建一個(gè)臨時(shí)zip格式文件
2、用jdk自帶的API將服務(wù)器所有文件輸入到zip文件中
3、將zip文件下載到本地,并刪除服務(wù)器端zip文件
?
將服務(wù)器文件存到壓縮包中
public static void zipFile(List<File> files, ZipOutputStream outputStream) throws IOException, ServletException { try { int size = files.size(); // 壓縮列表中的文件 for (int i = 0; i < size; i++) { File file = (File) files.get(i); zipFile(file, outputStream); } } catch (IOException e) { throw e; } } public static void zipFile(File inputFile, ZipOutputStream outputstream) throws IOException, ServletException { try { if (inputFile.exists()) { if (inputFile.isFile()) { FileInputStream inStream = new FileInputStream(inputFile); BufferedInputStream bInStream = new BufferedInputStream(inStream); ZipEntry entry = new ZipEntry(inputFile.getName()); outputstream.putNextEntry(entry); final int MAX_BYTE = 10 * 1024 * 1024; // 最大的流為10M long streamTotal = 0; // 接受流的容量 int streamNum = 0; // 流需要分開的數(shù)量 int leaveByte = 0; // 文件剩下的字符數(shù) byte[] inOutbyte; // byte數(shù)組接受文件的數(shù)據(jù) streamTotal = bInStream.available(); // 通過available方法取得流的最大字符數(shù) streamNum = (int) Math.floor(streamTotal / MAX_BYTE); // 取得流文件需要分開的數(shù)量 leaveByte = (int) streamTotal % MAX_BYTE; // 分開文件之后,剩余的數(shù)量 if (streamNum > 0) { for (int j = 0; j < streamNum; ++j) { inOutbyte = new byte[MAX_BYTE]; // 讀入流,保存在byte數(shù)組 bInStream.read(inOutbyte, 0, MAX_BYTE); outputstream.write(inOutbyte, 0, MAX_BYTE); // 寫出流 } } // 寫出剩下的流數(shù)據(jù) inOutbyte = new byte[leaveByte]; bInStream.read(inOutbyte, 0, leaveByte); outputstream.write(inOutbyte); outputstream.closeEntry(); // Closes the current ZIP entry // and positions the stream for // writing the next entry bInStream.close(); // 關(guān)閉 inStream.close(); } } else { throw new ServletException("文件不存在!"); } } catch (IOException e) { throw e; } }?
下載文件
public void downloadFile(File file,HttpServletResponse response,boolean isDelete) {try { // 以流的形式下載文件。 BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file.getPath())); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); // 清空response response.reset(); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename=" + new String(file.getName().getBytes("UTF-8"),"ISO-8859-1")); toClient.write(buffer); toClient.flush(); toClient.close(); if(isDelete) { file.delete(); //是否將生成的服務(wù)器端文件刪除 } } catch (IOException ex) { ex.printStackTrace(); } }?
單個(gè)文件的下載
@RequestMapping(value="/singleDownload.do")public String singleDownload(String fileName, HttpServletRequest request, HttpServletResponse response)throws Exception{ response.setCharacterEncoding("utf-8"); response.setContentType("multipart/form-data"); response.setHeader("Content-Disposition", "attachment;fileName="+ fileName); String realPath = request.getSession().getServletContext().getRealPath("/"); String path = realPath+"upload/"; File file = new File(path+ File.separator + fileName); downloadFile(file, response, false); return null; }轉(zhuǎn)載于:https://www.cnblogs.com/libin6505/p/9970672.html
總結(jié)
以上是生活随笔為你收集整理的关于springmvc下服务器文件打包成zip格式下载功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C# 程序执行时间差
- 下一篇: s3c2440移植MQTT