springboot单,多文件打包下载到客户端--工具类
2019-09-25 07:07
1381 查看
import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; /** *文件下载导出 */ public class ZipFileExport { /** * 文件导出下载到----客户端 * @param response * @param filename * @param path */ public void downImgClient(HttpServletResponse response, String filename, String path ){ if (filename != null) { FileInputStream inputStream = null; BufferedInputStream bs = null; ServletOutputStream servletOutputStream = null; try { response.setHeader("Content-Type","application/octet-stream"); response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate"); response.addHeader("charset", "utf-8"); response.addHeader("Pragma", "no-cache"); String encodeName = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString()); response.setHeader("Content-Disposition", "attachment; filename=\"" + encodeName + "\"; filename*=utf-8''" + encodeName); File file = new File(path); inputStream = new FileInputStream(file); bs =new BufferedInputStream(inputStream); servletOutputStream = response.getOutputStream(); writeBytes(bs, servletOutputStream); } catch (Exception e) { e.printStackTrace(); } finally { try { if (servletOutputStream != null) { servletOutputStream.close(); //servletOutputStream = null; } if (bs!=null){ bs.close(); } if (inputStream != null) { inputStream.close(); //inputStream = null; } } catch (Exception e) { e.printStackTrace(); } } } } //writeBytes()构造方法 private void writeBytes(InputStream in, OutputStream out) throws IOException { byte[] buffer= new byte[1024]; int length = -1; while ((length = in.read(buffer))!=-1){ out.write(buffer,0,length); } in.close(); out.close(); } /** * 单文件导出下载 * @param response * @param filename * @param path */ public void downImg(HttpServletResponse response, String filename, String path ){ if (filename != null) { FileInputStream is = null; BufferedInputStream bs = null; OutputStream os = null; try { File file = new File(path); if (file.exists()) { is = new FileInputStream(file); bs =new BufferedInputStream(is); os = response.getOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while((len = bs.read(buffer)) != -1){ os.write(buffer,0,len); } }else{ String error = "下载的文件资源不存在"; System.out.println(error); } }catch(IOException ex){ ex.printStackTrace(); }finally { try{ if(is != null){ is.close(); } if( bs != null ){ bs.close(); } if( os != null){ os.flush(); os.close(); } }catch (IOException e){ e.printStackTrace(); } } downImgClient(response,filename,path); } } /** * 多文件打包下载 * @param response * @param names 文件名集合 * @param paths 文件路径集合 * @param directoryPath //临时存放--服务器上--zip文件的目录 */ public void FileDownload(HttpServletResponse response, List<String> names, List<String> paths,String directoryPath,String zipFileNameEn) { File directoryFile=new File(directoryPath); if(!directoryFile.isDirectory() && !directoryFile.exists()){ directoryFile.mkdirs(); } //设置最终输出zip文件的目录+文件名 SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss"); String zipFileName =zipFileNameEn+ formatter.format(new Date())+".zip"; String strZipPath = directoryPath+"/"+zipFileName; ZipOutputStream zipStream = null; FileInputStream zipSource = null; BufferedInputStream bufferStream = null; File zipFile = new File(strZipPath); try{ //构造最终压缩包的输出流 zipStream = new ZipOutputStream(new FileOutputStream(zipFile)); for (int i = 0; i<paths.size() ;i++){ //解码获取真实路径与文件名 String realFileName = java.net.URLDecoder.decode(names.get(i),"UTF-8"); String realFilePath = java.net.URLDecoder.decode(paths.get(i),"UTF-8"); File file = new File(realFilePath) if(file.exists()) { zipSource = new FileInputStream(file);//将需要压缩的文件格式化为输入流 /** * 压缩条目不是具体独立的文件,而是压缩包文件列表中的列表项,称为条目,就像索引一样这里的name就是文件名, * 文件名和之前的重复就会导致文件被覆盖 */ ZipEntry zipEntry = new ZipEntry(realFileName);//在压缩目录中文件的名字 zipStream.putNextEntry(zipEntry);//定位该压缩条目位置,开始写入文件到压缩包中 bufferStream = new BufferedInputStream(zipSource, 1024 * 10); int read = 0; byte[] buf = new byte[1024 * 10]; while((read = bufferStream.read(buf, 0, 1024 * 10)) != -1) { zipStream.write(buf, 0, read); } } } } catch (Exception e) { e.printStackTrace(); } finally { //关闭流 try { if(null != bufferStream) { bufferStream.close(); } if(null != zipStream){ zipStream.flush(); zipStream.close(); } if(null != zipSource){ zipSource.close(); } } catch (IOException e) { e.printStackTrace(); } } //判断当前压缩文件是否生成存在:true-把该压缩文件通过流输出给客户端后删除该压缩文件 if(zipFile.exists()){ //发送给客户端 downImgClient(response,zipFileName,strZipPath); //删除本地存储的文件 zipFile.delete(); } } }
相关文章推荐
- Java多文件下载打包zip返回客户端,不写入本地
- springboot单文件下载和多文件打包下载
- [Java Web]服务器获取远程文件并Zip打包给客户端下载
- 【转】文件下载之断点续传(客户端与服务端的实现)
- python--》客户端与服务端文件的下载
- .Net/C#: 实现支持断点续传多线程下载的 Http Web 客户端工具类 (第2版) (C# DIY HttpWebClient)
- 快速浏览silverlight3 Beta: 打包下载文件(共享程序集)
- 万恶的中文乱码问题ajax提交、单个文件下载(文件名)和打包下载压缩包中文件文件名乱码...
- 【Java-Android开发系列】Android SDK for Windows全部文件打包下载【115已死,文件无法下载】
- springmvc批量文件打包成zip下载功能
- Eclipse新建Maven项目并运行(打包)java程序实现hdfs与本地之间的上传或下载文件
- ASP.NET多文件批量打包下载
- web 项目中资源下载出错,或者下载的文件对应的形式不对应,web 项目下载的文件自动打包为一个压缩包
- 打包成apk,生成apk文件,上传到网站服务器提供链接下载
- .Net/C#: 实现支持断点续传多线程下载的 Http Web 客户端工具类
- 下载文件打包 zip java
- 客户端下载或保存文件
- Java实现文件上传下载工具类
- JSP 多个文件打包下载代码
- C#网站开发----在服务器上生成临时文件,下载到客户端,然后删除临时文件