delete 2.0
This commit is contained in:
@ -1,96 +0,0 @@
|
||||
package com.ruoyi.common.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.ruoyi.common.constant.Constants;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* 获取地址类
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class AddressUtils
|
||||
{
|
||||
private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);
|
||||
|
||||
public static final String IP_URL = "http://ip.taobao.com/service/getIpInfo.php";
|
||||
|
||||
/**
|
||||
* 获取查询结果
|
||||
*
|
||||
* @param urlStr
|
||||
* @param content
|
||||
* @param encoding
|
||||
* @return
|
||||
*/
|
||||
private static String sendPost(String content, String encoding)
|
||||
{
|
||||
URL url = null;
|
||||
HttpURLConnection connection = null;
|
||||
try
|
||||
{
|
||||
url = new URL(IP_URL);
|
||||
connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setConnectTimeout(2000);
|
||||
connection.setReadTimeout(2000);
|
||||
connection.setDoOutput(true);
|
||||
connection.setDoInput(true);
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setUseCaches(false);
|
||||
connection.connect();
|
||||
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
|
||||
out.writeBytes(content);
|
||||
out.flush();
|
||||
out.close();
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), encoding));
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
String line = "";
|
||||
while ((line = reader.readLine()) != null)
|
||||
{
|
||||
buffer.append(line);
|
||||
}
|
||||
reader.close();
|
||||
return buffer.toString();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
log.error("温馨提醒:您的主机已经断网,请您检查主机的网络连接");
|
||||
log.error("根据IP获取所在位置----------错误消息:" + e.getMessage());
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (connection != null)
|
||||
{
|
||||
connection.disconnect();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getRealAddressByIP(String ip)
|
||||
{
|
||||
String address = "";
|
||||
try
|
||||
{
|
||||
address = sendPost("ip=" + ip, Constants.UTF8);
|
||||
|
||||
JSONObject json = JSONObject.parseObject(address);
|
||||
JSONObject object = json.getObject("data", JSONObject.class);
|
||||
String region = object.getString("region");
|
||||
String city = object.getString("city");
|
||||
address = region + " " + city;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.error("根据IP获取所在位置----------错误消息:" + e.getMessage());
|
||||
}
|
||||
return address;
|
||||
}
|
||||
}
|
@ -1,100 +0,0 @@
|
||||
package com.ruoyi.common.utils;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import org.apache.commons.lang3.time.DateFormatUtils;
|
||||
|
||||
/**
|
||||
* 时间工具类
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class DateUtils
|
||||
{
|
||||
public static String YYYY = "yyyy";
|
||||
|
||||
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";
|
||||
|
||||
/**
|
||||
* 获取当前Date型日期
|
||||
*
|
||||
* @return Date() 当前日期
|
||||
*/
|
||||
public static Date getNowDate()
|
||||
{
|
||||
return new Date();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前日期, 默认格式为yyyy-MM-dd
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public static String getDate()
|
||||
{
|
||||
return dateTimeNow(YYYY_MM_DD);
|
||||
}
|
||||
|
||||
public static final String getTime()
|
||||
{
|
||||
return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
|
||||
}
|
||||
|
||||
public static final String dateTimeNow()
|
||||
{
|
||||
return dateTimeNow(YYYYMMDDHHMMSS);
|
||||
}
|
||||
|
||||
public static final String dateTimeNow(final String format)
|
||||
{
|
||||
return parseDateToStr(format, new Date());
|
||||
}
|
||||
|
||||
public static final String dateTime(final Date date)
|
||||
{
|
||||
return parseDateToStr(YYYY_MM_DD, date);
|
||||
}
|
||||
|
||||
public static final String parseDateToStr(final String format, final Date date)
|
||||
{
|
||||
return new SimpleDateFormat(format).format(date);
|
||||
}
|
||||
|
||||
public static final Date dateTime(final String format, final String ts)
|
||||
{
|
||||
try
|
||||
{
|
||||
return new SimpleDateFormat(format).parse(ts);
|
||||
}
|
||||
catch (ParseException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 日期路径 即年/月/日 如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");
|
||||
}
|
||||
|
||||
}
|
@ -1,155 +0,0 @@
|
||||
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,43 +0,0 @@
|
||||
package com.ruoyi.common.utils;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* 获取IP方法
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class IpUtils
|
||||
{
|
||||
public static String getIpAddr(HttpServletRequest request)
|
||||
{
|
||||
if (request == null)
|
||||
{
|
||||
return "unknown";
|
||||
}
|
||||
String ip = request.getHeader("x-forwarded-for");
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
|
||||
{
|
||||
ip = request.getHeader("Proxy-Client-IP");
|
||||
}
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
|
||||
{
|
||||
ip = request.getHeader("X-Forwarded-For");
|
||||
}
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
|
||||
{
|
||||
ip = request.getHeader("WL-Proxy-Client-IP");
|
||||
}
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
|
||||
{
|
||||
ip = request.getHeader("X-Real-IP");
|
||||
}
|
||||
|
||||
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
|
||||
{
|
||||
ip = request.getRemoteAddr();
|
||||
}
|
||||
|
||||
return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
|
||||
}
|
||||
}
|
@ -1,136 +0,0 @@
|
||||
package com.ruoyi.common.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 处理并记录日志文件
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class LogUtils
|
||||
{
|
||||
|
||||
public static final Logger ERROR_LOG = LoggerFactory.getLogger("sys-error");
|
||||
public static final Logger ACCESS_LOG = LoggerFactory.getLogger("sys-access");
|
||||
|
||||
/**
|
||||
* 记录访问日志 [username][jsessionid][ip][accept][UserAgent][url][params][Referer]
|
||||
*
|
||||
* @param request
|
||||
*/
|
||||
public static void logAccess(HttpServletRequest request)
|
||||
{
|
||||
String username = getUsername();
|
||||
String jsessionId = request.getRequestedSessionId();
|
||||
String ip = IpUtils.getIpAddr(request);
|
||||
String accept = request.getHeader("accept");
|
||||
String userAgent = request.getHeader("User-Agent");
|
||||
String url = request.getRequestURI();
|
||||
String params = getParams(request);
|
||||
|
||||
StringBuilder s = new StringBuilder();
|
||||
s.append(getBlock(username));
|
||||
s.append(getBlock(jsessionId));
|
||||
s.append(getBlock(ip));
|
||||
s.append(getBlock(accept));
|
||||
s.append(getBlock(userAgent));
|
||||
s.append(getBlock(url));
|
||||
s.append(getBlock(params));
|
||||
s.append(getBlock(request.getHeader("Referer")));
|
||||
getAccessLog().info(s.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录异常错误 格式 [exception]
|
||||
*
|
||||
* @param message
|
||||
* @param e
|
||||
*/
|
||||
public static void logError(String message, Throwable e)
|
||||
{
|
||||
String username = getUsername();
|
||||
StringBuilder s = new StringBuilder();
|
||||
s.append(getBlock("exception"));
|
||||
s.append(getBlock(username));
|
||||
s.append(getBlock(message));
|
||||
ERROR_LOG.error(s.toString(), e);
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录页面错误 错误日志记录 [page/eception][username][statusCode][errorMessage][servletName][uri][exceptionName][ip][exception]
|
||||
*
|
||||
* @param request
|
||||
*/
|
||||
public static void logPageError(HttpServletRequest request)
|
||||
{
|
||||
String username = getUsername();
|
||||
|
||||
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
|
||||
String message = (String) request.getAttribute("javax.servlet.error.message");
|
||||
String uri = (String) request.getAttribute("javax.servlet.error.request_uri");
|
||||
Throwable t = (Throwable) request.getAttribute("javax.servlet.error.exception");
|
||||
|
||||
if (statusCode == null)
|
||||
{
|
||||
statusCode = 0;
|
||||
}
|
||||
|
||||
StringBuilder s = new StringBuilder();
|
||||
s.append(getBlock(t == null ? "page" : "exception"));
|
||||
s.append(getBlock(username));
|
||||
s.append(getBlock(statusCode));
|
||||
s.append(getBlock(message));
|
||||
s.append(getBlock(IpUtils.getIpAddr(request)));
|
||||
|
||||
s.append(getBlock(uri));
|
||||
s.append(getBlock(request.getHeader("Referer")));
|
||||
StringWriter sw = new StringWriter();
|
||||
|
||||
while (t != null)
|
||||
{
|
||||
t.printStackTrace(new PrintWriter(sw));
|
||||
t = t.getCause();
|
||||
}
|
||||
s.append(getBlock(sw.toString()));
|
||||
getErrorLog().error(s.toString());
|
||||
|
||||
}
|
||||
|
||||
public static String getBlock(Object msg)
|
||||
{
|
||||
if (msg == null)
|
||||
{
|
||||
msg = "";
|
||||
}
|
||||
return "[" + msg.toString() + "]";
|
||||
}
|
||||
|
||||
protected static String getParams(HttpServletRequest request)
|
||||
{
|
||||
Map<String, String[]> params = request.getParameterMap();
|
||||
return JSON.toJSONString(params);
|
||||
}
|
||||
|
||||
protected static String getUsername()
|
||||
{
|
||||
return (String) SecurityUtils.getSubject().getPrincipal();
|
||||
}
|
||||
|
||||
public static Logger getAccessLog()
|
||||
{
|
||||
return ACCESS_LOG;
|
||||
}
|
||||
|
||||
public static Logger getErrorLog()
|
||||
{
|
||||
return ERROR_LOG;
|
||||
}
|
||||
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
package com.ruoyi.common.utils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* Map通用处理方法
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class MapDataUtil
|
||||
{
|
||||
public static Map<String, Object> convertDataMap(HttpServletRequest request)
|
||||
{
|
||||
Map<String, String[]> properties = request.getParameterMap();
|
||||
Map<String, Object> returnMap = new HashMap<String, Object>();
|
||||
Iterator<?> entries = properties.entrySet().iterator();
|
||||
Map.Entry<?, ?> entry;
|
||||
String name = "";
|
||||
String value = "";
|
||||
while (entries.hasNext())
|
||||
{
|
||||
entry = (Entry<?, ?>) entries.next();
|
||||
name = (String) entry.getKey();
|
||||
Object valueObj = entry.getValue();
|
||||
if (null == valueObj)
|
||||
{
|
||||
value = "";
|
||||
}
|
||||
else if (valueObj instanceof String[])
|
||||
{
|
||||
String[] values = (String[]) valueObj;
|
||||
for (int i = 0; i < values.length; i++)
|
||||
{
|
||||
value = values[i] + ",";
|
||||
}
|
||||
value = value.substring(0, value.length() - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
value = valueObj.toString();
|
||||
}
|
||||
returnMap.put(name, value);
|
||||
}
|
||||
return returnMap;
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package com.ruoyi.common.utils;
|
||||
|
||||
import org.springframework.context.MessageSource;
|
||||
import com.ruoyi.common.utils.spring.SpringUtils;
|
||||
|
||||
/**
|
||||
* 获取i18n资源文件
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class MessageUtils
|
||||
{
|
||||
|
||||
/**
|
||||
* 根据消息键和参数 获取消息 委托给spring messageSource
|
||||
*
|
||||
* @param code 消息键
|
||||
* @param args 参数
|
||||
* @return
|
||||
*/
|
||||
public static String message(String code, Object... args)
|
||||
{
|
||||
MessageSource messageSource = SpringUtils.getBean(MessageSource.class);
|
||||
return messageSource.getMessage(code, args, null);
|
||||
}
|
||||
|
||||
}
|
@ -1,137 +0,0 @@
|
||||
package com.ruoyi.common.utils;
|
||||
|
||||
import java.io.IOException;
|
||||
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;
|
||||
import com.ruoyi.common.support.Convert;
|
||||
|
||||
/**
|
||||
* 客户端工具类
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class ServletUtils
|
||||
{
|
||||
/**
|
||||
* 获取String参数
|
||||
*/
|
||||
public static String getParameter(String name)
|
||||
{
|
||||
return getRequest().getParameter(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取String参数
|
||||
*/
|
||||
public static String getParameter(String name, String defaultValue)
|
||||
{
|
||||
return Convert.toStr(getRequest().getParameter(name), defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Integer参数
|
||||
*/
|
||||
public static Integer getParameterToInt(String name)
|
||||
{
|
||||
return Convert.toInt(getRequest().getParameter(name));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Integer参数
|
||||
*/
|
||||
public static Integer getParameterToInt(String name, Integer defaultValue)
|
||||
{
|
||||
return Convert.toInt(getRequest().getParameter(name), defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取request
|
||||
*/
|
||||
public static HttpServletRequest getRequest()
|
||||
{
|
||||
return getRequestAttributes().getRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取response
|
||||
*/
|
||||
public static HttpServletResponse getResponse()
|
||||
{
|
||||
return getRequestAttributes().getResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取session
|
||||
*/
|
||||
public static HttpSession getSession()
|
||||
{
|
||||
return getRequest().getSession();
|
||||
}
|
||||
|
||||
public static ServletRequestAttributes getRequestAttributes()
|
||||
{
|
||||
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
|
||||
return (ServletRequestAttributes) attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串渲染到客户端
|
||||
*
|
||||
* @param response 渲染对象
|
||||
* @param string 待渲染的字符串
|
||||
* @return null
|
||||
*/
|
||||
public static String renderString(HttpServletResponse response, String string)
|
||||
{
|
||||
try
|
||||
{
|
||||
response.setContentType("application/json");
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.getWriter().print(string);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是Ajax异步请求
|
||||
*
|
||||
* @param request
|
||||
*/
|
||||
public static boolean isAjaxRequest(HttpServletRequest request)
|
||||
{
|
||||
|
||||
String accept = request.getHeader("accept");
|
||||
if (accept != null && accept.indexOf("application/json") != -1)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
String xRequestedWith = request.getHeader("X-Requested-With");
|
||||
if (xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
String uri = request.getRequestURI();
|
||||
if (StringUtils.inStringIgnoreCase(uri, ".json", ".xml"))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
String ajax = request.getParameter("__ajax");
|
||||
if (StringUtils.inStringIgnoreCase(ajax, "json", "xml"))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
@ -1,373 +0,0 @@
|
||||
package com.ruoyi.common.utils;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.lang.text.StrBuilder;
|
||||
|
||||
import com.ruoyi.common.support.StrFormatter;
|
||||
|
||||
/**
|
||||
* 字符串工具类
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class StringUtils
|
||||
{
|
||||
/** 空字符串 */
|
||||
private static final String NULLSTR = "";
|
||||
|
||||
/** 下划线 */
|
||||
private static final char SEPARATOR = '_';
|
||||
|
||||
/**
|
||||
* 获取参数不为空值
|
||||
*
|
||||
* @param value defaultValue 要判断的value
|
||||
* @return value 返回值
|
||||
*/
|
||||
public static <T> T nvl(T value, T defaultValue)
|
||||
{
|
||||
return value != null ? value : defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* * 判断一个Collection是否为空, 包含List,Set,Queue
|
||||
*
|
||||
* @param coll 要判断的Collection
|
||||
* @return true:为空 false:非空
|
||||
*/
|
||||
public static boolean isEmpty(Collection<?> coll)
|
||||
{
|
||||
return isNull(coll) || coll.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* * 判断一个Collection是否非空,包含List,Set,Queue
|
||||
*
|
||||
* @param coll 要判断的Collection
|
||||
* @return true:非空 false:空
|
||||
*/
|
||||
public static boolean isNotEmpty(Collection<?> coll)
|
||||
{
|
||||
return !isEmpty(coll);
|
||||
}
|
||||
|
||||
/**
|
||||
* * 判断一个对象数组是否为空
|
||||
*
|
||||
* @param objects 要判断的对象数组
|
||||
** @return true:为空 false:非空
|
||||
*/
|
||||
public static boolean isEmpty(Object[] objects)
|
||||
{
|
||||
return isNull(objects) || (objects.length == 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* * 判断一个对象数组是否非空
|
||||
*
|
||||
* @param objects 要判断的对象数组
|
||||
* @return true:非空 false:空
|
||||
*/
|
||||
public static boolean isNotEmpty(Object[] objects)
|
||||
{
|
||||
return !isEmpty(objects);
|
||||
}
|
||||
|
||||
/**
|
||||
* * 判断一个Map是否为空
|
||||
*
|
||||
* @param map 要判断的Map
|
||||
* @return true:为空 false:非空
|
||||
*/
|
||||
public static boolean isEmpty(Map<?, ?> map)
|
||||
{
|
||||
return isNull(map) || map.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* * 判断一个Map是否为空
|
||||
*
|
||||
* @param map 要判断的Map
|
||||
* @return true:非空 false:空
|
||||
*/
|
||||
public static boolean isNotEmpty(Map<?, ?> map)
|
||||
{
|
||||
return !isEmpty(map);
|
||||
}
|
||||
|
||||
/**
|
||||
* * 判断一个字符串是否为空串
|
||||
*
|
||||
* @param str String
|
||||
* @return true:为空 false:非空
|
||||
*/
|
||||
public static boolean isEmpty(String str)
|
||||
{
|
||||
return isNull(str) || NULLSTR.equals(str.trim());
|
||||
}
|
||||
|
||||
/**
|
||||
* * 判断一个字符串是否为非空串
|
||||
*
|
||||
* @param str String
|
||||
* @return true:非空串 false:空串
|
||||
*/
|
||||
public static boolean isNotEmpty(String str)
|
||||
{
|
||||
return !isEmpty(str);
|
||||
}
|
||||
|
||||
/**
|
||||
* * 判断一个对象是否为空
|
||||
*
|
||||
* @param object Object
|
||||
* @return true:为空 false:非空
|
||||
*/
|
||||
public static boolean isNull(Object object)
|
||||
{
|
||||
return object == null;
|
||||
}
|
||||
|
||||
/**
|
||||
* * 判断一个对象是否非空
|
||||
*
|
||||
* @param object Object
|
||||
* @return true:非空 false:空
|
||||
*/
|
||||
public static boolean isNotNull(Object object)
|
||||
{
|
||||
return !isNull(object);
|
||||
}
|
||||
|
||||
/**
|
||||
* * 判断一个对象是否是数组类型(Java基本型别的数组)
|
||||
*
|
||||
* @param object 对象
|
||||
* @return true:是数组 false:不是数组
|
||||
*/
|
||||
public static boolean isArray(Object object)
|
||||
{
|
||||
return isNotNull(object) && object.getClass().isArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 去空格
|
||||
*/
|
||||
public static String trim(String str)
|
||||
{
|
||||
return (str == null ? "" : str.trim());
|
||||
}
|
||||
|
||||
/**
|
||||
* 截取字符串
|
||||
*
|
||||
* @param str 字符串
|
||||
* @param start 开始
|
||||
* @return 结果
|
||||
*/
|
||||
public static String substring(final String str, int start)
|
||||
{
|
||||
if (str == null)
|
||||
{
|
||||
return NULLSTR;
|
||||
}
|
||||
|
||||
if (start < 0)
|
||||
{
|
||||
start = str.length() + start;
|
||||
}
|
||||
|
||||
if (start < 0)
|
||||
{
|
||||
start = 0;
|
||||
}
|
||||
if (start > str.length())
|
||||
{
|
||||
return NULLSTR;
|
||||
}
|
||||
|
||||
return str.substring(start);
|
||||
}
|
||||
|
||||
/**
|
||||
* 截取字符串
|
||||
*
|
||||
* @param str 字符串
|
||||
* @param start 开始
|
||||
* @param end 结束
|
||||
* @return 结果
|
||||
*/
|
||||
public static String substring(final String str, int start, int end)
|
||||
{
|
||||
if (str == null)
|
||||
{
|
||||
return NULLSTR;
|
||||
}
|
||||
|
||||
if (end < 0)
|
||||
{
|
||||
end = str.length() + end;
|
||||
}
|
||||
if (start < 0)
|
||||
{
|
||||
start = str.length() + start;
|
||||
}
|
||||
|
||||
if (end > str.length())
|
||||
{
|
||||
end = str.length();
|
||||
}
|
||||
|
||||
if (start > end)
|
||||
{
|
||||
return NULLSTR;
|
||||
}
|
||||
|
||||
if (start < 0)
|
||||
{
|
||||
start = 0;
|
||||
}
|
||||
if (end < 0)
|
||||
{
|
||||
end = 0;
|
||||
}
|
||||
|
||||
return str.substring(start, end);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化文本, {} 表示占位符<br>
|
||||
* 此方法只是简单将占位符 {} 按照顺序替换为参数<br>
|
||||
* 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可<br>
|
||||
* 例:<br>
|
||||
* 通常使用:format("this is {} for {}", "a", "b") -> this is a for b<br>
|
||||
* 转义{}: format("this is \\{} for {}", "a", "b") -> this is \{} for a<br>
|
||||
* 转义\: format("this is \\\\{} for {}", "a", "b") -> this is \a for b<br>
|
||||
*
|
||||
* @param template 文本模板,被替换的部分用 {} 表示
|
||||
* @param params 参数值
|
||||
* @return 格式化后的文本
|
||||
*/
|
||||
public static String format(String template, Object... params)
|
||||
{
|
||||
if (isEmpty(params) || isEmpty(template))
|
||||
{
|
||||
return template;
|
||||
}
|
||||
return StrFormatter.format(template, params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 驼峰首字符小写
|
||||
*/
|
||||
public static String uncapitalize(String str)
|
||||
{
|
||||
int strLen;
|
||||
if (str == null || (strLen = str.length()) == 0)
|
||||
{
|
||||
return str;
|
||||
}
|
||||
return new StrBuilder(strLen).append(Character.toLowerCase(str.charAt(0))).append(str.substring(1)).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 下划线转驼峰命名
|
||||
*/
|
||||
public static String toUnderScoreCase(String s)
|
||||
{
|
||||
if (s == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
boolean upperCase = false;
|
||||
for (int i = 0; i < s.length(); i++)
|
||||
{
|
||||
char c = s.charAt(i);
|
||||
|
||||
boolean nextUpperCase = true;
|
||||
|
||||
if (i < (s.length() - 1))
|
||||
{
|
||||
nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
|
||||
}
|
||||
|
||||
if ((i > 0) && Character.isUpperCase(c))
|
||||
{
|
||||
if (!upperCase || !nextUpperCase)
|
||||
{
|
||||
sb.append(SEPARATOR);
|
||||
}
|
||||
upperCase = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
upperCase = false;
|
||||
}
|
||||
|
||||
sb.append(Character.toLowerCase(c));
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否包含字符串
|
||||
*
|
||||
* @param str 验证字符串
|
||||
* @param strs 字符串组
|
||||
* @return 包含返回true
|
||||
*/
|
||||
public static boolean inStringIgnoreCase(String str, String... strs)
|
||||
{
|
||||
if (str != null && strs != null)
|
||||
{
|
||||
for (String s : strs)
|
||||
{
|
||||
if (str.equalsIgnoreCase(trim(s)))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。 例如:HELLO_WORLD->HelloWorld
|
||||
*
|
||||
* @param name 转换前的下划线大写方式命名的字符串
|
||||
* @return 转换后的驼峰式命名的字符串
|
||||
*/
|
||||
public static String convertToCamelCase(String name)
|
||||
{
|
||||
StringBuilder result = new StringBuilder();
|
||||
// 快速检查
|
||||
if (name == null || name.isEmpty())
|
||||
{
|
||||
// 没必要转换
|
||||
return "";
|
||||
}
|
||||
else if (!name.contains("_"))
|
||||
{
|
||||
// 不含下划线,仅将首字母大写
|
||||
return name.substring(0, 1).toUpperCase() + name.substring(1);
|
||||
}
|
||||
// 用下划线将原始字符串分割
|
||||
String[] camels = name.split("_");
|
||||
for (String camel : camels)
|
||||
{
|
||||
// 跳过原始字符串中开头、结尾的下换线或双重下划线
|
||||
if (camel.isEmpty())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
// 首字母大写
|
||||
result.append(camel.substring(0, 1).toUpperCase());
|
||||
result.append(camel.substring(1).toLowerCase());
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
}
|
@ -1,71 +0,0 @@
|
||||
package com.ruoyi.common.utils;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import com.ruoyi.common.constant.Constants;
|
||||
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;
|
||||
|
||||
/**
|
||||
* 记录用户日志信息
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class SystemLogUtils
|
||||
{
|
||||
|
||||
private static final Logger sys_user_logger = LoggerFactory.getLogger("sys-user");
|
||||
|
||||
/**
|
||||
* 记录格式 [ip][用户名][操作][错误消息]
|
||||
* <p/>
|
||||
* 注意操作如下: loginError 登录失败 loginSuccess 登录成功 passwordError 密码错误 changePassword 修改密码 changeStatus 修改状态
|
||||
*
|
||||
* @param username
|
||||
* @param op
|
||||
* @param msg
|
||||
* @param args
|
||||
*/
|
||||
public static void log(String username, String status, String msg, Object... args)
|
||||
{
|
||||
StringBuilder s = new StringBuilder();
|
||||
s.append(LogUtils.getBlock(ShiroUtils.getIp()));
|
||||
s.append(AddressUtils.getRealAddressByIP(ShiroUtils.getIp()));
|
||||
s.append(LogUtils.getBlock(username));
|
||||
s.append(LogUtils.getBlock(status));
|
||||
s.append(LogUtils.getBlock(msg));
|
||||
|
||||
sys_user_logger.info(s.toString(), args);
|
||||
|
||||
if (Constants.LOGIN_SUCCESS.equals(status) || Constants.LOGOUT.equals(status))
|
||||
{
|
||||
saveOpLog(username, msg, Constants.SUCCESS);
|
||||
}
|
||||
else if (Constants.LOGIN_FAIL.equals(status))
|
||||
{
|
||||
saveOpLog(username, msg, Constants.FAIL);
|
||||
}
|
||||
}
|
||||
|
||||
public static void saveOpLog(String username, String message, String status)
|
||||
{
|
||||
UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
|
||||
// 获取客户端操作系统
|
||||
String os = userAgent.getOperatingSystem().getName();
|
||||
// 获取客户端浏览器
|
||||
String browser = userAgent.getBrowser().getName();
|
||||
LogininforServiceImpl logininforService = SpringUtils.getBean(LogininforServiceImpl.class);
|
||||
Logininfor logininfor = new Logininfor();
|
||||
logininfor.setLoginName(username);
|
||||
logininfor.setStatus(status);
|
||||
logininfor.setIpaddr(ShiroUtils.getIp());
|
||||
logininfor.setLoginLocation(AddressUtils.getRealAddressByIP(ShiroUtils.getIp()));
|
||||
logininfor.setBrowser(browser);
|
||||
logininfor.setOs(os);
|
||||
logininfor.setMsg(message);
|
||||
logininforService.insertLogininfor(logininfor);
|
||||
}
|
||||
}
|
@ -1,145 +0,0 @@
|
||||
package com.ruoyi.common.utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import com.ruoyi.project.system.menu.domain.Menu;
|
||||
|
||||
/**
|
||||
* 权限数据处理
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class TreeUtils
|
||||
{
|
||||
|
||||
/**
|
||||
* 根据父节点的ID获取所有子节点
|
||||
*
|
||||
* @param list 分类表
|
||||
* @param typeId 传入的父节点ID
|
||||
* @return String
|
||||
*/
|
||||
public static List<Menu> getChildPerms(List<Menu> list, int parentId)
|
||||
{
|
||||
List<Menu> returnList = new ArrayList<Menu>();
|
||||
for (Iterator<Menu> iterator = list.iterator(); iterator.hasNext();)
|
||||
{
|
||||
Menu t = (Menu) iterator.next();
|
||||
// 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
|
||||
if (t.getParentId() == parentId)
|
||||
{
|
||||
recursionFn(list, t);
|
||||
returnList.add(t);
|
||||
}
|
||||
}
|
||||
return returnList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归列表
|
||||
*
|
||||
* @param list
|
||||
* @param Menu
|
||||
*/
|
||||
private static void recursionFn(List<Menu> list, Menu t)
|
||||
{
|
||||
// 得到子节点列表
|
||||
List<Menu> childList = getChildList(list, t);
|
||||
t.setChildren(childList);
|
||||
for (Menu tChild : childList)
|
||||
{
|
||||
if (hasChild(list, tChild))
|
||||
{
|
||||
// 判断是否有子节点
|
||||
Iterator<Menu> it = childList.iterator();
|
||||
while (it.hasNext())
|
||||
{
|
||||
Menu n = (Menu) it.next();
|
||||
recursionFn(list, n);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 得到子节点列表
|
||||
*/
|
||||
private static List<Menu> getChildList(List<Menu> list, Menu t)
|
||||
{
|
||||
|
||||
List<Menu> tlist = new ArrayList<Menu>();
|
||||
Iterator<Menu> it = list.iterator();
|
||||
while (it.hasNext())
|
||||
{
|
||||
Menu n = (Menu) it.next();
|
||||
if (n.getParentId().longValue() == t.getMenuId().longValue())
|
||||
{
|
||||
tlist.add(n);
|
||||
}
|
||||
}
|
||||
return tlist;
|
||||
}
|
||||
|
||||
List<Menu> returnList = new ArrayList<Menu>();
|
||||
|
||||
/**
|
||||
* 根据父节点的ID获取所有子节点
|
||||
*
|
||||
* @param list 分类表
|
||||
* @param typeId 传入的父节点ID
|
||||
* @param prefix 子节点前缀
|
||||
*/
|
||||
public List<Menu> getChildPerms(List<Menu> list, int typeId, String prefix)
|
||||
{
|
||||
if (list == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
for (Iterator<Menu> iterator = list.iterator(); iterator.hasNext();)
|
||||
{
|
||||
Menu node = (Menu) iterator.next();
|
||||
// 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
|
||||
if (node.getParentId() == typeId)
|
||||
{
|
||||
recursionFn(list, node, prefix);
|
||||
}
|
||||
// 二、遍历所有的父节点下的所有子节点
|
||||
/*
|
||||
* if (node.getParentId()==0) { recursionFn(list, node); }
|
||||
*/
|
||||
}
|
||||
return returnList;
|
||||
}
|
||||
|
||||
private void recursionFn(List<Menu> list, Menu node, String p)
|
||||
{
|
||||
// 得到子节点列表
|
||||
List<Menu> childList = getChildList(list, node);
|
||||
if (hasChild(list, node))
|
||||
{
|
||||
// 判断是否有子节点
|
||||
returnList.add(node);
|
||||
Iterator<Menu> it = childList.iterator();
|
||||
while (it.hasNext())
|
||||
{
|
||||
Menu n = (Menu) it.next();
|
||||
n.setMenuName(p + n.getMenuName());
|
||||
recursionFn(list, n, p + p);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
returnList.add(node);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否有子节点
|
||||
*/
|
||||
private static boolean hasChild(List<Menu> list, Menu t)
|
||||
{
|
||||
return getChildList(list, t).size() > 0 ? true : false;
|
||||
}
|
||||
}
|
@ -1,409 +0,0 @@
|
||||
package com.ruoyi.common.utils.poi;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.lang.reflect.Field;
|
||||
import java.math.BigDecimal;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import org.apache.poi.hssf.usermodel.DVConstraint;
|
||||
import org.apache.poi.hssf.usermodel.HSSFCell;
|
||||
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
|
||||
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
|
||||
import org.apache.poi.hssf.usermodel.HSSFFont;
|
||||
import org.apache.poi.hssf.usermodel.HSSFRow;
|
||||
import org.apache.poi.hssf.usermodel.HSSFSheet;
|
||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||
import org.apache.poi.hssf.util.HSSFColor;
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.CellStyle;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.apache.poi.ss.usermodel.WorkbookFactory;
|
||||
import org.apache.poi.ss.util.CellRangeAddressList;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.util.ResourceUtils;
|
||||
import com.ruoyi.framework.aspectj.lang.annotation.Excel;
|
||||
import com.ruoyi.framework.shiro.web.session.OnlineWebSessionManager;
|
||||
import com.ruoyi.framework.web.domain.AjaxResult;
|
||||
|
||||
/**
|
||||
* Excel相关处理
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class ExcelUtil<T>
|
||||
{
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(OnlineWebSessionManager.class);
|
||||
|
||||
public Class<T> clazz;
|
||||
|
||||
public ExcelUtil(Class<T> clazz)
|
||||
{
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
public List<T> importExcel(String sheetName, InputStream input) throws Exception
|
||||
{
|
||||
List<T> list = new ArrayList<T>();
|
||||
|
||||
Workbook workbook = WorkbookFactory.create(input);
|
||||
Sheet sheet = workbook.getSheet(sheetName);
|
||||
if (!sheetName.trim().equals(""))
|
||||
{
|
||||
sheet = workbook.getSheet(sheetName); // 如果指定sheet名,则取指定sheet中的内容.
|
||||
}
|
||||
if (sheet == null)
|
||||
{
|
||||
sheet = workbook.getSheetAt(0); // 如果传入的sheet名不存在则默认指向第1个sheet.
|
||||
}
|
||||
int rows = sheet.getPhysicalNumberOfRows();
|
||||
|
||||
if (rows > 0)
|
||||
{
|
||||
// 有数据时才处理
|
||||
Field[] allFields = clazz.getDeclaredFields(); // 得到类的所有field.
|
||||
Map<Integer, Field> fieldsMap = new HashMap<Integer, Field>(); // 定义一个map用于存放列的序号和field.
|
||||
for (Field field : allFields)
|
||||
{
|
||||
// 将有注解的field存放到map中.
|
||||
if (field.isAnnotationPresent(Excel.class))
|
||||
{
|
||||
Excel attr = field.getAnnotation(Excel.class);
|
||||
int col = getExcelCol(attr.column());// 获得列号
|
||||
field.setAccessible(true);// 设置类的私有字段属性可访问.
|
||||
fieldsMap.put(col, field);
|
||||
}
|
||||
}
|
||||
for (int i = 1; i < rows; i++)
|
||||
{
|
||||
// 从第2行开始取数据,默认第一行是表头.
|
||||
Row row = sheet.getRow(i);
|
||||
int cellNum = sheet.getRow(0).getPhysicalNumberOfCells();
|
||||
T entity = null;
|
||||
for (int j = 0; j < cellNum; j++)
|
||||
{
|
||||
Cell cell = row.getCell(j);
|
||||
if (cell == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 先设置Cell的类型,然后就可以把纯数字作为String类型读进来了 by zhuyangyong 20171228
|
||||
row.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
|
||||
cell = row.getCell(j);
|
||||
}
|
||||
|
||||
String c = cell.getStringCellValue();
|
||||
if (c.equals(""))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
entity = (entity == null ? clazz.newInstance() : entity);// 如果不存在实例则新建.
|
||||
Field field = fieldsMap.get(j);// 从map中得到对应列的field.
|
||||
// 取得类型,并根据对象类型设置值.
|
||||
Class<?> fieldType = field.getType();
|
||||
if (String.class == fieldType)
|
||||
{
|
||||
field.set(entity, String.valueOf(c));
|
||||
}
|
||||
else if ((Integer.TYPE == fieldType) || (Integer.class == fieldType))
|
||||
{
|
||||
field.set(entity, Integer.parseInt(c));
|
||||
}
|
||||
else if ((Long.TYPE == fieldType) || (Long.class == fieldType))
|
||||
{
|
||||
field.set(entity, Long.valueOf(c));
|
||||
}
|
||||
else if ((Float.TYPE == fieldType) || (Float.class == fieldType))
|
||||
{
|
||||
field.set(entity, Float.valueOf(c));
|
||||
}
|
||||
else if ((Short.TYPE == fieldType) || (Short.class == fieldType))
|
||||
{
|
||||
field.set(entity, Short.valueOf(c));
|
||||
}
|
||||
else if ((Double.TYPE == fieldType) || (Double.class == fieldType))
|
||||
{
|
||||
field.set(entity, Double.valueOf(c));
|
||||
}
|
||||
else if (Character.TYPE == fieldType)
|
||||
{
|
||||
if ((c != null) && (c.length() > 0))
|
||||
{
|
||||
field.set(entity, Character.valueOf(c.charAt(0)));
|
||||
}
|
||||
}
|
||||
else if (java.util.Date.class == fieldType)
|
||||
{
|
||||
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
|
||||
{
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
cell.setCellValue(sdf.format(cell.getNumericCellValue()));
|
||||
c = sdf.format(cell.getNumericCellValue());
|
||||
}
|
||||
else
|
||||
{
|
||||
c = cell.getStringCellValue();
|
||||
}
|
||||
}
|
||||
else if (java.math.BigDecimal.class == fieldType)
|
||||
{
|
||||
c = cell.getStringCellValue();
|
||||
}
|
||||
}
|
||||
if (entity != null)
|
||||
{
|
||||
list.add(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* 对list数据源将其里面的数据导入到excel表单
|
||||
*
|
||||
* @param sheetName 工作表的名称
|
||||
*/
|
||||
public AjaxResult exportExcel(List<T> list, String sheetName)
|
||||
{
|
||||
Field[] allFields = clazz.getDeclaredFields();// 得到所有定义字段
|
||||
List<Field> fields = new ArrayList<Field>();
|
||||
// 得到所有field并存放到一个list中.
|
||||
for (Field field : allFields)
|
||||
{
|
||||
if (field.isAnnotationPresent(Excel.class))
|
||||
{
|
||||
fields.add(field);
|
||||
}
|
||||
}
|
||||
|
||||
HSSFWorkbook workbook = new HSSFWorkbook();// 产生工作薄对象
|
||||
// excel2003中每个sheet中最多有65536行
|
||||
int sheetSize = 65536;
|
||||
double sheetNo = Math.ceil(list.size() / sheetSize);// 取出一共有多少个sheet.
|
||||
for (int index = 0; index <= sheetNo; index++)
|
||||
{
|
||||
HSSFSheet sheet = workbook.createSheet();// 产生工作表对象
|
||||
if (sheetNo == 0)
|
||||
{
|
||||
workbook.setSheetName(index, sheetName);
|
||||
}
|
||||
else
|
||||
{
|
||||
workbook.setSheetName(index, sheetName + index);// 设置工作表的名称.
|
||||
}
|
||||
HSSFRow row;
|
||||
HSSFCell cell; // 产生单元格
|
||||
|
||||
row = sheet.createRow(0); // 产生一行
|
||||
// 写入各个字段的列头名称
|
||||
for (int i = 0; i < fields.size(); i++)
|
||||
{
|
||||
Field field = fields.get(i);
|
||||
Excel attr = field.getAnnotation(Excel.class);
|
||||
int col = getExcelCol(attr.column()); // 获得列号
|
||||
cell = row.createCell(col); // 创建列
|
||||
cell.setCellType(HSSFCell.CELL_TYPE_STRING); // 设置列中写入内容为String类型
|
||||
HSSFCellStyle cellStyle = workbook.createCellStyle();
|
||||
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
|
||||
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
|
||||
if (attr.name().indexOf("注:") >= 0)
|
||||
{
|
||||
HSSFFont font = workbook.createFont();
|
||||
font.setColor(HSSFFont.COLOR_RED);
|
||||
cellStyle.setFont(font);
|
||||
cellStyle.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
|
||||
sheet.setColumnWidth(i, 6000);
|
||||
}
|
||||
else
|
||||
{
|
||||
HSSFFont font = workbook.createFont();
|
||||
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 粗体显示
|
||||
cellStyle.setFont(font); // 选择需要用到的字体格式
|
||||
cellStyle.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
|
||||
// 设置列宽
|
||||
sheet.setColumnWidth(i, 3766);
|
||||
}
|
||||
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
|
||||
cellStyle.setWrapText(true);
|
||||
cell.setCellStyle(cellStyle);
|
||||
|
||||
cell.setCellValue(attr.name());// 写入列名
|
||||
|
||||
// 如果设置了提示信息则鼠标放上去提示.
|
||||
if (!attr.prompt().trim().equals(""))
|
||||
{
|
||||
setHSSFPrompt(sheet, "", attr.prompt(), 1, 100, col, col); // 这里默认设了2-101列提示.
|
||||
}
|
||||
// 如果设置了combo属性则本列只能选择不能输入
|
||||
if (attr.combo().length > 0)
|
||||
{
|
||||
setHSSFValidation(sheet, attr.combo(), 1, 100, col, col); // 这里默认设了2-101列只能选择不能输入.
|
||||
}
|
||||
}
|
||||
|
||||
int startNo = index * sheetSize;
|
||||
int endNo = Math.min(startNo + sheetSize, list.size());
|
||||
// 写入各条记录,每条记录对应excel表中的一行
|
||||
HSSFCellStyle cs = workbook.createCellStyle();
|
||||
cs.setAlignment(HSSFCellStyle.ALIGN_CENTER);
|
||||
cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
|
||||
for (int i = startNo; i < endNo; i++)
|
||||
{
|
||||
row = sheet.createRow(i + 1 - startNo);
|
||||
T vo = (T) list.get(i); // 得到导出对象.
|
||||
for (int j = 0; j < fields.size(); j++)
|
||||
{
|
||||
Field field = fields.get(j); // 获得field.
|
||||
field.setAccessible(true); // 设置实体类私有属性可访问
|
||||
Excel attr = field.getAnnotation(Excel.class);
|
||||
try
|
||||
{
|
||||
// 根据Excel中设置情况决定是否导出,有些情况需要保持为空,希望用户填写这一列.
|
||||
if (attr.isExport())
|
||||
{
|
||||
cell = row.createCell(getExcelCol(attr.column()));// 创建cell
|
||||
cell.setCellStyle(cs);
|
||||
try
|
||||
{
|
||||
if (String.valueOf(field.get(vo)).length() > 10)
|
||||
throw new Exception("长度超过10位就不用转数字了");
|
||||
// 如果可以转成数字则导出为数字类型
|
||||
BigDecimal bc = new BigDecimal(String.valueOf(field.get(vo)));
|
||||
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
|
||||
cell.setCellValue(bc.doubleValue());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
|
||||
if (vo == null)
|
||||
{
|
||||
cell.setCellValue(""); // 如果数据存在就填入,不存在填入空格.
|
||||
}
|
||||
else
|
||||
{
|
||||
cell.setCellValue(field.get(vo) == null ? "" : String.valueOf(field.get(vo)));// 如果数据存在就填入,不存在填入空格.
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.error("导出Excel失败{}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
try
|
||||
{
|
||||
String filename = encodingFilename(sheetName);
|
||||
OutputStream out = new FileOutputStream(getfile() + filename);
|
||||
workbook.write(out);
|
||||
out.close();
|
||||
return AjaxResult.success(filename);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.error("关闭flush失败{}", e);
|
||||
return AjaxResult.error("导出Excel失败,请联系网站管理员!");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将EXCEL中A,B,C,D,E列映射成0,1,2,3
|
||||
*
|
||||
* @param col
|
||||
*/
|
||||
public static int getExcelCol(String col)
|
||||
{
|
||||
col = col.toUpperCase();
|
||||
// 从-1开始计算,字母重1开始运算。这种总数下来算数正好相同。
|
||||
int count = -1;
|
||||
char[] cs = col.toCharArray();
|
||||
for (int i = 0; i < cs.length; i++)
|
||||
{
|
||||
count += (cs[i] - 64) * Math.pow(26, cs.length - 1 - i);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置单元格上提示
|
||||
*
|
||||
* @param sheet 要设置的sheet.
|
||||
* @param promptTitle 标题
|
||||
* @param promptContent 内容
|
||||
* @param firstRow 开始行
|
||||
* @param endRow 结束行
|
||||
* @param firstCol 开始列
|
||||
* @param endCol 结束列
|
||||
* @return 设置好的sheet.
|
||||
*/
|
||||
public static HSSFSheet setHSSFPrompt(HSSFSheet sheet, String promptTitle, String promptContent, int firstRow,
|
||||
int endRow, int firstCol, int endCol)
|
||||
{
|
||||
// 构造constraint对象
|
||||
DVConstraint constraint = DVConstraint.createCustomFormulaConstraint("DD1");
|
||||
// 四个参数分别是:起始行、终止行、起始列、终止列
|
||||
CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
|
||||
// 数据有效性对象
|
||||
HSSFDataValidation data_validation_view = new HSSFDataValidation(regions, constraint);
|
||||
data_validation_view.createPromptBox(promptTitle, promptContent);
|
||||
sheet.addValidationData(data_validation_view);
|
||||
return sheet;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置某些列的值只能输入预制的数据,显示下拉框.
|
||||
*
|
||||
* @param sheet 要设置的sheet.
|
||||
* @param textlist 下拉框显示的内容
|
||||
* @param firstRow 开始行
|
||||
* @param endRow 结束行
|
||||
* @param firstCol 开始列
|
||||
* @param endCol 结束列
|
||||
* @return 设置好的sheet.
|
||||
*/
|
||||
public static HSSFSheet setHSSFValidation(HSSFSheet sheet, String[] textlist, int firstRow, int endRow,
|
||||
int firstCol, int endCol)
|
||||
{
|
||||
// 加载下拉列表内容
|
||||
DVConstraint constraint = DVConstraint.createExplicitListConstraint(textlist);
|
||||
// 设置数据有效性加载在哪个单元格上,四个参数分别是:起始行、终止行、起始列、终止列
|
||||
CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
|
||||
// 数据有效性对象
|
||||
HSSFDataValidation data_validation_list = new HSSFDataValidation(regions, constraint);
|
||||
sheet.addValidationData(data_validation_list);
|
||||
return sheet;
|
||||
}
|
||||
|
||||
/**
|
||||
* 编码文件名
|
||||
*/
|
||||
public String encodingFilename(String filename)
|
||||
{
|
||||
filename = UUID.randomUUID().toString() + "_" + filename + ".xls";
|
||||
return filename;
|
||||
}
|
||||
|
||||
public String getfile() throws FileNotFoundException
|
||||
{
|
||||
return ResourceUtils.getURL("classpath:").getPath() + "static/file/";
|
||||
}
|
||||
|
||||
}
|
@ -1,76 +0,0 @@
|
||||
package com.ruoyi.common.utils.security;
|
||||
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.apache.shiro.mgt.RealmSecurityManager;
|
||||
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.framework.shiro.realm.UserRealm;
|
||||
import com.ruoyi.project.system.user.domain.User;
|
||||
|
||||
/**
|
||||
* shiro 工具类
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class ShiroUtils
|
||||
{
|
||||
|
||||
public static Subject getSubjct()
|
||||
{
|
||||
return SecurityUtils.getSubject();
|
||||
}
|
||||
|
||||
public static Session getSession()
|
||||
{
|
||||
return SecurityUtils.getSubject().getSession();
|
||||
}
|
||||
|
||||
public static void logout()
|
||||
{
|
||||
getSubjct().logout();
|
||||
}
|
||||
|
||||
public static User getUser()
|
||||
{
|
||||
return (User) getSubjct().getPrincipal();
|
||||
}
|
||||
|
||||
public static void setUser(User user)
|
||||
{
|
||||
Subject subject = getSubjct();
|
||||
PrincipalCollection principalCollection = subject.getPrincipals();
|
||||
String realmName = principalCollection.getRealmNames().iterator().next();
|
||||
PrincipalCollection newPrincipalCollection = new SimplePrincipalCollection(user, realmName);
|
||||
// 重新加载Principal
|
||||
subject.runAs(newPrincipalCollection);
|
||||
}
|
||||
|
||||
public static void clearCachedAuthorizationInfo()
|
||||
{
|
||||
RealmSecurityManager rsm = (RealmSecurityManager) SecurityUtils.getSecurityManager();
|
||||
UserRealm realm = (UserRealm) rsm.getRealms().iterator().next();
|
||||
realm.clearCachedAuthorizationInfo();
|
||||
}
|
||||
|
||||
public static Long getUserId()
|
||||
{
|
||||
return getUser().getUserId().longValue();
|
||||
}
|
||||
|
||||
public static String getLoginName()
|
||||
{
|
||||
return getUser().getLoginName();
|
||||
}
|
||||
|
||||
public static String getIp()
|
||||
{
|
||||
return getSubjct().getSession().getHost();
|
||||
}
|
||||
|
||||
public static String getSessionId()
|
||||
{
|
||||
return String.valueOf(getSubjct().getSession().getId());
|
||||
}
|
||||
}
|
@ -1,102 +0,0 @@
|
||||
package com.ruoyi.common.utils.spring;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* spring工具类 方便在非spring管理环境中获取bean
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Component
|
||||
public final class SpringUtils implements BeanFactoryPostProcessor
|
||||
{
|
||||
/** Spring应用上下文环境 */
|
||||
private static ConfigurableListableBeanFactory beanFactory;
|
||||
|
||||
@Override
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
|
||||
{
|
||||
SpringUtils.beanFactory = beanFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取对象
|
||||
*
|
||||
* @param name
|
||||
* @return Object 一个以所给名字注册的bean的实例
|
||||
* @throws org.springframework.beans.BeansException
|
||||
*
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T getBean(String name) throws BeansException
|
||||
{
|
||||
return (T) beanFactory.getBean(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取类型为requiredType的对象
|
||||
*
|
||||
* @param clz
|
||||
* @return
|
||||
* @throws org.springframework.beans.BeansException
|
||||
*
|
||||
*/
|
||||
public static <T> T getBean(Class<T> clz) throws BeansException
|
||||
{
|
||||
T result = (T) beanFactory.getBean(clz);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
|
||||
*
|
||||
* @param name
|
||||
* @return boolean
|
||||
*/
|
||||
public static boolean containsBean(String name)
|
||||
{
|
||||
return beanFactory.containsBean(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
|
||||
*
|
||||
* @param name
|
||||
* @return boolean
|
||||
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
|
||||
*
|
||||
*/
|
||||
public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException
|
||||
{
|
||||
return beanFactory.isSingleton(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name
|
||||
* @return Class 注册对象的类型
|
||||
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
|
||||
*
|
||||
*/
|
||||
public static Class<?> getType(String name) throws NoSuchBeanDefinitionException
|
||||
{
|
||||
return beanFactory.getType(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果给定的bean名字在bean定义中有别名,则返回这些别名
|
||||
*
|
||||
* @param name
|
||||
* @return
|
||||
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
|
||||
*
|
||||
*/
|
||||
public static String[] getAliases(String name) throws NoSuchBeanDefinitionException
|
||||
{
|
||||
return beanFactory.getAliases(name);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user