Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
163a0f46df | ||
|
|
4625fbefed | ||
|
|
d7e971f32d | ||
|
|
7ab231a76f |
@@ -1,82 +0,0 @@
|
||||
package sc545.pay.interceptor;
|
||||
|
||||
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.*;
|
||||
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
import sc545.pay.utils.DBUtil;
|
||||
import sc545.pay.utils.ReadTxt;
|
||||
import sc545.pay.utils.Utils;
|
||||
|
||||
|
||||
|
||||
|
||||
public class testInterceptor implements HandlerInterceptor{
|
||||
|
||||
@Override
|
||||
public boolean preHandle(
|
||||
HttpServletRequest request,
|
||||
HttpServletResponse response,
|
||||
Object handler) throws Exception {
|
||||
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.setContentType("text/html;charset=utf-8");
|
||||
request.setCharacterEncoding("utf-8");
|
||||
|
||||
/*************************************拉黑高频访问ip******************************************/
|
||||
if(!"".equals(ReadTxt.getSetting(null, "拉黑ip.txt", Utils.getIpAddr(request), ""))){
|
||||
PrintWriter out = response.getWriter();
|
||||
out.print("<meta name='viewport' content='width=device-width, initial-scale=1.0'>系统检测到高频次访问已被拉黑IP<br>若误封请前往微信公众号【顺诚百宝箱】反馈<br>感谢理解!<br>IP:"+Utils.getIpAddr(request));
|
||||
return false;
|
||||
}
|
||||
String ipdate = ReadTxt.getSetting(null, "访问者IP.txt", Utils.getIpAddr(request), "");//获取此ip上次访问时间和已经访问的次数
|
||||
String[] idt = ipdate.split("_");//分隔
|
||||
long i=0;//已经访问的次数
|
||||
if(!"".equals(ipdate)){
|
||||
if(ipdate.indexOf("_")>0) i=Utils.getNum(idt[1]);
|
||||
//检查此ip第一次访问到现在过了几秒
|
||||
Date ds = Utils.string2Date(idt[0], null);
|
||||
long xc = (new Date().getTime() - ds.getTime())/1000;
|
||||
if(xc<=30){//30秒超过50次
|
||||
if(i>50){//拉黑此ip3天
|
||||
ReadTxt.writeSetting(null,"拉黑ip.txt", Utils.getIpAddr(request), Utils.date2String(new Date(), null));
|
||||
ReadTxt.delSetting(null, "访问者IP.txt", Utils.getIpAddr(request));
|
||||
}else{//正常ip
|
||||
}
|
||||
}else{//正常ip
|
||||
ReadTxt.delSetting(null, "访问者IP.txt", Utils.getIpAddr(request));
|
||||
}
|
||||
ReadTxt.writeSetting(null,"访问者IP.txt", Utils.getIpAddr(request), idt[0]+"_"+(++i));
|
||||
}else
|
||||
ReadTxt.writeSetting(null,"访问者IP.txt", Utils.getIpAddr(request), Utils.date2String(new Date(), "yyyy-MM-dd HH:mm:ss")+"_1");
|
||||
/*******************************************************************************/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
DBUtil db = new DBUtil();
|
||||
|
||||
//闭站
|
||||
String cw = ReadTxt.getSetting(null, "setting.ini", "closeweb", "0");
|
||||
if("1".equals(cw)) {
|
||||
String uri = request.getRequestURI();
|
||||
if(uri.indexOf("/admin")<0){
|
||||
Utils.outHtml(response.getWriter(), ReadTxt.getSetting(null, "setting.ini", "closewebtxt", "本站暂停访问"), "/*about:blank*/");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,10 +2,8 @@ package sc545.pay.utils;
|
||||
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
@@ -1,602 +0,0 @@
|
||||
package sc545.pay.utils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URLEncoder;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.KeyStoreException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.ParseException;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.CookieStore;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.ssl.SSLContextBuilder;
|
||||
import org.apache.http.ssl.TrustStrategy;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
|
||||
/**
|
||||
* 基于 httpclient 4.5版本的 http工具类
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class HttpUtils {
|
||||
|
||||
private static final CloseableHttpClient httpClient;
|
||||
public static final String CHARSET = "UTF-8";
|
||||
// 采用静态代码块,初始化超时时间配置,再根据配置生成默认httpClient对象
|
||||
static {
|
||||
//10秒相应超时
|
||||
RequestConfig config = RequestConfig.custom().setConnectTimeout(10000).setSocketTimeout(10000).setConnectionRequestTimeout(5000).build();
|
||||
httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
|
||||
}
|
||||
|
||||
public static String doGet(String url, Map<String, String> params) {
|
||||
return doGet(url, params, CHARSET);
|
||||
}
|
||||
|
||||
public static String doGetSSL(String url, Map<String, String> params) {
|
||||
return doGetSSL(url, params, CHARSET);
|
||||
}
|
||||
|
||||
public static String doPost(String url, Map<String, String> params){
|
||||
return doPost(url, params, CHARSET);
|
||||
}
|
||||
|
||||
/**
|
||||
* HTTP Get 获取内容
|
||||
* @param url 请求的url地址 ?之前的地址
|
||||
* @param params 请求的参数
|
||||
* @param charset 编码格式
|
||||
* @return 页面内容
|
||||
*/
|
||||
public static String doGet(String url, Map<String, String> params, String charset) {
|
||||
if(charset==null||charset.trim().length()<1) charset="UTF-8";
|
||||
if (StringUtils.isBlank(url)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
if (params != null && !params.isEmpty()) {
|
||||
List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());
|
||||
for (Map.Entry<String, String> entry : params.entrySet()) {
|
||||
String value = entry.getValue();
|
||||
if (value != null) {
|
||||
pairs.add(new BasicNameValuePair(entry.getKey(), value));
|
||||
}
|
||||
}
|
||||
// 将请求参数和url进行拼接
|
||||
url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, charset));
|
||||
}
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
CloseableHttpResponse response = httpClient.execute(httpGet);
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (statusCode != 200) {
|
||||
httpGet.abort();
|
||||
throw new RuntimeException("HttpClient,error status code :" + statusCode);
|
||||
}
|
||||
HttpEntity entity = response.getEntity();
|
||||
String result = null;
|
||||
if (entity != null) {
|
||||
result = EntityUtils.toString(entity, charset);
|
||||
}
|
||||
EntityUtils.consume(entity);
|
||||
response.close();
|
||||
if(!"UTF-8".equals(getEncoding(result))) result=GBKtoUTF8(result);
|
||||
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
System.out.println("url错误:::"+e.toString());
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* HTTP Post 获取内容
|
||||
* @param url 请求的url地址 ?之前的地址
|
||||
* @param params 请求的参数
|
||||
* @param charset 编码格式
|
||||
* @return 页面内容
|
||||
* @throws IOException
|
||||
*/
|
||||
public static String doPost(String url, Map<String, String> params, String charset) {
|
||||
if(charset==null||charset.trim().length()<1) charset="UTF-8";
|
||||
if (StringUtils.isBlank(url)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<NameValuePair> pairs = null;
|
||||
if (params != null && !params.isEmpty()) {
|
||||
pairs = new ArrayList<NameValuePair>(params.size());
|
||||
for (Map.Entry<String, String> entry : params.entrySet()) {
|
||||
String value = entry.getValue();
|
||||
if (value != null) {
|
||||
pairs.add(new BasicNameValuePair(entry.getKey(), value));
|
||||
}
|
||||
}
|
||||
}
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
CloseableHttpResponse response = null;
|
||||
try {
|
||||
if (pairs != null && pairs.size() > 0) {
|
||||
httpPost.setEntity(new UrlEncodedFormEntity(pairs, CHARSET));
|
||||
}
|
||||
|
||||
response = httpClient.execute(httpPost);
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (statusCode != 200) {
|
||||
httpPost.abort();
|
||||
throw new RuntimeException("HttpClient,error status code :" + statusCode);
|
||||
}
|
||||
HttpEntity entity = response.getEntity();
|
||||
String result = null;
|
||||
if (entity != null) {
|
||||
result = EntityUtils.toString(entity, charset);
|
||||
}
|
||||
EntityUtils.consume(entity);
|
||||
if(!"UTF-8".equals(getEncoding(result))) result=GBKtoUTF8(result);
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
return "";
|
||||
} finally {
|
||||
if (response != null)
|
||||
try {
|
||||
response.close();
|
||||
} catch (IOException e) {}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* HTTPS Get 获取内容
|
||||
* @param url 请求的url地址 ?之前的地址
|
||||
* @param params 请求的参数
|
||||
* @param charset 编码格式
|
||||
* @return 页面内容
|
||||
*/
|
||||
public static String doGetSSL(String url, Map<String, String> params, String charset) {
|
||||
if(charset==null||charset.trim().length()<1) charset="UTF-8";
|
||||
if (StringUtils.isBlank(url)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
if (params != null && !params.isEmpty()) {
|
||||
List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());
|
||||
for (Map.Entry<String, String> entry : params.entrySet()) {
|
||||
String value = entry.getValue();
|
||||
if (value != null) {
|
||||
pairs.add(new BasicNameValuePair(entry.getKey(), value));
|
||||
}
|
||||
}
|
||||
url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, charset));
|
||||
}
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
|
||||
// https 注意这里获取https内容,使用了忽略证书的方式,当然还有其他的方式来获取https内容
|
||||
CloseableHttpClient httpsClient = HttpUtils.createSSLClientDefault();
|
||||
CloseableHttpResponse response = httpsClient.execute(httpGet);
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (statusCode != 200) {
|
||||
httpGet.abort();
|
||||
throw new RuntimeException("HttpClient,error status code :" + statusCode);
|
||||
}
|
||||
HttpEntity entity = response.getEntity();
|
||||
String result = null;
|
||||
if (entity != null) {
|
||||
result = EntityUtils.toString(entity, charset);
|
||||
}
|
||||
EntityUtils.consume(entity);
|
||||
response.close();
|
||||
if(!"UTF-8".equals(getEncoding(result))) result=GBKtoUTF8(result);
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 这里创建了忽略整数验证的CloseableHttpClient对象
|
||||
* @return
|
||||
*/
|
||||
public static CloseableHttpClient createSSLClientDefault() {
|
||||
try {
|
||||
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
|
||||
// 信任所有
|
||||
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
||||
return true;
|
||||
}
|
||||
}).build();
|
||||
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
|
||||
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
|
||||
} catch (KeyManagementException e) {
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
} catch (KeyStoreException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return HttpClients.createDefault();
|
||||
}
|
||||
|
||||
/**
|
||||
* 这里创建了忽略整数验证的CloseableHttpClient对象
|
||||
* @return
|
||||
*/
|
||||
public static CloseableHttpClient createSSLClientCookie(CookieStore cookie) {
|
||||
try {
|
||||
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
|
||||
// 信任所有
|
||||
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
||||
return true;
|
||||
}
|
||||
}).build();
|
||||
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
|
||||
return HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultCookieStore(cookie).build();
|
||||
} catch (KeyManagementException e) {
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
} catch (KeyStoreException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return HttpClients.createDefault();
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送post请求
|
||||
* @param url 请求地址
|
||||
* @param json json格式字符串
|
||||
* @param contentType 这里用 "application/json"
|
||||
* @return
|
||||
*/
|
||||
public static String post(String url, String json, String contentType,String charset) {
|
||||
if(charset==null||charset.trim().length()<1) charset="UTF-8";
|
||||
if("json".equals(contentType)) contentType="application/json";
|
||||
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
|
||||
//HttpClient
|
||||
CloseableHttpClient client = httpClientBuilder.build();
|
||||
client = (CloseableHttpClient) wrapClient(client);
|
||||
HttpPost post = new HttpPost(url);
|
||||
try {
|
||||
StringEntity s = new StringEntity(json, "utf-8");
|
||||
if (StringUtils.isBlank(contentType)) {
|
||||
s.setContentType("application/json");
|
||||
}
|
||||
s.setContentType(contentType);
|
||||
post.setEntity(s);
|
||||
HttpResponse res = client.execute(post);
|
||||
HttpEntity entity = res.getEntity();
|
||||
String str = EntityUtils.toString(entity, charset);
|
||||
if(!"UTF-8".equals(getEncoding(str))) str=GBKtoUTF8(str);
|
||||
return str;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
private static org.apache.http.client.HttpClient wrapClient(HttpClient base) {
|
||||
try {
|
||||
SSLContext ctx = SSLContext.getInstance("TLSv1");
|
||||
X509TrustManager tm = new X509TrustManager() {
|
||||
public void checkClientTrusted(X509Certificate[] xcs,
|
||||
String string) throws CertificateException {
|
||||
}
|
||||
|
||||
public void checkServerTrusted(X509Certificate[] xcs,
|
||||
String string) throws CertificateException {
|
||||
}
|
||||
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
ctx.init(null, new TrustManager[]{tm}, null);
|
||||
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(ctx, new String[]{"TLSv1"}, null,
|
||||
SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
|
||||
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
|
||||
return httpclient;
|
||||
|
||||
} catch (Exception ex) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//正则找出所有
|
||||
public static String getUrl(String s){
|
||||
String regex = "http[s]?:\\/\\/([\\w]+\\.)+[\\w]+([\\w./?%&=]*)?";
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
Matcher m = pattern.matcher(s);
|
||||
String r="";
|
||||
while(m.find()){
|
||||
r+=m.group()+",";
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查url是否可访问
|
||||
* @param url
|
||||
* @return
|
||||
*/
|
||||
public static boolean ckUrl(String url) {
|
||||
|
||||
if (StringUtils.isBlank(url)) {
|
||||
return false;
|
||||
}
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
try {
|
||||
CloseableHttpResponse response = httpClient.execute(httpGet);
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (statusCode != 200) return false;
|
||||
} catch (Exception e) {
|
||||
String s = e.getMessage();
|
||||
if("Timeout waiting for connection from pool".equals(s)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**检查字符串编码格式*/
|
||||
public static String getEncoding(String str)
|
||||
{
|
||||
String encode;
|
||||
|
||||
encode = "UTF-16";
|
||||
try
|
||||
{
|
||||
if(str.equals(new String(str.getBytes(), encode)))
|
||||
{
|
||||
return encode;
|
||||
}
|
||||
}
|
||||
catch(Exception ex) {}
|
||||
|
||||
encode = "ASCII";
|
||||
try
|
||||
{
|
||||
if(str.equals(new String(str.getBytes(), encode)))
|
||||
{
|
||||
//字符串 str 中仅由数字和英文字母组成,无法识别其编码格式
|
||||
return "UTF-8";
|
||||
}
|
||||
}
|
||||
catch(Exception ex) {}
|
||||
|
||||
encode = "ISO-8859-1";
|
||||
try
|
||||
{
|
||||
if(str.equals(new String(str.getBytes(), encode)))
|
||||
{
|
||||
return encode;
|
||||
}
|
||||
}
|
||||
catch(Exception ex) {}
|
||||
|
||||
encode = "GB2312";
|
||||
try
|
||||
{
|
||||
if(str.equals(new String(str.getBytes(), encode)))
|
||||
{
|
||||
return encode;
|
||||
}
|
||||
}
|
||||
catch(Exception ex) {}
|
||||
|
||||
encode = "UTF-8";
|
||||
try
|
||||
{
|
||||
if(str.equals(new String(str.getBytes(), encode)))
|
||||
{
|
||||
return encode;
|
||||
}
|
||||
}
|
||||
catch(Exception ex) {}
|
||||
|
||||
/*
|
||||
*......待完善
|
||||
*/
|
||||
|
||||
return "UTF-8";
|
||||
}
|
||||
|
||||
/**将字符串转换字符编码*/
|
||||
public static String GBKtoUTF8(String str) {
|
||||
if("UTF-8".equals(getEncoding(str))) return str;
|
||||
|
||||
return getUTF8StringFromGBKString(str);
|
||||
|
||||
}
|
||||
|
||||
public static String getUTF8StringFromGBKString(String gbkStr) {
|
||||
try {
|
||||
return new String(getUTF8BytesFromGBKString(gbkStr), "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new InternalError();
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] getUTF8BytesFromGBKString(String gbkStr) {
|
||||
int n = gbkStr.length();
|
||||
byte[] utfBytes = new byte[3 * n];
|
||||
int k = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
int m = gbkStr.charAt(i);
|
||||
if (m < 128 && m >= 0) {
|
||||
utfBytes[k++] = (byte) m;
|
||||
continue;
|
||||
}
|
||||
utfBytes[k++] = (byte) (0xe0 | (m >> 12));
|
||||
utfBytes[k++] = (byte) (0x80 | ((m >> 6) & 0x3f));
|
||||
utfBytes[k++] = (byte) (0x80 | (m & 0x3f));
|
||||
}
|
||||
if (k < utfBytes.length) {
|
||||
byte[] tmp = new byte[k];
|
||||
System.arraycopy(utfBytes, 0, tmp, 0, k);
|
||||
return tmp;
|
||||
}
|
||||
return utfBytes;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 带有cookie和header 的post请求
|
||||
* @param url
|
||||
* @param params
|
||||
* @param cookie
|
||||
* @param header
|
||||
* @return
|
||||
*/
|
||||
public static String doPost2(String url, Map<String, String> params, CookieStore cookie,Map<String, String> header,String charset) {
|
||||
if(charset==null||charset.trim().length()<1) charset="UTF-8";
|
||||
if (StringUtils.isBlank(url)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<NameValuePair> pairs = null;
|
||||
if (params != null && !params.isEmpty()) {
|
||||
pairs = new ArrayList<NameValuePair>(params.size());
|
||||
for (Map.Entry<String, String> entry : params.entrySet()) {
|
||||
String value = entry.getValue();
|
||||
if (value != null) {
|
||||
pairs.add(new BasicNameValuePair(entry.getKey(), value));
|
||||
}
|
||||
}
|
||||
}
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
|
||||
Set<Entry<String, String>> set = header.entrySet();
|
||||
Iterator<Entry<String, String>> s = set.iterator();
|
||||
while (s.hasNext()) {
|
||||
Map.Entry m = (Map.Entry) s.next();
|
||||
httpPost.addHeader(m.getKey().toString(),m.getValue().toString());
|
||||
}
|
||||
|
||||
CloseableHttpResponse response = null;
|
||||
try {
|
||||
if (pairs != null && pairs.size() > 0) {
|
||||
httpPost.setEntity(new UrlEncodedFormEntity(pairs, CHARSET));
|
||||
}
|
||||
|
||||
CloseableHttpClient httpClientc = HttpUtils.createSSLClientCookie(cookie);
|
||||
response = httpClientc.execute(httpPost);
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (statusCode != 200) {
|
||||
httpPost.abort();
|
||||
throw new RuntimeException("HttpClient,error status code :" + statusCode);
|
||||
}
|
||||
HttpEntity entity = response.getEntity();
|
||||
String result = null;
|
||||
if (entity != null) {
|
||||
result = EntityUtils.toString(entity, charset);
|
||||
}
|
||||
EntityUtils.consume(entity);
|
||||
if(!"UTF-8".equals(getEncoding(result))) result=GBKtoUTF8(result);
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (response != null)
|
||||
try {
|
||||
response.close();
|
||||
} catch (IOException e) {e.printStackTrace();}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String doGet2(String url, Map<String, String> params, CookieStore cookie,Map<String, String> header,String charset) {
|
||||
if(charset==null||charset.trim().length()<1) charset="UTF-8";
|
||||
if (StringUtils.isBlank(url)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
if (params != null && !params.isEmpty()) {
|
||||
List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());
|
||||
for (Map.Entry<String, String> entry : params.entrySet()) {
|
||||
String value = entry.getValue();
|
||||
if (value != null) {
|
||||
pairs.add(new BasicNameValuePair(entry.getKey(), value));
|
||||
}
|
||||
}
|
||||
url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, charset));
|
||||
}
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
|
||||
Set<Entry<String, String>> set = header.entrySet();
|
||||
Iterator<Entry<String, String>> s = set.iterator();
|
||||
while (s.hasNext()) {
|
||||
Map.Entry m = (Map.Entry) s.next();
|
||||
httpGet.addHeader(m.getKey().toString(),m.getValue().toString());
|
||||
}
|
||||
|
||||
// https 注意这里获取https内容,使用了忽略证书的方式,当然还有其他的方式来获取https内容
|
||||
CloseableHttpClient httpsClient = HttpUtils.createSSLClientCookie(cookie);
|
||||
CloseableHttpResponse response = httpsClient.execute(httpGet);
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
if (statusCode != 200) {
|
||||
httpGet.abort();
|
||||
throw new RuntimeException("HttpClient,error status code :" + statusCode);
|
||||
}
|
||||
HttpEntity entity = response.getEntity();
|
||||
String result = null;
|
||||
if (entity != null) {
|
||||
result = EntityUtils.toString(entity, charset);
|
||||
}
|
||||
EntityUtils.consume(entity);
|
||||
response.close();
|
||||
if(!"UTF-8".equals(getEncoding(result))) result=GBKtoUTF8(result);
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package sc545.pay.utils;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
@@ -29,8 +29,7 @@ import org.apache.commons.lang.time.DateUtils;
|
||||
public class Utils {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String s = floatToString(0.01235788889f, 3,false);
|
||||
System.out.println(s);
|
||||
getWeekNum();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -702,12 +701,6 @@ public class Utils {
|
||||
*/
|
||||
public static long getDatePoor(Date endDate, Date nowDate) {
|
||||
|
||||
long nd = 1000 * 24 * 60 * 60;// 一天
|
||||
|
||||
long nh = 1000 * 60 * 60;// 一小时
|
||||
|
||||
long nm = 1000 * 60;// 一分钟
|
||||
|
||||
long ns = 1000;// 一秒
|
||||
|
||||
// 获得两个时间的毫秒时间差异
|
||||
@@ -718,6 +711,13 @@ public class Utils {
|
||||
return sec;
|
||||
|
||||
}
|
||||
|
||||
/**获取今天是周几,1-7*/
|
||||
public static long getWeekNum() {
|
||||
long[] weekDays = {7,1,2,3,4,5,6};
|
||||
Calendar c = Calendar.getInstance();
|
||||
return weekDays[c.get(Calendar.DAY_OF_WEEK)-1];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户真实ip
|
||||
@@ -1088,7 +1088,8 @@ public class Utils {
|
||||
DecimalFormat decimalFormat=new DecimalFormat(f);//构造方法的字符格式这里如果小数不足2位,会以0补足.
|
||||
String p=decimalFormat.format(d);//format 返回的是字符串
|
||||
if(p.split("\\.")[0].length()<1) p="0"+p;
|
||||
String nn = p.split("\\.")[1];
|
||||
String nn ="0";
|
||||
if(p.split("\\.").length>1) nn = p.split("\\.")[1];
|
||||
if(!x){
|
||||
if(nn.replaceAll("0", "").length()<1) p=p.split("\\.")[0];
|
||||
}
|
||||
|
||||
@@ -37,21 +37,4 @@
|
||||
</bean>
|
||||
|
||||
|
||||
|
||||
<!-- 配置静态资源 -->
|
||||
<mvc:resources location="/" mapping="/**/*.js" />
|
||||
<mvc:resources location="/" mapping="/**/*.css" />
|
||||
<mvc:resources location="/assets/" mapping="/assets/**/*" />
|
||||
<mvc:resources location="/images/" mapping="/images/*"
|
||||
cache-period="360000" />
|
||||
|
||||
<mvc:interceptors>
|
||||
<mvc:interceptor>
|
||||
<mvc:mapping path="/**" />
|
||||
<bean class="sc545.pay.interceptor.testInterceptor" />
|
||||
</mvc:interceptor>
|
||||
|
||||
</mvc:interceptors>
|
||||
|
||||
|
||||
</beans>
|
||||
|
||||
Reference in New Issue
Block a user