优化代码

This commit is contained in:
RuoYi 2022-02-12 12:55:13 +08:00
parent 81bbc157c4
commit afba71dfcf
27 changed files with 58 additions and 78 deletions

View File

@ -41,8 +41,7 @@ public class DictUtils
Object cacheObj = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
if (StringUtils.isNotNull(cacheObj))
{
List<SysDictData> dictDatas = StringUtils.cast(cacheObj);
return dictDatas;
return StringUtils.cast(cacheObj);
}
return null;
}
@ -92,7 +91,7 @@ public class DictUtils
{
if (value.equals(dict.getDictValue()))
{
propertyString.append(dict.getDictLabel() + separator);
propertyString.append(dict.getDictLabel()).append(separator);
break;
}
}
@ -132,7 +131,7 @@ public class DictUtils
{
if (label.equals(dict.getDictLabel()))
{
propertyString.append(dict.getDictValue() + separator);
propertyString.append(dict.getDictValue()).append(separator);
break;
}
}

View File

@ -18,8 +18,7 @@ public class ExceptionUtil
{
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw, true));
String str = sw.toString();
return str;
return sw.toString();
}
public static String getRootErrorMessage(Exception e)

View File

@ -99,9 +99,8 @@ public class ServletUtils
*
* @param response 渲染对象
* @param string 待渲染的字符串
* @return null
*/
public static String renderString(HttpServletResponse response, String string)
public static void renderString(HttpServletResponse response, String string)
{
try
{
@ -114,7 +113,6 @@ public class ServletUtils
{
e.printStackTrace();
}
return null;
}
/**
@ -125,13 +123,13 @@ public class ServletUtils
public static boolean isAjaxRequest(HttpServletRequest request)
{
String accept = request.getHeader("accept");
if (accept != null && accept.indexOf("application/json") != -1)
if (accept != null && accept.contains("application/json"))
{
return true;
}
String xRequestedWith = request.getHeader("X-Requested-With");
if (xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1)
if (xRequestedWith != null && xRequestedWith.contains("XMLHttpRequest"))
{
return true;
}
@ -143,10 +141,6 @@ public class ServletUtils
}
String ajax = request.getParameter("__ajax");
if (StringUtils.inStringIgnoreCase(ajax, "json", "xml"))
{
return true;
}
return false;
return StringUtils.inStringIgnoreCase(ajax, "json", "xml");
}
}

View File

@ -2,6 +2,7 @@ package com.ruoyi.common.utils.file;
import java.io.File;
import java.io.IOException;
import java.util.Objects;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.constant.Constants;
@ -15,7 +16,7 @@ import com.ruoyi.framework.config.RuoYiConfig;
/**
* 文件上传工具类
*
*
* @author ruoyi
*/
public class FileUploadUtils
@ -89,7 +90,7 @@ public class FileUploadUtils
*
* @param baseDir 相对应用的基目录
* @param file 上传的文件
* @param extension 上传文件类型
* @param allowedExtension 上传文件类型
* @return 返回上传成功的文件名
* @throws FileSizeLimitExceededException 如果超出最大大小
* @throws FileNameLengthLimitExceededException 文件名太长
@ -100,7 +101,7 @@ public class FileUploadUtils
throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
InvalidExtensionException
{
int fileNamelength = file.getOriginalFilename().length();
int fileNamelength = Objects.requireNonNull(file.getOriginalFilename()).length();
if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
{
throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
@ -112,8 +113,7 @@ public class FileUploadUtils
File desc = getAbsoluteFile(baseDir, fileName);
file.transferTo(desc);
String pathFileName = getPathFileName(baseDir, fileName);
return pathFileName;
return getPathFileName(baseDir, fileName);
}
/**
@ -145,8 +145,7 @@ public class FileUploadUtils
{
int dirLastIndex = RuoYiConfig.getProfile().length() + 1;
String currentDir = StringUtils.substring(uploadDir, dirLastIndex);
String pathFileName = Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
return pathFileName;
return Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
}
/**
@ -161,7 +160,7 @@ public class FileUploadUtils
throws FileSizeLimitExceededException, InvalidExtensionException
{
long size = file.getSize();
if (DEFAULT_MAX_SIZE != -1 && size > DEFAULT_MAX_SIZE)
if (size > DEFAULT_MAX_SIZE)
{
throw new FileSizeLimitExceededException(DEFAULT_MAX_SIZE / 1024 / 1024);
}
@ -219,7 +218,7 @@ public class FileUploadUtils
/**
* 获取文件名的后缀
*
*
* @param file 表单文件
* @return 后缀名
*/
@ -228,8 +227,8 @@ public class FileUploadUtils
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
if (StringUtils.isEmpty(extension))
{
extension = MimeTypeUtils.getExtension(file.getContentType());
extension = MimeTypeUtils.getExtension(Objects.requireNonNull(file.getContentType()));
}
return extension;
}
}
}

View File

@ -59,7 +59,7 @@ public class ImageUtils
/**
* 读取文件为字节数据
*
* @param key 地址
* @param url 地址
* @return 字节数据
*/
public static byte[] readFile(String url)

View File

@ -4,7 +4,7 @@ import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import javax.servlet.ServletRequest;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
@ -25,7 +25,7 @@ public class HttpHelper
BufferedReader reader = null;
try (InputStream inputStream = request.getInputStream())
{
reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
String line = "";
while ((line = reader.readLine()) != null)
{

View File

@ -9,6 +9,7 @@ import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
@ -130,9 +131,8 @@ public class HttpUtils
StringBuilder result = new StringBuilder();
try
{
String urlNameString = url;
log.info("sendPost - {}", urlNameString);
URL realUrl = new URL(urlNameString);
log.info("sendPost - {}", url);
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
@ -144,7 +144,7 @@ public class HttpUtils
out = new PrintWriter(conn.getOutputStream());
out.print(param);
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
String line;
while ((line = in.readLine()) != null)
{
@ -218,7 +218,7 @@ public class HttpUtils
{
if (ret != null && !"".equals(ret.trim()))
{
result.append(new String(ret.getBytes("ISO-8859-1"), "utf-8"));
result.append(new String(ret.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
}
}
log.info("recv - {}", result);

View File

@ -25,7 +25,6 @@ public class AddressUtils
public static String getRealAddressByIP(String ip)
{
String address = UNKNOWN;
// 内网不查询
if (IpUtils.internalIp(ip))
{
@ -51,6 +50,6 @@ public class AddressUtils
log.error("获取地理位置异常 {}", ip);
}
}
return address;
return UNKNOWN;
}
}

View File

@ -1121,7 +1121,7 @@ public class ExcelUtil<T>
if (StringUtils.isNotEmpty(excel.targetAttr()))
{
String target = excel.targetAttr();
if (target.indexOf(".") > -1)
if (target.contains("."))
{
String[] targets = target.split("[.]");
for (String name : targets)
@ -1216,7 +1216,7 @@ public class ExcelUtil<T>
for (Object[] os : this.fields)
{
Excel excel = (Excel) os[1];
maxHeight = maxHeight > excel.height() ? maxHeight : excel.height();
maxHeight = Math.max(maxHeight, excel.height());
}
return (short) (maxHeight * 20);
}

View File

@ -1,5 +1,6 @@
package com.ruoyi.common.utils.sign;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -55,7 +56,7 @@ public class Md5Utils
{
try
{
return new String(toHex(md5(s)).getBytes("UTF-8"), "UTF-8");
return new String(toHex(md5(s)).getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);
}
catch (Exception e)
{

View File

@ -50,9 +50,9 @@ public class SqlUtil
return;
}
String[] sqlKeywords = StringUtils.split(SQL_REGEX, "\\|");
for (int i = 0; i < sqlKeywords.length; i++)
for (String sqlKeyword : sqlKeywords)
{
if (StringUtils.indexOfIgnoreCase(value, sqlKeywords[i]) > -1)
if (StringUtils.indexOfIgnoreCase(value, sqlKeyword) > -1)
{
throw new UtilException("参数存在SQL注入风险");
}

View File

@ -25,7 +25,7 @@ public class LoginBody
/**
* 唯一标识
*/
private String uuid = "";
private String uuid;
public String getUsername()
{

View File

@ -100,7 +100,7 @@ public class SysRegisterService
*/
public void validateCaptcha(String username, String code, String uuid)
{
String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
String verifyKey = Constants.CAPTCHA_CODE_KEY + StringUtils.nvl(uuid, "");
String captcha = redisCache.getCacheObject(verifyKey);
redisCache.deleteObject(verifyKey);
if (captcha == null)

View File

@ -29,7 +29,7 @@ public interface ISysLogininforService
* 批量删除系统登录日志
*
* @param infoIds 需要删除的登录日志ID
* @return
* @return 结果
*/
public int deleteLogininforByIds(Long[] infoIds);

View File

@ -46,7 +46,7 @@ public class SysLogininforServiceImpl implements ISysLogininforService
* 批量删除系统登录日志
*
* @param infoIds 需要删除的登录日志ID
* @return
* @return 结果
*/
@Override
public int deleteLogininforByIds(Long[] infoIds)

View File

@ -61,7 +61,6 @@ public interface ISysConfigService
* 批量删除参数信息
*
* @param configIds 需要删除的参数ID
* @return 结果
*/
public void deleteConfigByIds(Long[] configIds);

View File

@ -39,7 +39,6 @@ public interface ISysDictDataService
* 批量删除字典数据信息
*
* @param dictCodes 需要删除的字典数据ID
* @return 结果
*/
public void deleteDictDataByIds(Long[] dictCodes);

View File

@ -54,7 +54,6 @@ public interface ISysDictTypeService
* 批量删除字典信息
*
* @param dictIds 需要删除的字典ID
* @return 结果
*/
public void deleteDictTypeByIds(Long[] dictIds);

View File

@ -78,7 +78,6 @@ public interface ISysPostService
*
* @param postIds 需要删除的岗位ID
* @return 结果
* @throws Exception 异常
*/
public int deletePostByIds(Long[] postIds);

View File

@ -143,7 +143,6 @@ public class SysConfigServiceImpl implements ISysConfigService
* 批量删除参数信息
*
* @param configIds 需要删除的参数ID
* @return 结果
*/
@Override
public void deleteConfigByIds(Long[] configIds)

View File

@ -63,9 +63,8 @@ public class SysDeptServiceImpl implements ISysDeptService
{
tempList.add(dept.getDeptId());
}
for (Iterator<SysDept> iterator = depts.iterator(); iterator.hasNext();)
for (SysDept dept : depts)
{
SysDept dept = (SysDept) iterator.next();
// 如果是顶级节点, 遍历该父节点的所有子节点
if (!tempList.contains(dept.getParentId()))
{

View File

@ -60,7 +60,6 @@ public class SysDictDataServiceImpl implements ISysDictDataService
* 批量删除字典数据信息
*
* @param dictCodes 需要删除的字典数据ID
* @return 结果
*/
@Override
public void deleteDictDataByIds(Long[] dictCodes)

View File

@ -115,7 +115,6 @@ public class SysDictTypeServiceImpl implements ISysDictTypeService
* 批量删除字典类型信息
*
* @param dictIds 需要删除的字典ID
* @return 结果
*/
@Override
public void deleteDictTypeByIds(Long[] dictIds)

View File

@ -498,7 +498,7 @@ public class SysMenuServiceImpl implements ISysMenuService
*/
private boolean hasChild(List<SysMenu> list, SysMenu t)
{
return getChildList(list, t).size() > 0 ? true : false;
return getChildList(list, t).size() > 0;
}
/**

View File

@ -137,7 +137,6 @@ public class SysPostServiceImpl implements ISysPostService
*
* @param postIds 需要删除的岗位ID
* @return 结果
* @throws Exception 异常
*/
@Override
public int deletePostByIds(Long[] postIds)

View File

@ -151,8 +151,7 @@ public class GenUtils
{
int lastIndex = packageName.lastIndexOf(".");
int nameLength = packageName.length();
String moduleName = StringUtils.substring(packageName, lastIndex + 1, nameLength);
return moduleName;
return StringUtils.substring(packageName, lastIndex + 1, nameLength);
}
/**
@ -165,8 +164,7 @@ public class GenUtils
{
int lastIndex = tableName.lastIndexOf("_");
int nameLength = tableName.length();
String businessName = StringUtils.substring(tableName, lastIndex + 1, nameLength);
return businessName;
return StringUtils.substring(tableName, lastIndex + 1, nameLength);
}
/**
@ -255,4 +253,4 @@ public class GenUtils
return 0;
}
}
}
}

View File

@ -30,7 +30,7 @@ public class VelocityUtils
/**
* 设置模板变量信息
*
*
* @return 模板列表
*/
public static VelocityContext prepareContext(GenTable genTable)
@ -119,9 +119,10 @@ public class VelocityUtils
context.put("subclassName", StringUtils.uncapitalize(subClassName));
context.put("subImportList", getImportList(genTable.getSubTable()));
}
/**
* 获取模板信息
*
*
* @return 模板列表
*/
public static List<String> getTemplateList(String tplCategory)
@ -220,15 +221,14 @@ public class VelocityUtils
/**
* 获取包前缀
*
*
* @param packageName 包名称
* @return 包前缀名称
*/
public static String getPackagePrefix(String packageName)
{
int lastIndex = packageName.lastIndexOf(".");
String basePackage = StringUtils.substring(packageName, 0, lastIndex);
return basePackage;
return StringUtils.substring(packageName, 0, lastIndex);
}
/**
@ -285,7 +285,7 @@ public class VelocityUtils
/**
* 获取权限前缀
*
*
* @param moduleName 模块名称
* @param businessName 业务名称
* @return 返回权限前缀
@ -297,8 +297,8 @@ public class VelocityUtils
/**
* 获取上级菜单ID字段
*
* @param options 生成其他选项
*
* @param paramsObj 生成其他选项
* @return 上级菜单ID字段
*/
public static String getParentMenuId(JSONObject paramsObj)
@ -313,8 +313,8 @@ public class VelocityUtils
/**
* 获取树编码
*
* @param options 生成其他选项
*
* @param paramsObj 生成其他选项
* @return 树编码
*/
public static String getTreecode(JSONObject paramsObj)
@ -328,8 +328,8 @@ public class VelocityUtils
/**
* 获取树父编码
*
* @param options 生成其他选项
*
* @param paramsObj 生成其他选项
* @return 树父编码
*/
public static String getTreeParentCode(JSONObject paramsObj)
@ -343,8 +343,8 @@ public class VelocityUtils
/**
* 获取树名称
*
* @param options 生成其他选项
*
* @param paramsObj 生成其他选项
* @return 树名称
*/
public static String getTreeName(JSONObject paramsObj)
@ -358,7 +358,7 @@ public class VelocityUtils
/**
* 获取需要在哪一列上面显示展开按钮
*
*
* @param genTable 业务表对象
* @return 展开按钮列序号
*/
@ -382,4 +382,4 @@ public class VelocityUtils
}
return num;
}
}
}