1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| import org.apache.commons.io.FileUtils;
import javax.servlet.http.HttpServletResponse; import java.io.*; import java.nio.file.Files; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream;
public class FileUtil {
public static File createTmpFile(InputStream inputStream, String name, String ext, File tmpDirFile) throws IOException { File resultFile = new File(tmpDirFile.getPath()+File.separator+name+"."+ext); resultFile.deleteOnExit(); FileUtils.copyToFile(inputStream, resultFile); return resultFile; }
public static File bytesToFile(byte[] bytes, String fileType,String fileName) throws IOException { return createTmpFile(new ByteArrayInputStream(bytes), fileName, fileType, Files.createTempDirectory("tempFile").toFile()); }
public final static String ROOT_PATH = System.getProperty("user.dir") + File.separator;
public static HttpServletResponse downLoadFiles(List<File> files, String fileName, HttpServletResponse response) throws Exception { try { SimpleDateFormat dateformate = new SimpleDateFormat("yyyyMMddhhmmss"); String name = dateformate.format(new Date()) + ".zip"; File file = new File(ROOT_PATH + fileName.concat(name)); if (!file.exists()) { file.createNewFile(); } response.reset();
FileOutputStream fous = new FileOutputStream(file);
ZipOutputStream zipOut = new ZipOutputStream(fous); zipFile(files, zipOut); zipOut.close(); fous.close(); return downloadZip(file, response); } catch (Exception e) { e.printStackTrace(); } return response; }
public static void zipFile(List files, ZipOutputStream outputStream) { int size = files.size(); for (int i = 0; i < size; i++) { File file = (File) files.get(i); zipFile(file, outputStream); } }
public static HttpServletResponse downloadZip(File file, HttpServletResponse response) { try { InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath())); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); 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(); } catch (IOException ex) { ex.printStackTrace(); } finally { try { File f = new File(file.getPath()); f.delete(); } catch (Exception e) { e.printStackTrace(); } } return response; }
public static void zipFile(File inputFile, ZipOutputStream outputStream) { try { if (inputFile.exists()) { if (inputFile.isFile()) { FileInputStream IN = new FileInputStream(inputFile); BufferedInputStream bins = new BufferedInputStream(IN, 512); ZipEntry entry = new ZipEntry(inputFile.getName()); outputStream.putNextEntry(entry); int nNumber; byte[] buffer = new byte[1024]; while ((nNumber = bins.read(buffer)) != -1) { outputStream.write(buffer, 0, nNumber); } bins.close(); IN.close(); } else { try { File[] files = inputFile.listFiles(); for (int i = 0; i < files.length; i++) { zipFile(files[i], outputStream); } } catch (Exception e) { e.printStackTrace(); } } } } catch (Exception e) { e.printStackTrace(); } } }
|