提交 | 用户 | age
|
c1a9ee
|
1 |
package com.hx.util; |
F |
2 |
|
|
3 |
import org.apache.commons.fileupload.FileItem; |
|
4 |
import org.apache.commons.fileupload.disk.DiskFileItemFactory; |
|
5 |
import org.apache.commons.io.IOUtils; |
|
6 |
import org.springframework.http.MediaType; |
|
7 |
import org.springframework.web.multipart.MultipartFile; |
|
8 |
import org.springframework.web.multipart.commons.CommonsMultipartFile; |
248c35
|
9 |
import sun.misc.BASE64Decoder; |
c1a9ee
|
10 |
|
F |
11 |
import java.io.*; |
|
12 |
import java.net.URL; |
|
13 |
import java.net.URLConnection; |
|
14 |
import java.util.Base64; |
|
15 |
|
|
16 |
/** |
|
17 |
* 文件转换工具类 |
|
18 |
* @USER: fhx |
|
19 |
* @DATE: 2022/3/2 |
|
20 |
**/ |
|
21 |
public class FileConvertTool { |
|
22 |
|
|
23 |
private static final int BUFFER_SIZE = 2 * 1024; |
|
24 |
|
|
25 |
|
|
26 |
/** 获取网络路径文件流 */ |
|
27 |
public static InputStream getUrlFile(String urlPath) throws IOException { |
|
28 |
if(StringUtils.isEmpty(urlPath)){ |
|
29 |
return null; |
|
30 |
} |
|
31 |
// 构造URL |
|
32 |
URL url = new URL(urlPath); |
|
33 |
// 打开连接 |
|
34 |
URLConnection con = url.openConnection(); |
|
35 |
//设置请求超时为5s |
|
36 |
con.setConnectTimeout(5*1000); |
|
37 |
// 输入流 |
|
38 |
InputStream is = con.getInputStream(); |
|
39 |
return is; |
|
40 |
} |
|
41 |
|
|
42 |
/** |
|
43 |
* 获取文件base64字符串 |
|
44 |
* @param urlPath 文件路径 |
|
45 |
* @return |
|
46 |
* @throws IOException |
|
47 |
*/ |
|
48 |
public static String getFileBaseStrByUrl(String urlPath) throws IOException { |
|
49 |
InputStream is = getUrlFile(urlPath); |
|
50 |
byte[] bytes = IOUtils.toByteArray(is); |
|
51 |
String encoded = Base64.getEncoder().encodeToString(bytes); |
|
52 |
return encoded; |
|
53 |
} |
|
54 |
|
|
55 |
// inputStream转outputStream |
|
56 |
public static ByteArrayOutputStream parse(final InputStream in) throws Exception { |
|
57 |
final ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); |
|
58 |
int ch; |
|
59 |
while ((ch = in.read()) != -1) { |
|
60 |
swapStream.write(ch); |
|
61 |
} |
|
62 |
return swapStream; |
|
63 |
} |
|
64 |
|
|
65 |
// outputStream转inputStream |
|
66 |
public static ByteArrayInputStream parse(final OutputStream out) throws Exception { |
|
67 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
68 |
baos = (ByteArrayOutputStream) out; |
|
69 |
final ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray()); |
|
70 |
return swapStream; |
|
71 |
} |
|
72 |
|
|
73 |
// inputStream转String |
|
74 |
public static String parse_String(final InputStream in) throws Exception { |
|
75 |
final ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); |
|
76 |
int ch; |
|
77 |
while ((ch = in.read()) != -1) { |
|
78 |
swapStream.write(ch); |
|
79 |
} |
|
80 |
return swapStream.toString(); |
|
81 |
} |
|
82 |
|
|
83 |
// OutputStream 转String |
|
84 |
public static String parse_String(final OutputStream out) throws Exception { |
|
85 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
86 |
baos = (ByteArrayOutputStream) out; |
|
87 |
final ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray()); |
|
88 |
return swapStream.toString(); |
|
89 |
} |
|
90 |
|
|
91 |
// String转inputStream |
|
92 |
public static ByteArrayInputStream parse_inputStream(final String in) throws Exception { |
|
93 |
final ByteArrayInputStream input = new ByteArrayInputStream(in.getBytes()); |
|
94 |
return input; |
|
95 |
} |
|
96 |
|
|
97 |
// String 转outputStream |
|
98 |
public static ByteArrayOutputStream parse_outputStream(final String in) throws Exception { |
|
99 |
return parse(parse_inputStream(in)); |
|
100 |
} |
|
101 |
|
|
102 |
/** |
|
103 |
* 根据byte数组,生成文件 |
|
104 |
*/ |
|
105 |
public static File getFile(byte[] bfile, String filePath,String fileName) { |
|
106 |
BufferedOutputStream bos = null; |
|
107 |
FileOutputStream fos = null; |
|
108 |
File file = null; |
|
109 |
try { |
|
110 |
File dir = new File(filePath); |
|
111 |
if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在 |
|
112 |
dir.mkdirs(); |
|
113 |
} |
|
114 |
file = new File(filePath+"\\"+fileName); |
|
115 |
fos = new FileOutputStream(file); |
|
116 |
bos = new BufferedOutputStream(fos); |
|
117 |
bos.write(bfile); |
|
118 |
} catch (Exception e) { |
|
119 |
e.printStackTrace(); |
|
120 |
} finally { |
|
121 |
if (bos != null) { |
|
122 |
try { |
|
123 |
bos.close(); |
|
124 |
} catch (IOException e1) { |
|
125 |
e1.printStackTrace(); |
|
126 |
} |
|
127 |
} |
|
128 |
if (fos != null) { |
|
129 |
try { |
|
130 |
fos.close(); |
|
131 |
} catch (IOException e1) { |
|
132 |
e1.printStackTrace(); |
|
133 |
} |
|
134 |
} |
|
135 |
} |
|
136 |
return file; |
|
137 |
} |
|
138 |
|
|
139 |
/** |
|
140 |
* 根据byte数组,生成文件 |
|
141 |
*/ |
|
142 |
public static File getFile(InputStream in, String filePath, String fileName) { |
|
143 |
if(in == null){ |
|
144 |
return null; |
|
145 |
} |
|
146 |
BufferedOutputStream bos = null; |
|
147 |
FileOutputStream fos = null; |
|
148 |
File file = null; |
|
149 |
try { |
|
150 |
File dir = new File(filePath); |
|
151 |
//判断文件目录是否存在 |
|
152 |
if(!dir.exists()){ |
|
153 |
dir.mkdirs(); |
|
154 |
} |
|
155 |
file = new File(filePath+"/"+fileName); |
|
156 |
fos = new FileOutputStream(file); |
|
157 |
byte[] b = new byte[BUFFER_SIZE]; |
|
158 |
// while ((in.read(b)) != -1) { |
|
159 |
// fos.write(b); // 写入数据 |
|
160 |
// } |
|
161 |
int len; |
|
162 |
while ((len = in.read(b)) != -1){ |
|
163 |
fos.write(b, 0, len); |
|
164 |
} |
|
165 |
in.close(); |
|
166 |
} catch (Exception e) { |
|
167 |
e.printStackTrace(); |
|
168 |
} finally { |
|
169 |
if (bos != null) { |
|
170 |
try { |
|
171 |
bos.close(); |
|
172 |
} catch (IOException e1) { |
|
173 |
e1.printStackTrace(); |
|
174 |
} |
|
175 |
} |
|
176 |
if (fos != null) { |
|
177 |
try { |
|
178 |
fos.close(); |
|
179 |
} catch (IOException e1) { |
|
180 |
e1.printStackTrace(); |
|
181 |
} |
|
182 |
} |
|
183 |
} |
|
184 |
return file; |
|
185 |
} |
|
186 |
|
|
187 |
/** |
|
188 |
* 获得指定文件的byte数组 |
|
189 |
*/ |
|
190 |
private static byte[] getBytes(String filePath){ |
|
191 |
byte[] buffer = null; |
|
192 |
try { |
|
193 |
File file = new File(filePath); |
|
194 |
FileInputStream fis = new FileInputStream(file); |
|
195 |
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000); |
|
196 |
byte[] b = new byte[1000]; |
|
197 |
int n; |
|
198 |
while ((n = fis.read(b)) != -1) { |
|
199 |
bos.write(b, 0, n); |
|
200 |
} |
|
201 |
fis.close(); |
|
202 |
bos.close(); |
|
203 |
buffer = bos.toByteArray(); |
|
204 |
} catch (FileNotFoundException e) { |
|
205 |
e.printStackTrace(); |
|
206 |
} catch (IOException e) { |
|
207 |
e.printStackTrace(); |
|
208 |
} |
|
209 |
return buffer; |
|
210 |
} |
|
211 |
|
|
212 |
public static MultipartFile getMultipartFile(File file) { |
|
213 |
FileItem item = new DiskFileItemFactory().createItem("file" |
|
214 |
, MediaType.MULTIPART_FORM_DATA_VALUE |
|
215 |
, true |
|
216 |
, file.getName()); |
|
217 |
try (InputStream input = new FileInputStream(file); |
|
218 |
OutputStream os = item.getOutputStream()) { |
|
219 |
// 流转移 |
|
220 |
IOUtils.copy(input, os); |
|
221 |
} catch (Exception e) { |
|
222 |
throw new IllegalArgumentException("Invalid file: " + e, e); |
|
223 |
} |
|
224 |
|
|
225 |
return new CommonsMultipartFile(item); |
|
226 |
} |
|
227 |
|
|
228 |
/** |
|
229 |
* 根据路径删除指定的目录或文件,无论存在与否 |
|
230 |
*@param sPath 要删除的目录或文件 |
|
231 |
*@return 删除成功返回 true,否则返回 false。 |
|
232 |
*/ |
|
233 |
public static boolean deleteFolder(String sPath) { |
|
234 |
File file = new File(sPath); |
|
235 |
// 判断目录或文件是否存在 |
|
236 |
if (!file.exists()) { |
|
237 |
// 不存在返回 true |
|
238 |
return true; |
|
239 |
} else { |
|
240 |
// 判断是否为文件 |
|
241 |
if (file.isFile()) { // 为文件时调用删除文件方法 |
|
242 |
return deleteFile(sPath); |
|
243 |
} else { // 为目录时调用删除目录方法 |
|
244 |
return deleteDirectory(sPath); |
|
245 |
} |
|
246 |
} |
|
247 |
} |
|
248 |
|
|
249 |
/** |
|
250 |
* 删除单个文件 |
|
251 |
* @param sPath 被删除文件的文件名 |
|
252 |
* @return 单个文件删除成功返回true,否则返回false |
|
253 |
*/ |
|
254 |
public static boolean deleteFile(String sPath) { |
|
255 |
File file = new File(sPath); |
|
256 |
// 路径为文件且不为空则进行删除 |
|
257 |
if (file.isFile() && file.exists()) { |
|
258 |
file.delete(); |
|
259 |
return true; |
|
260 |
} |
|
261 |
return false; |
|
262 |
} |
|
263 |
|
|
264 |
/** |
|
265 |
* 删除目录(文件夹)以及目录下的文件 |
|
266 |
* @param sPath 被删除目录的文件路径 |
|
267 |
* @return 目录删除成功返回true,否则返回false |
|
268 |
*/ |
|
269 |
public static boolean deleteDirectory(String sPath) { |
|
270 |
//如果sPath不以文件分隔符结尾,自动添加文件分隔符 |
|
271 |
if (!sPath.endsWith(File.separator)) { |
|
272 |
sPath = sPath + File.separator; |
|
273 |
} |
|
274 |
File dirFile = new File(sPath); |
|
275 |
//如果dir对应的文件不存在,或者不是一个目录,则退出 |
|
276 |
if (!dirFile.exists() || !dirFile.isDirectory()) { |
|
277 |
return false; |
|
278 |
} |
|
279 |
boolean flag = true; |
|
280 |
//删除文件夹下的所有文件(包括子目录) |
|
281 |
File[] files = dirFile.listFiles(); |
|
282 |
for (int i = 0; i < files.length; i++) { |
|
283 |
//删除子文件 |
|
284 |
if (files[i].isFile()) { |
|
285 |
flag = deleteFile(files[i].getAbsolutePath()); |
|
286 |
if (!flag) break; |
|
287 |
} //删除子目录 |
|
288 |
else { |
|
289 |
flag = deleteDirectory(files[i].getAbsolutePath()); |
|
290 |
if (!flag) break; |
|
291 |
} |
|
292 |
} |
|
293 |
if (!flag) return false; |
|
294 |
//删除当前目录 |
|
295 |
if (dirFile.delete()) { |
|
296 |
return true; |
|
297 |
} else { |
|
298 |
return false; |
|
299 |
} |
|
300 |
} |
248c35
|
301 |
|
F |
302 |
|
c1a9ee
|
303 |
} |