关于springmvc下服务器文件打包成zip格式下载功能
2016-09-21 11:22
495 查看
近期,项目要求把服务器存储的上传文件,批量下载到本地.参考网上资料,实现了服务器文件打包成压缩文件然后down到本地功能.
具体代码实现:
1、在服务器端创建一个临时zip格式文件
2、用jdk自带的API将服务器所有文件输入到zip文件中
3、将zip文件下载到本地,并删除服务器端zip文件
将服务器文件存到压缩包中
下载文件
单个文件的下载
具体代码实现:
1、在服务器端创建一个临时zip格式文件
2、用jdk自带的API将服务器所有文件输入到zip文件中
3、将zip文件下载到本地,并删除服务器端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"; // 在服务器端创建打包下载的临时文件 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; }
将服务器文件存到压缩包中
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; }
相关文章推荐
- springmvc批量文件打包成zip下载功能
- springmvc批量文件打包成zip下载功能
- struts2下载文件功能(边下载边打包)
- 在C#中在服务器上打包压缩文件,并下载
- 【转载】Java如何实现文件打包下载功能
- MyBatis与SpringMVC相结合实现文件上传、下载功能
- springMVC 文件下载【文件已经自服务器上准备,可以按照此下载文件】
- 给goahead-3.1.0-2 web服务器 增加 任意盘符文件下载功能
- 乱码问题, (文件下载功能时, 下载不下来,编码要和服务器系统编码一致
- SpringMVC下打包文件的下载
- 关于IIS服务器下载新文件类型提示找不到文件的问题
- SpringMVC下载服务器上的文件到本地
- 关于Java的socket服务端项目如何打包成exe文件运行在服务器上。
- springMVC实现Excel文件下载功能
- JAVA实现FTP服务器文件的上传,下载,删除功能
- windows基于阿帕奇服务器,实现vc++文件下载功能
- 关于php支持分块与断点续传文件下载功能代码
- Java如何实现文件打包下载功能
- 关于使用LinkButton实现点击超链完成文件下载的功能
- 解决springmvc中文件下载功能中使用javax.servlet.ServletOutputStream out = response.getOutputStream();后运行出异常但结果正确的问题