柒索

一个头脑聪明,五肢发达的男人。

0%

Java文件操作(打包压缩、下载、转换等)

代码示例

具体代码如下:

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 {

/**
* 创建临时文件
* @param inputStream 输入流
* @param name 文件名称
* @param ext 文件后缀
* @param tmpDirFile 文件
* @return
* @throws IOException
*/
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;
}

/**
* Byte转成File
* @param bytes 文件数据
* @param fileType 文件类型
* @param fileName 文件名称
* @return
* @throws IOException
*/
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;
/**
* 文件打包下载
* @param files
* @param fileName 文件压缩包的名字
* @param response
* @return
* @throws Exception
*/
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这样一个输出流,所以这里我们把输出流转换一下
//org.apache.tools.zip.ZipOutputStream
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);
}
}

/**
* 浏览器下载压缩文件
* @param file Zip压缩文件
* @param response
* @return
*/
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
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;
}

/**
* 根据输入的文件与输出流对文件进行打包
*
* @param outputStream Zip输出流
* @param inputFile 文件输入流
*/
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);
//org.apache.tools.zip.ZipEntry
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();
}
}
}