• 多文件打包下载以及单文件下载


    今天项目中需要多文件打包下载和单文件下载的功能,以下做一些总结。

    原代码:

      1 package com.hlbj.utils;
      2 
      3 import java.io.File; 
      4 import java.io.FileInputStream;
      5 import java.io.FileOutputStream;
      6 import java.io.InputStream;
      7 import java.io.OutputStream;
      8 import java.util.ArrayList;
      9 import java.util.List;
     10 import java.util.zip.ZipEntry;
     11 import java.util.zip.ZipOutputStream;
     12 import javax.servlet.http.HttpServletResponse;
     13 import org.apache.commons.lang3.StringUtils;
     14 
     15 import com.app.core.common.Config;
     16 import com.hlbj.pdf.component.GeneratePdf;
     17 
     18 public class DownLoadUitls {
     19         //图片地址
     20     private static String physicalPath = "";
    //多文件压缩下载 21 public static boolean downLoadPic(String filePathstr, String fileNamestr, HttpServletResponse response) { 22 boolean isgood = false; 23 24 if (StringUtils.isBlank(filePathstr) || StringUtils.isBlank(fileNamestr)) { 25 return isgood; 26 } 27 28 String[] filePaths = filePathstr.split(","); 29 String[] fileNames = fileNamestr.split(","); 30 31 if (filePaths == null || filePaths.length == 0 || fileNames == null || fileNames.length == 0) { 32 return isgood; 33 } 34 35 String classpath = ""; 36 if (StringUtils.isNoneBlank(physicalPath)) { 37 classpath = physicalPath; 38 } else { 39 classpath = GeneratePdf.class.getClassLoader().getResource("").getPath(); 40 classpath = classpath.substring(0, classpath.lastIndexOf("hlbj_api") - 1); 41 } 42 43 List<FileInputStream> inputs = new ArrayList<FileInputStream>(); 44 try { 45 for (String path : filePaths) { 46 String str = classpath + path.substring(path.indexOf("/s4m3files")); 47 File file = new File(str); 48 if (file.exists()) { 49 FileInputStream input = new FileInputStream(file); 50 inputs.add(input); 51 } 52 } 53 54 // ZIP打包图片 55 String downPath = classpath + "/images.zip"; 56 File zipFile = new File(downPath); 57 byte[] buf = new byte[1024]; 58 int len; 59 ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(zipFile)); 60 for (int i = 0; i < inputs.size(); i++) { 61 FileInputStream in = inputs.get(i); 62 zout.putNextEntry(new ZipEntry(fileNames[i])); 63 while ((len = in.read(buf)) > 0) { 64 zout.write(buf, 0, len); 65 } 66 zout.closeEntry(); 67 in.close(); 68 } 69 zout.close(); 70 71 // 下载图片 72 FileInputStream zipInput = new FileInputStream(zipFile); 73 OutputStream out = response.getOutputStream(); 74 response.setContentType("application/octet-stream"); 75 response.setHeader("Content-Disposition", "attachment; filename=images.zip"); 76 while ((len = zipInput.read(buf)) != -1) { 77 out.write(buf, 0, len); 78 } 79 zipInput.close(); 80 out.flush(); 81 out.close(); 82 // 删除压缩包 83 zipFile.delete(); 84 isgood = true; 85 } catch (Exception e) { 86 // TODO Auto-generated catch block 87 e.printStackTrace(); 88 } 89 90 return isgood; 91 } 92 //单文件下载 93 public static void downLoadPdf(String filePathstr, HttpServletResponse response) { 94 String classpath = ""; 95 if (StringUtils.isNoneBlank(physicalPath)) { 96 classpath = physicalPath; 97 } else { 98 classpath = GeneratePdf.class.getClassLoader().getResource("").getPath(); 99 classpath = classpath.substring(0, classpath.lastIndexOf("hlbj_api") - 1); 100 } 101 filePathstr= classpath + filePathstr.substring(filePathstr.indexOf("/pdf")); 102 // 下载本地文件 103 // path是指欲下载的文件的路径。 104 File file = new File(filePathstr); 105 // 取得文件名。 106 String filename = file.getName(); 107 // 读到流中 108 InputStream inStream; 109 110 111 try { 112 inStream = new FileInputStream(filePathstr); 113 // 设置输出的格式 114 response.reset(); 115 response.setContentType("bin"); 116 response.addHeader("Content-Disposition", "attachment; filename="" + filename + """); 117 // 循环取出流中的数据 118 byte[] b = new byte[100]; 119 int len; 120 while ((len = inStream.read(b)) > 0){ 121 response.getOutputStream().write(b, 0, len); 122 } 123 inStream.close(); 124 } catch (Exception e1) { 125 // TODO Auto-generated catch block 126 e1.printStackTrace(); 127 } // 文件的存放路径 128 } 129 }
  • 相关阅读:
    HDU 1003 Max Sum
    HDU 1728 逃离迷宫
    UVA 10057 A midsummer night's dream.
    HDU 1232 畅通工程
    poj3331
    poj3481
    poj1053
    poj3281
    poj3199
    !!! Gridview的多种使用方法总结
  • 原文地址:https://www.cnblogs.com/huanlingjisi/p/9082607.html
Copyright © 2020-2023  润新知