若依开源1.1.3发布
This commit is contained in:
@ -4,6 +4,8 @@ import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import org.apache.commons.lang3.time.DateFormatUtils;
|
||||
|
||||
/**
|
||||
* 时间工具类
|
||||
*
|
||||
@ -11,9 +13,15 @@ import java.util.Date;
|
||||
*/
|
||||
public class DateUtils
|
||||
{
|
||||
public static final String DEFAULT_YYYYMMDD = "yyyyMMddHHmmss";
|
||||
public static String YYYY = "yyyy";
|
||||
|
||||
public static final String DEFAULT_YYYY_MM_DD = "yyyy-MM-dd HH:mm:ss";
|
||||
public static String YYYY_MM = "yyyy-MM";
|
||||
|
||||
public static String YYYY_MM_DD = "yyyy-MM-dd";
|
||||
|
||||
public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
|
||||
|
||||
public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
|
||||
|
||||
/**
|
||||
* 获取当前日期, 默认格式为yyyy-MM-dd
|
||||
@ -22,30 +30,30 @@ public class DateUtils
|
||||
*/
|
||||
public static String getDate()
|
||||
{
|
||||
return dateTimeNow("yyyy-MM-dd");
|
||||
return dateTimeNow(YYYY_MM_DD);
|
||||
}
|
||||
|
||||
public static final String dateTimeStr()
|
||||
public static final String getTime()
|
||||
{
|
||||
return dateTimeNow(DEFAULT_YYYY_MM_DD);
|
||||
return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
|
||||
}
|
||||
|
||||
public static final String dateTimeNow()
|
||||
{
|
||||
return dateTimeNow(DEFAULT_YYYYMMDD);
|
||||
return dateTimeNow(YYYYMMDDHHMMSS);
|
||||
}
|
||||
|
||||
public static final String dateTimeNow(final String format)
|
||||
{
|
||||
return dateTime(format, new Date());
|
||||
return parseDateToStr(format, new Date());
|
||||
}
|
||||
|
||||
public static final String dateTime(final Date date)
|
||||
{
|
||||
return dateTime(DEFAULT_YYYYMMDD, date);
|
||||
return parseDateToStr(YYYY_MM_DD, date);
|
||||
}
|
||||
|
||||
public static final String dateTime(final String format, final Date date)
|
||||
public static final String parseDateToStr(final String format, final Date date)
|
||||
{
|
||||
return new SimpleDateFormat(format).format(date);
|
||||
}
|
||||
@ -62,4 +70,22 @@ public class DateUtils
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期路径 即年/月/日 如2018/08/08
|
||||
*/
|
||||
public static final String datePath()
|
||||
{
|
||||
Date now = new Date();
|
||||
return DateFormatUtils.format(now, "yyyy/MM/dd");
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期路径 即年/月/日 如20180808
|
||||
*/
|
||||
public static final String dateTime()
|
||||
{
|
||||
Date now = new Date();
|
||||
return DateFormatUtils.format(now, "yyyyMMdd");
|
||||
}
|
||||
|
||||
}
|
||||
|
155
src/main/java/com/ruoyi/common/utils/FileUploadUtils.java
Normal file
155
src/main/java/com/ruoyi/common/utils/FileUploadUtils.java
Normal file
@ -0,0 +1,155 @@
|
||||
package com.ruoyi.common.utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import org.apache.shiro.crypto.hash.Md5Hash;
|
||||
import org.apache.tomcat.util.http.fileupload.FileUploadBase.FileSizeLimitExceededException;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import com.ruoyi.common.exception.file.FileNameLengthLimitExceededException;
|
||||
import com.ruoyi.framework.config.RuoYiConfig;
|
||||
|
||||
/**
|
||||
* 文件上传工具类
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class FileUploadUtils
|
||||
{
|
||||
|
||||
// 默认大小 50M
|
||||
public static final long DEFAULT_MAX_SIZE = 52428800;
|
||||
|
||||
// 默认上传的地址
|
||||
private static String defaultBaseDir = RuoYiConfig.getProfile();
|
||||
|
||||
// 默认的文件名最大长度
|
||||
public static final int DEFAULT_FILE_NAME_LENGTH = 200;
|
||||
|
||||
// 默认文件类型jpg
|
||||
public static final String IMAGE_JPG_EXTENSION = ".jpg";
|
||||
|
||||
private static int counter = 0;
|
||||
|
||||
public static void setDefaultBaseDir(String defaultBaseDir)
|
||||
{
|
||||
FileUploadUtils.defaultBaseDir = defaultBaseDir;
|
||||
}
|
||||
|
||||
public static String getDefaultBaseDir()
|
||||
{
|
||||
return defaultBaseDir;
|
||||
}
|
||||
|
||||
/**
|
||||
* 以默认配置进行文件上传
|
||||
*
|
||||
* @param file 上传的文件
|
||||
* @return 文件名称
|
||||
* @throws Exception
|
||||
*/
|
||||
public static final String upload(MultipartFile file) throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
return upload(getDefaultBaseDir(), file, FileUploadUtils.IMAGE_JPG_EXTENSION);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new IOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件路径上传
|
||||
*
|
||||
* @param baseDir 相对应用的基目录
|
||||
* @param file 上传的文件
|
||||
* @return 文件名称
|
||||
* @throws IOException
|
||||
*/
|
||||
public static final String upload(String baseDir, MultipartFile file) throws IOException
|
||||
{
|
||||
try
|
||||
{
|
||||
return upload(baseDir, file, FileUploadUtils.IMAGE_JPG_EXTENSION);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new IOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
*
|
||||
* @param baseDir 相对应用的基目录
|
||||
* @param file 上传的文件
|
||||
* @param needDatePathAndRandomName 是否需要日期目录和随机文件名前缀
|
||||
* @param extension 上传文件类型
|
||||
* @return 返回上传成功的文件名
|
||||
* @throws FileSizeLimitExceededException 如果超出最大大小
|
||||
* @throws FileNameLengthLimitExceededException 文件名太长
|
||||
* @throws IOException 比如读写文件出错时
|
||||
*/
|
||||
public static final String upload(String baseDir, MultipartFile file, String extension)
|
||||
throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException
|
||||
{
|
||||
|
||||
int fileNamelength = file.getOriginalFilename().length();
|
||||
if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH)
|
||||
{
|
||||
throw new FileNameLengthLimitExceededException(file.getOriginalFilename(), fileNamelength,
|
||||
FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
|
||||
}
|
||||
|
||||
assertAllowed(file);
|
||||
|
||||
String fileName = encodingFilename(file.getOriginalFilename(), extension);
|
||||
|
||||
File desc = getAbsoluteFile(baseDir, baseDir + fileName);
|
||||
file.transferTo(desc);
|
||||
return fileName;
|
||||
}
|
||||
|
||||
private static final File getAbsoluteFile(String uploadDir, String filename) throws IOException
|
||||
{
|
||||
File desc = new File(File.separator + filename);
|
||||
|
||||
if (!desc.getParentFile().exists())
|
||||
{
|
||||
desc.getParentFile().mkdirs();
|
||||
}
|
||||
if (!desc.exists())
|
||||
{
|
||||
desc.createNewFile();
|
||||
}
|
||||
return desc;
|
||||
}
|
||||
|
||||
/**
|
||||
* 编码文件名
|
||||
*/
|
||||
private static final String encodingFilename(String filename, String extension)
|
||||
{
|
||||
filename = filename.replace("_", " ");
|
||||
filename = new Md5Hash(filename + System.nanoTime() + counter++).toHex().toString() + extension;
|
||||
return filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件大小校验
|
||||
*
|
||||
* @param file 上传的文件
|
||||
* @return
|
||||
* @throws FileSizeLimitExceededException 如果超出最大大小
|
||||
*/
|
||||
public static final void assertAllowed(MultipartFile file) throws FileSizeLimitExceededException
|
||||
{
|
||||
long size = file.getSize();
|
||||
if (DEFAULT_MAX_SIZE != -1 && size > DEFAULT_MAX_SIZE)
|
||||
{
|
||||
throw new FileSizeLimitExceededException("not allowed upload upload", size, DEFAULT_MAX_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,6 +1,9 @@
|
||||
package com.ruoyi.common.utils;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
import org.springframework.web.context.request.RequestAttributes;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
@ -12,11 +15,65 @@ import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
public class ServletUtils
|
||||
{
|
||||
/**
|
||||
* 获取request对象
|
||||
* 获取getStrAttribute
|
||||
*/
|
||||
public static HttpServletRequest getHttpServletRequest()
|
||||
public static String getStrAttribute(String name)
|
||||
{
|
||||
return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
||||
return StringUtils.valueAsStr(getRequestAttributes().getRequest().getAttribute(name));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取getIntAttribute
|
||||
*/
|
||||
public static int getIntAttribute(String name)
|
||||
{
|
||||
return StringUtils.valueAsInt(getRequestAttributes().getRequest().getAttribute(name));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取getStrParameter
|
||||
*/
|
||||
public static String getStrParameter(String name)
|
||||
{
|
||||
return StringUtils.valueAsStr(getRequestAttributes().getRequest().getParameter(name));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取getIntParameter
|
||||
*/
|
||||
public static Integer getIntParameter(String name)
|
||||
{
|
||||
return StringUtils.valueAsInt(getRequestAttributes().getRequest().getParameter(name));
|
||||
}
|
||||
|
||||
public static ServletRequestAttributes getRequestAttributes()
|
||||
{
|
||||
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
|
||||
return (ServletRequestAttributes) attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取request
|
||||
*/
|
||||
public static HttpServletRequest getRequest()
|
||||
{
|
||||
return getRequestAttributes().getRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取response
|
||||
*/
|
||||
public static HttpServletResponse getResponse()
|
||||
{
|
||||
return getRequestAttributes().getResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取session
|
||||
*/
|
||||
public static HttpSession getSession()
|
||||
{
|
||||
return getRequest().getSession();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -51,4 +108,5 @@ public class ServletUtils
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -298,4 +298,54 @@ public class StringUtils
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串数据处理
|
||||
*/
|
||||
public static String valueAsStr(Object value)
|
||||
{
|
||||
if (value instanceof String)
|
||||
{
|
||||
return (String) value;
|
||||
}
|
||||
else if (value != null)
|
||||
{
|
||||
return value.toString();
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 整型数据处理
|
||||
*/
|
||||
public static Integer valueAsInt(Object value)
|
||||
{
|
||||
if (value instanceof Integer)
|
||||
{
|
||||
return (Integer) value;
|
||||
}
|
||||
else if (value instanceof Number)
|
||||
{
|
||||
return ((Number) value).intValue();
|
||||
}
|
||||
else if (value instanceof String)
|
||||
{
|
||||
if ("NaN".equals(value))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return Integer.valueOf((String) value);
|
||||
}
|
||||
else if (value instanceof Boolean)
|
||||
{
|
||||
return ((Boolean) value) ? 1 : 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -2,13 +2,11 @@ package com.ruoyi.common.utils;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.ruoyi.common.constant.CommonConstant;
|
||||
import com.ruoyi.common.utils.security.ShiroUtils;
|
||||
import com.ruoyi.common.utils.spring.SpringUtils;
|
||||
import com.ruoyi.project.monitor.logininfor.domain.Logininfor;
|
||||
import com.ruoyi.project.monitor.logininfor.service.LogininforServiceImpl;
|
||||
|
||||
import eu.bitwalker.useragentutils.UserAgent;
|
||||
|
||||
/**
|
||||
@ -53,7 +51,7 @@ public class SystemLogUtils
|
||||
|
||||
public static void saveOpLog(String username, String message, String status)
|
||||
{
|
||||
UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getHttpServletRequest().getHeader("User-Agent"));
|
||||
UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
|
||||
// 获取客户端操作系统
|
||||
String os = userAgent.getOperatingSystem().getName();
|
||||
// 获取客户端浏览器
|
||||
|
@ -1,10 +1,10 @@
|
||||
package com.ruoyi.common.utils.security;
|
||||
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.apache.shiro.session.Session;
|
||||
import org.apache.shiro.subject.PrincipalCollection;
|
||||
import org.apache.shiro.subject.SimplePrincipalCollection;
|
||||
import org.apache.shiro.subject.Subject;
|
||||
|
||||
import com.ruoyi.project.system.user.domain.User;
|
||||
|
||||
/**
|
||||
@ -19,6 +19,11 @@ public class ShiroUtils
|
||||
{
|
||||
return SecurityUtils.getSubject();
|
||||
}
|
||||
|
||||
public static Session getSession()
|
||||
{
|
||||
return SecurityUtils.getSubject().getSession();
|
||||
}
|
||||
|
||||
public static void logout()
|
||||
{
|
||||
|
Reference in New Issue
Block a user