近期,項目要求把服務器存儲的上傳文件,批量下載到本地.參考網上資料,實現了服務器文件打包成壓縮文件而後down到本地功能.
具體代碼實現:
一、在服務器端建立一個臨時zip格式文件
二、用jdk自帶的API將服務器全部文件輸入到zip文件中
三、將zip文件下載到本地,並刪除服務器端zip文件java
@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"; // 在服務器端建立打包下載的臨時文件 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; }
將服務器文件存到壓縮包中spring
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; // 流須要分開的數量 int leaveByte = 0; // 文件剩下的字符數 byte[] inOutbyte; // byte數組接受文件的數據 streamTotal = bInStream.available(); // 經過available方法取得流的最大字符數 streamNum = (int) Math.floor(streamTotal / MAX_BYTE); // 取得流文件須要分開的數量 leaveByte = (int) streamTotal % MAX_BYTE; // 分開文件以後,剩餘的數量 if (streamNum > 0) { for (int j = 0; j < streamNum; ++j) { inOutbyte = new byte[MAX_BYTE]; // 讀入流,保存在byte數組 bInStream.read(inOutbyte, 0, MAX_BYTE); outputstream.write(inOutbyte, 0, MAX_BYTE); // 寫出流 } } // 寫出剩下的流數據 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(); // 關閉 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(); //是否將生成的服務器端文件刪除 } } catch (IOException ex) { ex.printStackTrace(); } }
單個文件的下載服務器
@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; }