解决配置文件读取乱码方案
1.配置中的中文转为对应的ASCII码(项目要在学校部署未采用)
2.修改idea中的编码(本地运行可以,放到服务器还是乱码)
3.使用yml配置文件进行配置。Spring Boot在读取yaml配置文件时使用的是UTF-8的编码方式(习惯使用properties配置文件未采用)
4.Spring Boot注解读取application.properties文件时默认的是ISO_8859_1编码导致乱码,进行转码(项目中使用方案)
new String(signName.getBytes(“ISO_8859_1”),“UTF-8”)
阿里短信服务jar
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.1.0</version>
</dependency>
application.properties
#阿里短信配置
regionId=cn-hangzhou
accessKeyId=XXX
secret=XXX
domain=dysmsapi.aliyuncs.com
version=2017-05-25
signName=河南
#修改密码验证码
xgmm=SMS_135230176
#登录确认验证码
dlqr=SMS_135230179
读取配置文件类
package com.xhkjedu.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.UnsupportedEncodingException;
/**
* @author ywx
* @classname ConfigKey
* @description 读取配置文件类
* @date 2020/10/16 9:19
**/
@Component
public class ConfigKey {
public static String regionId;
public static String accessKeyId;
public static String secret;
public static String domain;
public static String version;
public static String signName;
public static String xgmm;
public static String dlqr;
@Value("${regionId}")
private void setRegionId(String regionId) {
ConfigKey.regionId = regionId;
}
@Value("${accessKeyId}")
private void setAccessKeyId(String accessKeyId) {
ConfigKey.accessKeyId = accessKeyId;
}
@Value("${secret}")
private void setSecret(String secret) {
ConfigKey.secret = secret;
}
@Value("${domain}")
private void setDomain(String domain) {
ConfigKey.domain = domain;
}
@Value("${version}")
private void setVersion(String version) {
ConfigKey.version = version;
}
@Value("${signName}")
private void setSignName(String signName) throws UnsupportedEncodingException {
//Spring Boot注解读取application.properties或者application-{profile}.properties文件时默认的是ISO_8859_1编码,中文会乱码
ConfigKey.signName = new String(signName.getBytes("ISO_8859_1"),"UTF-8");;
}
@Value("${xgmm}")
private void setXgmm(String xgmm) {
ConfigKey.xgmm = xgmm;
}
@Value("${dlqr}")
private void setDlqr(String dlqr) {
ConfigKey.dlqr = dlqr;
}
}
package com.xhkjedu.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.UnsupportedEncodingException;
/**
* @author ywx
* @classname ConfigKey
* @description 读取配置文件类
* 在使用类中使用@Autowired自动装配
* 使用@Autowired的当前类也必须由spring容器托管(打@Coponent、@Controller、@Service 、@repository)
* 不能使用new来初始化,否则会导致他的自动装配的子类为null
* @date 2020/10/16 9:19
**/
@Component
public class ConfigKey {
@Value("${regionId}")
public String regionId;
@Value("${accessKeyId}")
public String accessKeyId;
@Value("${secret}")
public String secret;
@Value("${domain}")
public String domain;
@Value("${version}")
public String version;
// @Value("${signName}")
public String signName;
@Value("${xgmm}")
public String xgmm;
@Value("${dlqr}")
public String dlqr;
@Value("${signName}")
public void setSignName(String signName) throws UnsupportedEncodingException {
this.signName = new String(signName.getBytes("ISO_8859_1"),"UTF-8");
}
}
调用短信接口工具类
package com.xhkjedu.utils;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.xhkjedu.config.ConfigKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author ywx
* @className SendSmsUtil
* @description
* @date 2020/5/7 17:15
**/
@Slf4j
public class SendSmsUtil {
/**
* 阿里云发送短信
* @Param [userPhone, templateCode]
* @Author ywx
* @Date 2020/5/7 17:47
* @return void
**/
public static void sendMsg(String userPhone,String templateCode,Integer messageCode) {
DefaultProfile profile = getDefaultProfile();
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = getCommonRequest();
request.putQueryParameter("PhoneNumbers", userPhone);
request.setAction("SendSms");
if ("xgmm".equals(templateCode)){//修改密码验证码
templateCode = ConfigKey.xgmm;
} else if ("dlqr".equals(templateCode)){//登录确认验证码
templateCode = ConfigKey.dlqr;
}
request.putQueryParameter("TemplateCode", templateCode);
request.putQueryParameter("TemplateParam", "{'code':"+messageCode+"}");
try {
CommonResponse response = client.getCommonResponse(request);
log.info(response.getData());
} catch (ServerException e) {
log.error(e.getMessage());
} catch (ClientException e) {
log.error(e.getMessage());
}
}
private static CommonRequest getCommonRequest() {
CommonRequest request = new CommonRequest();
request.setMethod(MethodType.POST);
request.setDomain(ConfigKey.domain);
request.setVersion(ConfigKey.version);
request.putQueryParameter("RegionId", ConfigKey.regionId);
request.putQueryParameter("SignName", ConfigKey.signName);
return request;
}
/**
* 发送记录查询
* @Param [userPhone, sendDate]
* @Author ywx
* @Date 2020/10/15 16:31
* @return java.lang.String
**/
@Autowired
public static String querySendMsg(String userPhone,String sendDate) {
DefaultProfile profile = getDefaultProfile();
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = getCommonRequest();
request.setAction("QuerySendDetails");
request.putQueryParameter("PhoneNumber", userPhone);
request.putQueryParameter("SendDate", sendDate);
request.putQueryParameter("PageSize", "10");
request.putQueryParameter("CurrentPage", "1");
String result = null;
try {
CommonResponse response = client.getCommonResponse(request);
result = response.getData();
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
return result;
}
private static DefaultProfile getDefaultProfile() {
return DefaultProfile.getProfile(ConfigKey.regionId, ConfigKey.accessKeyId, ConfigKey.secret);
}
}
package com.xhkjedu.utils;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.xhkjedu.config.ConfigKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author ywx
* @className SendSmsUtil
* @description
* @date 2020/5/7 17:15
**/
@Slf4j
public class SendSmsUtil {
/**
* 阿里云发送短信
* @Param [userPhone, templateCode]
* @Author ywx
* @Date 2020/5/7 17:47
* @return void
**/
public static void sendMsg(String userPhone,String templateCode,Integer messageCode,ConfigKey configKey) {
DefaultProfile profile = getDefaultProfile(configKey);
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = getCommonRequest(configKey);
request.putQueryParameter("PhoneNumbers", userPhone);
request.setAction("SendSms");
if ("xgmm".equals(templateCode)){//修改密码验证码
templateCode = configKey.xgmm;
} else if ("dlqr".equals(templateCode)){//登录确认验证码
templateCode = configKey.dlqr;
}
request.putQueryParameter("TemplateCode", templateCode);
request.putQueryParameter("TemplateParam", "{'code':"+messageCode+"}");
try {
CommonResponse response = client.getCommonResponse(request);
log.info(response.getData());
} catch (ServerException e) {
log.error(e.getMessage());
} catch (ClientException e) {
log.error(e.getMessage());
}
}
private static CommonRequest getCommonRequest(ConfigKey configKey) {
CommonRequest request = new CommonRequest();
request.setMethod(MethodType.POST);
request.setDomain(configKey.domain);
request.setVersion(configKey.version);
request.putQueryParameter("RegionId", configKey.regionId);
request.putQueryParameter("SignName", configKey.signName);
return request;
}
/**
* 发送记录查询
* @Param [userPhone, sendDate]
* @Author ywx
* @Date 2020/10/15 16:31
* @return java.lang.String
**/
@Autowired
public static String querySendMsg(String userPhone,String sendDate,ConfigKey configKey) {
DefaultProfile profile = getDefaultProfile(configKey);
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = getCommonRequest(configKey);
request.setAction("QuerySendDetails");
request.putQueryParameter("PhoneNumber", userPhone);
request.putQueryParameter("SendDate", sendDate);
request.putQueryParameter("PageSize", "10");
request.putQueryParameter("CurrentPage", "1");
String result = null;
try {
CommonResponse response = client.getCommonResponse(request);
result = response.getData();
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
return result;
}
private static DefaultProfile getDefaultProfile(ConfigKey configKey) {
return DefaultProfile.getProfile(configKey.regionId, configKey.accessKeyId, configKey.secret);
}
}
##测试类
package com.xhkjedu;
import com.xhkjedu.config.ConfigKey;
import com.xhkjedu.utils.SendSmsUtil;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SchoolapiApplicationTests {
@Test
void contextLoads() {
System.out.println(ConfigKey.signName);
System.out.println(SendSmsUtil.querySendMsg("1314019XXXX","20201016"));
}
}
package com.xhkjedu;
import com.xhkjedu.config.ConfigKey;
import com.xhkjedu.utils.SendSmsUtil;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SchoolapiApplicationTests {
@Autowired
private ConfigKey configKey;
@Test
void contextLoads() {
System.out.println(configKey.signName);
System.out.println(SendSmsUtil.querySendMsg("1314019XXXX","20201016",configKey));
}
}