作者 稚屿

代码优化

@@ -41,8 +41,7 @@ public class DictUtils @@ -41,8 +41,7 @@ public class DictUtils
41 Object cacheObj = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key)); 41 Object cacheObj = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
42 if (StringUtils.isNotNull(cacheObj)) 42 if (StringUtils.isNotNull(cacheObj))
43 { 43 {
44 - List<SysDictData> dictDatas = StringUtils.cast(cacheObj);  
45 - return dictDatas; 44 + return StringUtils.cast(cacheObj);
46 } 45 }
47 return null; 46 return null;
48 } 47 }
@@ -18,8 +18,7 @@ public class ExceptionUtil @@ -18,8 +18,7 @@ public class ExceptionUtil
18 { 18 {
19 StringWriter sw = new StringWriter(); 19 StringWriter sw = new StringWriter();
20 e.printStackTrace(new PrintWriter(sw, true)); 20 e.printStackTrace(new PrintWriter(sw, true));
21 - String str = sw.toString();  
22 - return str; 21 + return sw.toString();
23 } 22 }
24 23
25 public static String getRootErrorMessage(Exception e) 24 public static String getRootErrorMessage(Exception e)
@@ -99,9 +99,8 @@ public class ServletUtils @@ -99,9 +99,8 @@ public class ServletUtils
99 * 99 *
100 * @param response 渲染对象 100 * @param response 渲染对象
101 * @param string 待渲染的字符串 101 * @param string 待渲染的字符串
102 - * @return null  
103 */ 102 */
104 - public static String renderString(HttpServletResponse response, String string) 103 + public static void renderString(HttpServletResponse response, String string)
105 { 104 {
106 try 105 try
107 { 106 {
@@ -114,7 +113,6 @@ public class ServletUtils @@ -114,7 +113,6 @@ public class ServletUtils
114 { 113 {
115 e.printStackTrace(); 114 e.printStackTrace();
116 } 115 }
117 - return null;  
118 } 116 }
119 117
120 /** 118 /**
@@ -125,13 +123,13 @@ public class ServletUtils @@ -125,13 +123,13 @@ public class ServletUtils
125 public static boolean isAjaxRequest(HttpServletRequest request) 123 public static boolean isAjaxRequest(HttpServletRequest request)
126 { 124 {
127 String accept = request.getHeader("accept"); 125 String accept = request.getHeader("accept");
128 - if (accept != null && accept.indexOf("application/json") != -1) 126 + if (accept != null && accept.contains("application/json"))
129 { 127 {
130 return true; 128 return true;
131 } 129 }
132 130
133 String xRequestedWith = request.getHeader("X-Requested-With"); 131 String xRequestedWith = request.getHeader("X-Requested-With");
134 - if (xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1) 132 + if (xRequestedWith != null && xRequestedWith.contains("XMLHttpRequest"))
135 { 133 {
136 return true; 134 return true;
137 } 135 }
@@ -143,10 +141,6 @@ public class ServletUtils @@ -143,10 +141,6 @@ public class ServletUtils
143 } 141 }
144 142
145 String ajax = request.getParameter("__ajax"); 143 String ajax = request.getParameter("__ajax");
146 - if (StringUtils.inStringIgnoreCase(ajax, "json", "xml"))  
147 - {  
148 - return true;  
149 - }  
150 - return false; 144 + return StringUtils.inStringIgnoreCase(ajax, "json", "xml");
151 } 145 }
152 } 146 }
@@ -208,7 +208,6 @@ public class VerifyCodeUtils @@ -208,7 +208,6 @@ public class VerifyCodeUtils
208 208
209 int period = random.nextInt(40) + 10; // 50; 209 int period = random.nextInt(40) + 10; // 50;
210 210
211 - boolean borderGap = true;  
212 int frames = 20; 211 int frames = 20;
213 int phase = 7; 212 int phase = 7;
214 for (int i = 0; i < w1; i++) 213 for (int i = 0; i < w1; i++)
@@ -216,13 +215,9 @@ public class VerifyCodeUtils @@ -216,13 +215,9 @@ public class VerifyCodeUtils
216 double d = (double) (period >> 1) 215 double d = (double) (period >> 1)
217 * Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames); 216 * Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames);
218 g.copyArea(i, 0, 1, h1, 0, (int) d); 217 g.copyArea(i, 0, 1, h1, 0, (int) d);
219 - if (borderGap)  
220 - {  
221 - g.setColor(color);  
222 - g.drawLine(i, (int) d, i, 0);  
223 - g.drawLine(i, (int) d + h1, i, h1);  
224 - }  
225 - 218 + g.setColor(color);
  219 + g.drawLine(i, (int) d, i, 0);
  220 + g.drawLine(i, (int) d + h1, i, h1);
226 } 221 }
227 } 222 }
228 } 223 }
@@ -2,6 +2,8 @@ package com.ruoyi.common.utils.file; @@ -2,6 +2,8 @@ package com.ruoyi.common.utils.file;
2 2
3 import java.io.File; 3 import java.io.File;
4 import java.io.IOException; 4 import java.io.IOException;
  5 +import java.util.Objects;
  6 +
5 import org.apache.commons.io.FilenameUtils; 7 import org.apache.commons.io.FilenameUtils;
6 import org.springframework.web.multipart.MultipartFile; 8 import org.springframework.web.multipart.MultipartFile;
7 import com.ruoyi.common.config.RuoYiConfig; 9 import com.ruoyi.common.config.RuoYiConfig;
@@ -100,7 +102,7 @@ public class FileUploadUtils @@ -100,7 +102,7 @@ public class FileUploadUtils
100 throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException, 102 throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
101 InvalidExtensionException 103 InvalidExtensionException
102 { 104 {
103 - int fileNamelength = file.getOriginalFilename().length(); 105 + int fileNamelength = Objects.requireNonNull(file.getOriginalFilename()).length();
104 if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) 106 if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
105 { 107 {
106 throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH); 108 throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
@@ -112,8 +114,7 @@ public class FileUploadUtils @@ -112,8 +114,7 @@ public class FileUploadUtils
112 114
113 File desc = getAbsoluteFile(baseDir, fileName); 115 File desc = getAbsoluteFile(baseDir, fileName);
114 file.transferTo(desc); 116 file.transferTo(desc);
115 - String pathFileName = getPathFileName(baseDir, fileName);  
116 - return pathFileName; 117 + return getPathFileName(baseDir, fileName);
117 } 118 }
118 119
119 /** 120 /**
@@ -145,8 +146,7 @@ public class FileUploadUtils @@ -145,8 +146,7 @@ public class FileUploadUtils
145 { 146 {
146 int dirLastIndex = RuoYiConfig.getProfile().length() + 1; 147 int dirLastIndex = RuoYiConfig.getProfile().length() + 1;
147 String currentDir = StringUtils.substring(uploadDir, dirLastIndex); 148 String currentDir = StringUtils.substring(uploadDir, dirLastIndex);
148 - String pathFileName = Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;  
149 - return pathFileName; 149 + return Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
150 } 150 }
151 151
152 /** 152 /**
@@ -161,7 +161,7 @@ public class FileUploadUtils @@ -161,7 +161,7 @@ public class FileUploadUtils
161 throws FileSizeLimitExceededException, InvalidExtensionException 161 throws FileSizeLimitExceededException, InvalidExtensionException
162 { 162 {
163 long size = file.getSize(); 163 long size = file.getSize();
164 - if (DEFAULT_MAX_SIZE != -1 && size > DEFAULT_MAX_SIZE) 164 + if (size > DEFAULT_MAX_SIZE)
165 { 165 {
166 throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024); 166 throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024);
167 } 167 }
@@ -228,7 +228,7 @@ public class FileUploadUtils @@ -228,7 +228,7 @@ public class FileUploadUtils
228 String extension = FilenameUtils.getExtension(file.getOriginalFilename()); 228 String extension = FilenameUtils.getExtension(file.getOriginalFilename());
229 if (StringUtils.isEmpty(extension)) 229 if (StringUtils.isEmpty(extension))
230 { 230 {
231 - extension = MimeTypeUtils.getExtension(file.getContentType()); 231 + extension = MimeTypeUtils.getExtension(Objects.requireNonNull(file.getContentType()));
232 } 232 }
233 return extension; 233 return extension;
234 } 234 }
@@ -59,7 +59,7 @@ public class ImageUtils @@ -59,7 +59,7 @@ public class ImageUtils
59 /** 59 /**
60 * 读取文件为字节数据 60 * 读取文件为字节数据
61 * 61 *
62 - * @param key 地址 62 + * @param url 地址
63 * @return 字节数据 63 * @return 字节数据
64 */ 64 */
65 public static byte[] readFile(String url) 65 public static byte[] readFile(String url)
@@ -5,6 +5,7 @@ import java.io.IOException; @@ -5,6 +5,7 @@ import java.io.IOException;
5 import java.io.InputStream; 5 import java.io.InputStream;
6 import java.io.InputStreamReader; 6 import java.io.InputStreamReader;
7 import java.nio.charset.Charset; 7 import java.nio.charset.Charset;
  8 +import java.nio.charset.StandardCharsets;
8 import javax.servlet.ServletRequest; 9 import javax.servlet.ServletRequest;
9 import org.apache.commons.lang3.exception.ExceptionUtils; 10 import org.apache.commons.lang3.exception.ExceptionUtils;
10 import org.slf4j.Logger; 11 import org.slf4j.Logger;
@@ -25,7 +26,7 @@ public class HttpHelper @@ -25,7 +26,7 @@ public class HttpHelper
25 BufferedReader reader = null; 26 BufferedReader reader = null;
26 try (InputStream inputStream = request.getInputStream()) 27 try (InputStream inputStream = request.getInputStream())
27 { 28 {
28 - reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8"))); 29 + reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
29 String line = ""; 30 String line = "";
30 while ((line = reader.readLine()) != null) 31 while ((line = reader.readLine()) != null)
31 { 32 {
@@ -9,6 +9,7 @@ import java.net.ConnectException; @@ -9,6 +9,7 @@ import java.net.ConnectException;
9 import java.net.SocketTimeoutException; 9 import java.net.SocketTimeoutException;
10 import java.net.URL; 10 import java.net.URL;
11 import java.net.URLConnection; 11 import java.net.URLConnection;
  12 +import java.nio.charset.StandardCharsets;
12 import java.security.cert.X509Certificate; 13 import java.security.cert.X509Certificate;
13 import javax.net.ssl.HostnameVerifier; 14 import javax.net.ssl.HostnameVerifier;
14 import javax.net.ssl.HttpsURLConnection; 15 import javax.net.ssl.HttpsURLConnection;
@@ -130,9 +131,8 @@ public class HttpUtils @@ -130,9 +131,8 @@ public class HttpUtils
130 StringBuilder result = new StringBuilder(); 131 StringBuilder result = new StringBuilder();
131 try 132 try
132 { 133 {
133 - String urlNameString = url;  
134 - log.info("sendPost - {}", urlNameString);  
135 - URL realUrl = new URL(urlNameString); 134 + log.info("sendPost - {}", url);
  135 + URL realUrl = new URL(url);
136 URLConnection conn = realUrl.openConnection(); 136 URLConnection conn = realUrl.openConnection();
137 conn.setRequestProperty("accept", "*/*"); 137 conn.setRequestProperty("accept", "*/*");
138 conn.setRequestProperty("connection", "Keep-Alive"); 138 conn.setRequestProperty("connection", "Keep-Alive");
@@ -144,7 +144,7 @@ public class HttpUtils @@ -144,7 +144,7 @@ public class HttpUtils
144 out = new PrintWriter(conn.getOutputStream()); 144 out = new PrintWriter(conn.getOutputStream());
145 out.print(param); 145 out.print(param);
146 out.flush(); 146 out.flush();
147 - in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8")); 147 + in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
148 String line; 148 String line;
149 while ((line = in.readLine()) != null) 149 while ((line = in.readLine()) != null)
150 { 150 {
@@ -218,7 +218,7 @@ public class HttpUtils @@ -218,7 +218,7 @@ public class HttpUtils
218 { 218 {
219 if (ret != null && !"".equals(ret.trim())) 219 if (ret != null && !"".equals(ret.trim()))
220 { 220 {
221 - result.append(new String(ret.getBytes("ISO-8859-1"), "utf-8")); 221 + result.append(new String(ret.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
222 } 222 }
223 } 223 }
224 log.info("recv - {}", result); 224 log.info("recv - {}", result);
@@ -25,7 +25,6 @@ public class AddressUtils @@ -25,7 +25,6 @@ public class AddressUtils
25 25
26 public static String getRealAddressByIP(String ip) 26 public static String getRealAddressByIP(String ip)
27 { 27 {
28 - String address = UNKNOWN;  
29 // 内网不查询 28 // 内网不查询
30 if (IpUtils.internalIp(ip)) 29 if (IpUtils.internalIp(ip))
31 { 30 {
@@ -51,6 +50,6 @@ public class AddressUtils @@ -51,6 +50,6 @@ public class AddressUtils
51 log.error("获取地理位置异常 {}", ip); 50 log.error("获取地理位置异常 {}", ip);
52 } 51 }
53 } 52 }
54 - return address; 53 + return UNKNOWN;
55 } 54 }
56 } 55 }
@@ -1122,7 +1122,7 @@ public class ExcelUtil<T> @@ -1122,7 +1122,7 @@ public class ExcelUtil<T>
1122 if (StringUtils.isNotEmpty(excel.targetAttr())) 1122 if (StringUtils.isNotEmpty(excel.targetAttr()))
1123 { 1123 {
1124 String target = excel.targetAttr(); 1124 String target = excel.targetAttr();
1125 - if (target.indexOf(".") > -1) 1125 + if (target.contains("."))
1126 { 1126 {
1127 String[] targets = target.split("[.]"); 1127 String[] targets = target.split("[.]");
1128 for (String name : targets) 1128 for (String name : targets)
@@ -1217,7 +1217,7 @@ public class ExcelUtil<T> @@ -1217,7 +1217,7 @@ public class ExcelUtil<T>
1217 for (Object[] os : this.fields) 1217 for (Object[] os : this.fields)
1218 { 1218 {
1219 Excel excel = (Excel) os[1]; 1219 Excel excel = (Excel) os[1];
1220 - maxHeight = maxHeight > excel.height() ? maxHeight : excel.height(); 1220 + maxHeight = Math.max(maxHeight, excel.height());
1221 } 1221 }
1222 return (short) (maxHeight * 20); 1222 return (short) (maxHeight * 20);
1223 } 1223 }
1 package com.ruoyi.common.utils.sign; 1 package com.ruoyi.common.utils.sign;
2 2
  3 +import java.nio.charset.StandardCharsets;
3 import java.security.MessageDigest; 4 import java.security.MessageDigest;
4 import org.slf4j.Logger; 5 import org.slf4j.Logger;
5 import org.slf4j.LoggerFactory; 6 import org.slf4j.LoggerFactory;
@@ -55,7 +56,7 @@ public class Md5Utils @@ -55,7 +56,7 @@ public class Md5Utils
55 { 56 {
56 try 57 try
57 { 58 {
58 - return new String(toHex(md5(s)).getBytes("UTF-8"), "UTF-8"); 59 + return new String(toHex(md5(s)).getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);
59 } 60 }
60 catch (Exception e) 61 catch (Exception e)
61 { 62 {
@@ -50,10 +50,8 @@ public class SqlUtil @@ -50,10 +50,8 @@ public class SqlUtil
50 return; 50 return;
51 } 51 }
52 String[] sqlKeywords = StringUtils.split(SQL_REGEX, "\\|"); 52 String[] sqlKeywords = StringUtils.split(SQL_REGEX, "\\|");
53 - for (int i = 0; i < sqlKeywords.length; i++)  
54 - {  
55 - if (StringUtils.indexOfIgnoreCase(value, sqlKeywords[i]) > -1)  
56 - { 53 + for (String sqlKeyword : sqlKeywords) {
  54 + if (StringUtils.indexOfIgnoreCase(value, sqlKeyword) > -1) {
57 throw new UtilException("参数存在SQL注入风险"); 55 throw new UtilException("参数存在SQL注入风险");
58 } 56 }
59 } 57 }
@@ -151,8 +151,7 @@ public class GenUtils @@ -151,8 +151,7 @@ public class GenUtils
151 { 151 {
152 int lastIndex = packageName.lastIndexOf("."); 152 int lastIndex = packageName.lastIndexOf(".");
153 int nameLength = packageName.length(); 153 int nameLength = packageName.length();
154 - String moduleName = StringUtils.substring(packageName, lastIndex + 1, nameLength);  
155 - return moduleName; 154 + return StringUtils.substring(packageName, lastIndex + 1, nameLength);
156 } 155 }
157 156
158 /** 157 /**
@@ -165,8 +164,7 @@ public class GenUtils @@ -165,8 +164,7 @@ public class GenUtils
165 { 164 {
166 int lastIndex = tableName.lastIndexOf("_"); 165 int lastIndex = tableName.lastIndexOf("_");
167 int nameLength = tableName.length(); 166 int nameLength = tableName.length();
168 - String businessName = StringUtils.substring(tableName, lastIndex + 1, nameLength);  
169 - return businessName; 167 + return StringUtils.substring(tableName, lastIndex + 1, nameLength);
170 } 168 }
171 169
172 /** 170 /**
@@ -228,8 +228,7 @@ public class VelocityUtils @@ -228,8 +228,7 @@ public class VelocityUtils
228 public static String getPackagePrefix(String packageName) 228 public static String getPackagePrefix(String packageName)
229 { 229 {
230 int lastIndex = packageName.lastIndexOf("."); 230 int lastIndex = packageName.lastIndexOf(".");
231 - String basePackage = StringUtils.substring(packageName, 0, lastIndex);  
232 - return basePackage; 231 + return StringUtils.substring(packageName, 0, lastIndex);
233 } 232 }
234 233
235 /** 234 /**
@@ -63,12 +63,9 @@ public class SysDeptServiceImpl implements ISysDeptService @@ -63,12 +63,9 @@ public class SysDeptServiceImpl implements ISysDeptService
63 { 63 {
64 tempList.add(dept.getDeptId()); 64 tempList.add(dept.getDeptId());
65 } 65 }
66 - for (Iterator<SysDept> iterator = depts.iterator(); iterator.hasNext();)  
67 - {  
68 - SysDept dept = (SysDept) iterator.next(); 66 + for (SysDept dept : depts) {
69 // 如果是顶级节点, 遍历该父节点的所有子节点 67 // 如果是顶级节点, 遍历该父节点的所有子节点
70 - if (!tempList.contains(dept.getParentId()))  
71 - { 68 + if (!tempList.contains(dept.getParentId())) {
72 recursionFn(depts, dept); 69 recursionFn(depts, dept);
73 returnList.add(dept); 70 returnList.add(dept);
74 } 71 }
@@ -498,7 +498,7 @@ public class SysMenuServiceImpl implements ISysMenuService @@ -498,7 +498,7 @@ public class SysMenuServiceImpl implements ISysMenuService
498 */ 498 */
499 private boolean hasChild(List<SysMenu> list, SysMenu t) 499 private boolean hasChild(List<SysMenu> list, SysMenu t)
500 { 500 {
501 - return getChildList(list, t).size() > 0 ? true : false; 501 + return getChildList(list, t).size() > 0;
502 } 502 }
503 503
504 /** 504 /**