Skip to content

Commit 4daaf61

Browse files
authored
FileUtils修改/验证码可配置 (elunez#403)
1、FileUtils 提供统一的系统临时目录获取方式 2、替换系统中异常错误打印方式 ,全部依赖改为Slf4j,实现错误信息的堆栈保留 修改配置文件 1、修改 配置信息 获取方式 2、调整原验证码 相关配置信息 3、新增多种验证码 方式
1 parent 38a8516 commit 4daaf61

23 files changed

Lines changed: 705 additions & 216 deletions

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2019-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package me.zhengjie.exception;
17+
18+
/**
19+
* 统一关于错误配置信息 异常
20+
*
21+
* @author: liaojinlong
22+
* @date: 2020/6/10 18:06
23+
*/
24+
public class BadConfigurationException extends RuntimeException {
25+
/**
26+
* Constructs a new runtime exception with {@code null} as its
27+
* detail message. The cause is not initialized, and may subsequently be
28+
* initialized by a call to {@link #initCause}.
29+
*/
30+
public BadConfigurationException() {
31+
super();
32+
}
33+
34+
/**
35+
* Constructs a new runtime exception with the specified detail message.
36+
* The cause is not initialized, and may subsequently be initialized by a
37+
* call to {@link #initCause}.
38+
*
39+
* @param message the detail message. The detail message is saved for
40+
* later retrieval by the {@link #getMessage()} method.
41+
*/
42+
public BadConfigurationException(String message) {
43+
super(message);
44+
}
45+
46+
/**
47+
* Constructs a new runtime exception with the specified detail message and
48+
* cause. <p>Note that the detail message associated with
49+
* {@code cause} is <i>not</i> automatically incorporated in
50+
* this runtime exception's detail message.
51+
*
52+
* @param message the detail message (which is saved for later retrieval
53+
* by the {@link #getMessage()} method).
54+
* @param cause the cause (which is saved for later retrieval by the
55+
* {@link #getCause()} method). (A {@code null} value is
56+
* permitted, and indicates that the cause is nonexistent or
57+
* unknown.)
58+
* @since 1.4
59+
*/
60+
public BadConfigurationException(String message, Throwable cause) {
61+
super(message, cause);
62+
}
63+
64+
/**
65+
* Constructs a new runtime exception with the specified cause and a
66+
* detail message of {@code (cause==null ? null : cause.toString())}
67+
* (which typically contains the class and detail message of
68+
* {@code cause}). This constructor is useful for runtime exceptions
69+
* that are little more than wrappers for other throwables.
70+
*
71+
* @param cause the cause (which is saved for later retrieval by the
72+
* {@link #getCause()} method). (A {@code null} value is
73+
* permitted, and indicates that the cause is nonexistent or
74+
* unknown.)
75+
* @since 1.4
76+
*/
77+
public BadConfigurationException(Throwable cause) {
78+
super(cause);
79+
}
80+
81+
/**
82+
* Constructs a new runtime exception with the specified detail
83+
* message, cause, suppression enabled or disabled, and writable
84+
* stack trace enabled or disabled.
85+
*
86+
* @param message the detail message.
87+
* @param cause the cause. (A {@code null} value is permitted,
88+
* and indicates that the cause is nonexistent or unknown.)
89+
* @param enableSuppression whether or not suppression is enabled
90+
* or disabled
91+
* @param writableStackTrace whether or not the stack trace should
92+
* be writable
93+
* @since 1.7
94+
*/
95+
protected BadConfigurationException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
96+
super(message, cause, enableSuppression, writableStackTrace);
97+
}
98+
}

eladmin-common/src/main/java/me/zhengjie/utils/FileUtil.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import cn.hutool.poi.excel.ExcelUtil;
2222
import me.zhengjie.exception.BadRequestException;
2323
import org.apache.poi.util.IOUtils;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
2426
import org.springframework.web.multipart.MultipartFile;
2527

2628
import javax.servlet.ServletOutputStream;
@@ -41,7 +43,20 @@
4143
* @date 2018-12-27
4244
*/
4345
public class FileUtil extends cn.hutool.core.io.FileUtil {
44-
46+
private static final Logger log = LoggerFactory.getLogger(FileUtil.class);
47+
/**
48+
* 系统临时目录
49+
* <br>
50+
* windows 包含路径分割符,但Linux 不包含,
51+
* 在windows \\==\ 前提下,
52+
* 为安全起见 同意拼装 路径分割符,
53+
* <pre>
54+
* java.io.tmpdir
55+
* windows : C:\Users/xxx\AppData\Local\Temp\
56+
* linux: /temp
57+
* </pre>
58+
*/
59+
public static final String SYS_TEM_DIR = System.getProperty("java.io.tmpdir") + File.separator;
4560
/**
4661
* 定义GB的计算常量
4762
*/
@@ -75,7 +90,7 @@ public static File toFile(MultipartFile multipartFile) {
7590
// MultipartFile to File
7691
multipartFile.transferTo(file);
7792
} catch (IOException e) {
78-
e.printStackTrace();
93+
log.error(e.getMessage(), e);
7994
}
8095
return file;
8196
}
@@ -130,7 +145,7 @@ public static String getSize(long size) {
130145
* inputStream 转 File
131146
*/
132147
static File inputStreamToFile(InputStream ins, String name) throws Exception {
133-
File file = new File(System.getProperty("java.io.tmpdir") + File.separator + name);
148+
File file = new File(SYS_TEM_DIR + name);
134149
if (file.exists()) {
135150
return file;
136151
}
@@ -170,7 +185,7 @@ public static File upload(MultipartFile file, String filePath) {
170185
file.transferTo(dest);
171186
return dest;
172187
} catch (Exception e) {
173-
e.printStackTrace();
188+
log.error(e.getMessage(), e);
174189
}
175190
return null;
176191
}
@@ -179,7 +194,7 @@ public static File upload(MultipartFile file, String filePath) {
179194
* 导出excel
180195
*/
181196
public static void downloadExcel(List<Map<String, Object>> list, HttpServletResponse response) throws IOException {
182-
String tempPath = System.getProperty("java.io.tmpdir") + IdUtil.fastSimpleUUID() + ".xlsx";
197+
String tempPath = SYS_TEM_DIR + IdUtil.fastSimpleUUID() + ".xlsx";
183198
File file = new File(tempPath);
184199
BigExcelWriter writer = ExcelUtil.getBigWriter(file);
185200
// 一次性写出内容,使用默认样式,强制输出标题
@@ -246,10 +261,10 @@ private static byte[] getByte(File file) {
246261
try {
247262
System.out.println(in.read(b));
248263
} catch (IOException e) {
249-
e.printStackTrace();
264+
log.error(e.getMessage(), e);
250265
}
251266
} catch (FileNotFoundException e) {
252-
e.printStackTrace();
267+
log.error(e.getMessage(), e);
253268
return null;
254269
}
255270
return b;
@@ -272,7 +287,7 @@ private static String getMd5(byte[] bytes) {
272287
}
273288
return new String(str);
274289
} catch (Exception e) {
275-
e.printStackTrace();
290+
log.error(e.getMessage(), e);
276291
}
277292
return null;
278293
}
@@ -294,7 +309,7 @@ public static void downloadFile(HttpServletRequest request, HttpServletResponse
294309
IOUtils.copy(fis, response.getOutputStream());
295310
response.flushBuffer();
296311
} catch (Exception e) {
297-
e.printStackTrace();
312+
log.error(e.getMessage(), e);
298313
} finally {
299314
if (fis != null) {
300315
try {
@@ -303,7 +318,7 @@ public static void downloadFile(HttpServletRequest request, HttpServletResponse
303318
file.deleteOnExit();
304319
}
305320
} catch (IOException e) {
306-
e.printStackTrace();
321+
log.error(e.getMessage(), e);
307322
}
308323
}
309324
}

0 commit comments

Comments
 (0)