Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ Servers
.metadata
upload
gen_code
litemall-wx/project.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,41 @@
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
* Spring 容器工具类,用于在非 Spring 管理的类中获取 Bean 实例。
*/
@Component
public class BeanUtil implements ApplicationContextAware {
/** 保存 Spring 容器上下文 */
protected static ApplicationContext context;

/**
* 注入 Spring 容器上下文。
*
* @param applicationContext Spring 容器上下文
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}

/**
* 根据 Bean 名称获取 Bean 实例。
*
* @param name Bean 名称
* @return Bean 实例
*/
public static Object getBean(String name) {
return context.getBean(name);
}

/**
* 根据 Bean 类型获取 Bean 实例。
*
* @param c Bean 类型
* @param <T> Bean 类型泛型
* @return Bean 实例
*/
public static <T> T getBean(Class<T> c){
return context.getBean(c);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,42 @@

import java.util.Random;

/**
* 随机字符串工具类。
*/
public class CharUtil {

/**
* 生成指定长度的随机字符串(包含小写字母和数字)。
*
* @param num 随机字符串的长度
* @return 随机字符串
*/
public static String getRandomString(Integer num) {
/* 随机字符池:小写字母与数字 */
String base = "abcdefghijklmnopqrstuvwxyz0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
/* 循环从字符池中随机取 num 个字符拼接 */
for (int i = 0; i < num; i++) {
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
}
return sb.toString();
}

/**
* 生成指定长度的随机数字字符串。
*
* @param num 随机字符串的长度
* @return 随机数字字符串
*/
public static String getRandomNum(Integer num) {
/* 随机字符池:仅数字 */
String base = "0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
/* 循环从数字池中随机取 num 个字符拼接 */
for (int i = 0; i < num; i++) {
int number = random.nextInt(base.length());
sb.append(base.charAt(number));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
public class DateTimeUtil {

/**
* 格式 yyyy年MM月dd日 HH:mm:ss
* 将日期时间格式化为 "yyyy年MM月dd日 HH:mm:ss" 形式的字符串。
*
* @param dateTime
* @return
* @param dateTime 待格式化的日期时间
* @return 格式化后的日期时间字符串
*/
public static String getDateTimeDisplayString(LocalDateTime dateTime) {
DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import java.net.URL;
import java.util.Map;


/**
* 向指定 URL 发送POST方法的请求
*
* @return 远程资源的响应结果
* HTTP 工具类,用于向指定 URL 发送 POST 请求。be'a
*/

public class HttpUtil {

private static final Log logger = LogFactory.getLog(HttpUtil.class);
Expand All @@ -27,29 +27,28 @@ public class HttpUtil {
* @param params 请求的参数集合
* @return 远程资源的响应结果
*/
@SuppressWarnings("unused")
public static String sendPost(String url, Map<String, String> params) {
OutputStreamWriter out = null;
BufferedReader in = null;
StringBuilder result = new StringBuilder();
try {
URL realUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
// 发送POST请求必须设置如下两行
/*发送POST请求必须设置如下两行*/
conn.setDoOutput(true);
conn.setDoInput(true);
// POST方法
/* POST方法 */
conn.setRequestMethod("POST");
// 设置通用的请求属性
/* 设置通用的请求属性 */
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.connect();
// 获取URLConnection对象对应的输出流
/* 获取URLConnection对象对应的输出流 */
out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
// 发送请求参数
/* 发送请求参数 */
if (params != null) {
StringBuilder param = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
Expand All @@ -64,9 +63,9 @@ public static String sendPost(String url, Map<String, String> params) {

out.write(param.toString());
}
// flush输出流的缓冲
/* flush输出流的缓冲 */
out.flush();
// 定义BufferedReader输入流来读取URL的响应
/* 定义BufferedReader输入流来读取URL的响应 */
in = new BufferedReader(
new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
Expand All @@ -76,7 +75,7 @@ public static String sendPost(String url, Map<String, String> params) {
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
//使用finally块来关闭输出流、输入流
/* 使用finally块来关闭输出流、输入流 */
finally {
try {
if (out != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ public class IpUtil {

private static final Log logger = LogFactory.getLog(IpUtil.class);

/**
* 获取客户端真实 IP 地址。
* <p>
* 依次从请求头 x-forwarded-for、Proxy-Client-IP、WL-Proxy-Client-IP 中获取,
* 若都为空则回退到远程地址;经过多级代理时取第一个 IP。
*
* @param request HTTP 请求
* @return 客户端 IP 地址
*/
public static String getIpAddr(HttpServletRequest request) {
String ipAddress;
try {
Expand All @@ -27,7 +36,7 @@ public static String getIpAddr(HttpServletRequest request) {
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
if (ipAddress.equals("127.0.0.1")) {
// 根据网卡取本机配置的IP
/* 根据网卡取本机配置的IP */
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
Expand All @@ -37,9 +46,9 @@ public static String getIpAddr(HttpServletRequest request) {
ipAddress = inet.getHostAddress();
}
}
// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
// = 15
/* 通过多个代理时,第一个 IP 为客户端真实 IP,多个 IP 以逗号分隔; */
/* 单个 IP 最长 15 个字符(如 255.255.255.255),超过 15 说明包含多个 IP */
if (ipAddress != null && ipAddress.length() > 15) {
if (ipAddress.indexOf(",") > 0) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
}
Expand Down
Loading