提交 | 用户 | age
|
a6a76f
|
1 |
package com.yc.utils; |
F |
2 |
|
97a11f
|
3 |
import com.yc.exception.ApplicationException; |
F |
4 |
import org.springframework.web.multipart.MultipartHttpServletRequest; |
|
5 |
import org.springframework.web.multipart.commons.CommonsMultipartResolver; |
|
6 |
|
|
7 |
import javax.servlet.http.HttpServletRequest; |
|
8 |
import javax.servlet.http.HttpSession; |
125603
|
9 |
import java.io.*; |
a6a76f
|
10 |
import java.text.DecimalFormat; |
F |
11 |
import java.util.ArrayList; |
|
12 |
import java.util.regex.Pattern; |
|
13 |
|
|
14 |
public class FileUtil { |
|
15 |
/** |
|
16 |
* 生成具体功能号页面 |
|
17 |
* @param String str 文件内容 |
|
18 |
* @param String file 保存文件路径 |
|
19 |
* **/ |
|
20 |
public static void writeFile(String str,String root){ |
|
21 |
BufferedWriter writer = null; |
|
22 |
File fp = new File(root).getParentFile(); |
|
23 |
if (!fp.exists()) { |
|
24 |
fp.mkdirs();// 目录不存在的情况下,创建目录。 |
|
25 |
} |
|
26 |
try { |
|
27 |
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(root),"utf-8")); |
|
28 |
writer.write(str); |
|
29 |
writer.flush(); |
|
30 |
writer.close(); |
|
31 |
|
|
32 |
} catch (IOException e) { |
|
33 |
e.printStackTrace(); |
|
34 |
throw new ApplicationException(e.getMessage()); |
|
35 |
} finally { |
|
36 |
if (writer != null){ |
|
37 |
try { |
|
38 |
writer.close(); |
|
39 |
} catch (IOException e1) { |
|
40 |
} |
|
41 |
} |
|
42 |
} |
|
43 |
} |
|
44 |
|
|
45 |
public static void writeFileAppend(String str,String root){ |
|
46 |
BufferedWriter writer = null; |
|
47 |
File fp = new File(root).getParentFile(); |
|
48 |
if (!fp.exists()) { |
|
49 |
fp.mkdirs();// 目录不存在的情况下,创建目录。 |
|
50 |
} |
|
51 |
try { |
|
52 |
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(root),"utf-8")); |
|
53 |
writer.append(str); |
|
54 |
writer.flush(); |
|
55 |
writer.close(); |
|
56 |
|
|
57 |
} catch (IOException e) { |
|
58 |
e.printStackTrace(); |
|
59 |
throw new ApplicationException(e.getMessage()); |
|
60 |
} finally { |
|
61 |
if (writer != null){ |
|
62 |
try { |
|
63 |
writer.close(); |
|
64 |
} catch (IOException e1) { |
|
65 |
} |
|
66 |
} |
|
67 |
} |
|
68 |
} |
|
69 |
/** |
|
70 |
* 读取gt-grid的模板文件 |
|
71 |
* @param String path 模板文件路径 |
|
72 |
* **/ |
|
73 |
public static String readFile(String path){ |
|
74 |
BufferedReader reader = null; |
|
75 |
StringBuilder sb=new StringBuilder(); |
|
76 |
try { |
|
77 |
reader = new BufferedReader(new InputStreamReader(new FileInputStream(path),"utf-8")); |
|
78 |
String tempString = null; |
|
79 |
//一次读入一行,直到读入null为文件结束 |
|
80 |
while ((tempString = reader.readLine()) != null){ |
|
81 |
sb.append(tempString).append("\n"); |
|
82 |
} |
|
83 |
reader.close(); |
97a11f
|
84 |
} catch (IOException e) { |
F |
85 |
e.printStackTrace(); |
|
86 |
throw new ApplicationException(e.getMessage()); |
a6a76f
|
87 |
} finally { |
F |
88 |
if (reader != null){ |
|
89 |
try { |
|
90 |
reader.close(); |
|
91 |
} catch (IOException e1) { |
|
92 |
} |
|
93 |
} |
|
94 |
} |
|
95 |
return sb.toString(); |
|
96 |
} |
|
97 |
|
|
98 |
/** |
|
99 |
* 获得文件(文件夹)大小 |
|
100 |
* @param f |
|
101 |
* @return |
|
102 |
*/ |
|
103 |
public static long getFileSize(File f){ |
|
104 |
long size = 0; |
|
105 |
if(f.exists()){ |
|
106 |
if(f.isDirectory()){ |
|
107 |
File [] files = f.listFiles(); |
|
108 |
for(int i=0;i<files.length;i++){ |
|
109 |
size += getFileSize(files[i]); |
|
110 |
} |
|
111 |
}else{ |
|
112 |
size = f.length(); |
|
113 |
} |
|
114 |
} |
|
115 |
return size; |
|
116 |
} |
|
117 |
/** |
|
118 |
* 替换字符串的会话值 |
|
119 |
* |
|
120 |
* */ |
|
121 |
public static String prossSessionInfo(HttpServletRequest request,String str) { |
|
122 |
HttpSession session=request.getSession(); |
|
123 |
if(str==null||"".equalsIgnoreCase(str)) return str; |
|
124 |
|
|
125 |
Pattern p = Pattern.compile("@.*?\\w+");// 匹配以@开头的单词 |
|
126 |
str=str.replaceAll("@~","%"); |
|
127 |
java.util.regex.Matcher propsMatcher = p.matcher(str); |
|
128 |
while (propsMatcher.find()) { |
|
129 |
try { |
|
130 |
String tem=(String)session.getAttribute(propsMatcher.group().toLowerCase()); |
|
131 |
str=str.replaceAll(propsMatcher.group(), tem); |
|
132 |
}catch(Exception e) { |
|
133 |
throw new ApplicationException(propsMatcher.group()+"不存在于会话中,请检查设置"); |
|
134 |
} |
|
135 |
|
|
136 |
} |
|
137 |
return str; |
|
138 |
} |
|
139 |
/** |
|
140 |
* 格式文件大小 |
|
141 |
* @param fileS |
|
142 |
* @return |
|
143 |
*/ |
|
144 |
public static String formetFileSize(long fileS) {//转换文件大小 |
|
145 |
DecimalFormat df = new DecimalFormat("#.00"); |
|
146 |
String fileSizeString = null; |
|
147 |
if (fileS < 1024) { |
|
148 |
fileSizeString = df.format((double) fileS) + "B"; |
|
149 |
} else if (fileS < 1048576) { |
|
150 |
fileSizeString = df.format((double) fileS / 1024) + "KB"; |
|
151 |
} else if (fileS < 1073741824) { |
|
152 |
fileSizeString = df.format((double) fileS / 1048576) + "MB"; |
|
153 |
} else { |
|
154 |
fileSizeString = df.format((double) fileS / 1073741824) + "GB"; |
|
155 |
} |
|
156 |
return fileSizeString; |
|
157 |
} |
|
158 |
/** |
|
159 |
* 获得文件的最后修改时间,作为文件的版本号,解决缓存问题 |
|
160 |
* |
|
161 |
* **/ |
|
162 |
public static long getVerstion(HttpServletRequest request,String path){ |
|
163 |
String s=request.getServletContext().getRealPath("/")+"/"; |
|
164 |
java.io.File file=new java.io.File(s+path); |
|
165 |
long v=file.lastModified(); |
|
166 |
file=null; |
|
167 |
return v; |
|
168 |
} |
|
169 |
//14.复制一个文件夹下所有的文件夹到另一个文件夹下 |
|
170 |
public static void copyFiles(String str1,String str2){ |
|
171 |
File copyfolders=new File(str1); |
|
172 |
File[] copyfoldersList=copyfolders.listFiles(); |
|
173 |
for(int k=0;k<copyfoldersList.length;k++){ |
|
174 |
if(copyfoldersList[k].isDirectory()){ |
|
175 |
ArrayList<String>folderList=new ArrayList<String>(); |
|
176 |
folderList.add(copyfoldersList[k].getPath()); |
|
177 |
ArrayList<String>folderList2=new ArrayList<String>(); |
|
178 |
folderList2.add(str2+"/"+copyfoldersList[k].getName()); |
|
179 |
for(int j=0;j<folderList.size();j++){ |
|
180 |
(new File(folderList2.get(j))).mkdirs(); //如果文件夹不存在 则建立新文件夹 |
|
181 |
File folders=new File(folderList.get(j)); |
|
182 |
String[] file=folders.list(); |
|
183 |
File temp=null; |
|
184 |
try { |
|
185 |
for (int i = 0; i < file.length; i++) { |
|
186 |
if(folderList.get(j).endsWith(File.separator)){ |
|
187 |
temp=new File(folderList.get(j)+"/"+file[i]); |
|
188 |
} else { |
|
189 |
temp=new File(folderList.get(j)+"/"+File.separator+file[i]); |
|
190 |
} |
|
191 |
FileInputStream input = new FileInputStream(temp); |
|
192 |
if(temp.isFile()){ |
|
193 |
FileInputStream input1 = new FileInputStream(temp); |
|
194 |
FileOutputStream output = new FileOutputStream(folderList2.get(j) + "/" + (temp.getName()).toString()); |
|
195 |
byte[] b = new byte[5120]; |
|
196 |
int len; |
|
197 |
while ( (len = input1.read(b)) != -1) { |
|
198 |
output.write(b, 0, len); |
|
199 |
} |
|
200 |
output.flush(); |
|
201 |
output.close(); |
|
202 |
input1.close(); |
|
203 |
} |
|
204 |
if(temp.isDirectory()){//如果是子文件夹 |
|
205 |
folderList.add(folderList.get(j)+"/"+file[i]); |
|
206 |
folderList2.add(folderList2.get(j)+"/"+file[i]); |
|
207 |
} |
|
208 |
} |
|
209 |
} |
|
210 |
catch (Exception e) { |
|
211 |
System.out.println("复制整个文件夹内容操作出错"); |
|
212 |
e.printStackTrace(); |
|
213 |
} |
|
214 |
} |
|
215 |
} |
|
216 |
} |
|
217 |
} |
|
218 |
|
|
219 |
/** |
|
220 |
* 通过spring内置的上传处理功能,取得MultipartHttpServletRequest,可以取得上传文件,附带的参数 |
|
221 |
* 用法: |
|
222 |
* MultipartFile file = multipartRequest.getFile("file"); |
|
223 |
* String dbid=multipartRequest.getParameter("dbid"); |
|
224 |
* @param request |
|
225 |
* @return |
|
226 |
*/ |
|
227 |
public static MultipartHttpServletRequest getMultipartHttpServletRequest(HttpServletRequest request) { |
|
228 |
//创建一个通用的多部分解析器. |
|
229 |
CommonsMultipartResolver commonsMultipartResolver = new |
|
230 |
CommonsMultipartResolver(request.getSession().getServletContext()); |
|
231 |
//设置编码 |
|
232 |
commonsMultipartResolver.setDefaultEncoding("utf-8"); |
|
233 |
//判断 request 是否有文件上传,即多部分请求... |
|
234 |
if (commonsMultipartResolver.isMultipart(request)) { |
|
235 |
//转换成多部分request |
|
236 |
return commonsMultipartResolver.resolveMultipart(request); |
|
237 |
} |
|
238 |
return null; |
|
239 |
} |
125603
|
240 |
/** |
F |
241 |
* |
|
242 |
* @param path |
|
243 |
* @return String |
|
244 |
* @description 将文件转base64字符串 |
|
245 |
* @date 2018年3月20日 |
|
246 |
* @author changyl |
|
247 |
* File转成编码成BASE64 |
|
248 |
*/ |
|
249 |
|
|
250 |
public static String fileToBase64(String path) { |
|
251 |
String base64 = null; |
|
252 |
InputStream in = null; |
|
253 |
try { |
|
254 |
File file = new File(path); |
|
255 |
in = new FileInputStream(file); |
|
256 |
byte[] bytes=new byte[(int)file.length()]; |
|
257 |
in.read(bytes); |
|
258 |
base64 = java.util.Base64.getEncoder().encodeToString(bytes); |
|
259 |
} catch (Exception e) { |
|
260 |
e.printStackTrace(); |
|
261 |
} finally { |
|
262 |
if (in != null) { |
|
263 |
try { |
|
264 |
in.close(); |
|
265 |
} catch (IOException e) { |
|
266 |
e.printStackTrace(); |
|
267 |
} |
|
268 |
} |
|
269 |
} |
|
270 |
return base64; |
|
271 |
} |
|
272 |
//BASE64解码成File文件 |
|
273 |
public static void base64ToFile(String destPath,String base64, String fileName) { |
|
274 |
File file = null; |
|
275 |
//创建文件目录 |
|
276 |
String filePath=destPath; |
|
277 |
File dir=new File(filePath); |
|
278 |
if (!dir.exists() && !dir.isDirectory()) { |
|
279 |
dir.mkdirs(); |
|
280 |
} |
|
281 |
BufferedOutputStream bos = null; |
|
282 |
java.io.FileOutputStream fos = null; |
|
283 |
try { |
|
284 |
byte[] bytes = java.util.Base64.getDecoder().decode(base64); |
|
285 |
file=new File(filePath+"/"+fileName); |
|
286 |
fos = new java.io.FileOutputStream(file); |
|
287 |
bos = new BufferedOutputStream(fos); |
|
288 |
bos.write(bytes); |
|
289 |
} catch (Exception e) { |
|
290 |
e.printStackTrace(); |
|
291 |
} finally { |
|
292 |
if (bos != null) { |
|
293 |
try { |
|
294 |
bos.close(); |
|
295 |
} catch (IOException e) { |
|
296 |
e.printStackTrace(); |
|
297 |
} |
|
298 |
} |
|
299 |
if (fos != null) { |
|
300 |
try { |
|
301 |
fos.close(); |
|
302 |
} catch (IOException e) { |
|
303 |
e.printStackTrace(); |
|
304 |
} |
|
305 |
} |
|
306 |
} |
|
307 |
} |
a6a76f
|
308 |
} |