日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

关于springmvc下服务器文件打包成zip格式下载功能

發(fā)布時(shí)間:2025/3/16 c/c++ 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 关于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文件

@RequestMapping(value = "/downloadZip.do")public String downloadFiles(String tcLwIds, HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { List<File> files = new ArrayList<File>(); File Allfile = new File(request.getSession().getServletContext().getRealPath("/") + "upload/"); if (Allfile.exists()) { File[] fileArr = Allfile.listFiles(); for (File file2 : fileArr) { files.add(file2); } } String fileName = UUID.randomUUID().toString() + ".zip"; // 在服務(wù)器端創(chuàng)建打包下載的臨時(shí)文件 String outFilePath = request.getSession().getServletContext().getRealPath("/") + "upload/"; File fileZip = new File(outFilePath + fileName); // 文件輸出流 FileOutputStream outStream = new FileOutputStream(fileZip); // 壓縮流 ZipOutputStream toClient = new ZipOutputStream(outStream); // toClient.setEncoding("gbk"); zipFile(files, toClient); toClient.close(); outStream.close(); this.downloadFile(fileZip, response, true); return null; }

?

將服務(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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。