Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9f6314c9e9 | ||
|
|
27f941768a | ||
|
|
63ae9e7433 | ||
|
|
2d242988e1 | ||
|
|
c976264581 | ||
|
|
b7c068e1ae | ||
|
|
b43a94f006 | ||
|
|
163a0f46df | ||
|
|
4625fbefed |
@@ -5,7 +5,7 @@
|
|||||||
<meta charset='UTF-8'>
|
<meta charset='UTF-8'>
|
||||||
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
|
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
|
||||||
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
|
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
|
||||||
<title>顺诚百宝箱</title>
|
<title>支付测试- 顺诚百宝箱</title>
|
||||||
<style>
|
<style>
|
||||||
* {
|
* {
|
||||||
margin: 10px auto;
|
margin: 10px auto;
|
||||||
@@ -39,7 +39,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
let response = await fetch('/pay/createOrder?n=' + n + '&t=test');
|
let response = await fetch('/pay/createOrder?n=' + n + '&t=test');
|
||||||
let res = eval("(" + await response.text() + ")");
|
let res = JSON.parse(await response.text());
|
||||||
|
|
||||||
if (res.code == 1) {
|
if (res.code == 1) {
|
||||||
|
|
||||||
@@ -54,7 +54,7 @@
|
|||||||
alert('获取二维码失败');
|
alert('获取二维码失败');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$('.imgs').html("<img src='data:image/jpg;base64," + img + "'><br><a href='" + code + "'>手机点这里打开支付宝APP</a><br>使用支付宝扫一扫<br><button onclick='selectPay(" + id + ")'>查询支付状态</button> <button onclick='getPayQr()'>刷新二维码</button>");
|
$('.imgs').html("<img src='data:image/jpg;base64," + img + "'><br><a href='alipays://platformapi/startapp?saId=10000007&qrcode=" + code + "'>手机点这里打开支付宝APP</a><br>使用支付宝扫一扫<br><button onclick='selectPay(" + id + ")'>查询支付状态</button> <button onclick='getPayQr()'>刷新二维码</button>");
|
||||||
subscribe(id);
|
subscribe(id);
|
||||||
|
|
||||||
} else alert('遇到错误 ' + res.msg);
|
} else alert('遇到错误 ' + res.msg);
|
||||||
@@ -77,7 +77,7 @@
|
|||||||
await subscribe(id);
|
await subscribe(id);
|
||||||
} else {
|
} else {
|
||||||
// 获取并显示消息
|
// 获取并显示消息
|
||||||
let message = eval("(" + await response.text() + ")");
|
let message = JSON.parse(await response.text());
|
||||||
|
|
||||||
// 再次调用 subscribe() 以获取下一条消息
|
// 再次调用 subscribe() 以获取下一条消息
|
||||||
if (message.msg == "已支付") {
|
if (message.msg == "已支付") {
|
||||||
@@ -94,7 +94,7 @@
|
|||||||
*/
|
*/
|
||||||
async function selectPay(id) {
|
async function selectPay(id) {
|
||||||
let response = await fetch('/pay/queryPay?id=' + id);
|
let response = await fetch('/pay/queryPay?id=' + id);
|
||||||
let res = eval("(" + await response.text() + ")");
|
let res = JSON.parse(await response.text());
|
||||||
|
|
||||||
if (response.status == 200 && res.msg == "已支付") {
|
if (response.status == 200 && res.msg == "已支付") {
|
||||||
return okPay();
|
return okPay();
|
||||||
|
|||||||
@@ -359,7 +359,7 @@ public static String putImg(ServletContext servletContext,MultipartFile file,Str
|
|||||||
saveSql(realurl, towhere,fileName);
|
saveSql(realurl, towhere,fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(servletContext != null) return "/getFile/"+rid;
|
if(servletContext != null) return "/getImg/"+rid;
|
||||||
else return realurl;
|
else return realurl;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -452,7 +452,7 @@ public static String putImgAsName(ServletContext servletContext,MultipartFile fi
|
|||||||
saveSql(realurl, towhere,fileName);
|
saveSql(realurl, towhere,fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(servletContext != null) return "/getFile/"+rid;
|
if(servletContext != null) return "/getImg/"+rid;
|
||||||
else return realurl;
|
else return realurl;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -548,7 +548,7 @@ public static String putVideo(ServletContext servletContext,MultipartFile file,S
|
|||||||
saveSql(realurl, towhere,fileName);
|
saveSql(realurl, towhere,fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(servletContext != null) return "/getFile/"+rid;
|
if(servletContext != null) return "/getImg/"+rid;
|
||||||
else return realurl;
|
else return realurl;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -669,8 +669,8 @@ public static void saveSql(String realurl, String towhere, String saveName) {
|
|||||||
String msid = towhere.split("#")[1];
|
String msid = towhere.split("#")[1];
|
||||||
String mstag = towhere.split("#")[2];
|
String mstag = towhere.split("#")[2];
|
||||||
|
|
||||||
if(realurl.indexOf("/getFile/")==0) {
|
if(realurl.indexOf("/getImg/")==0) {
|
||||||
realurl=realurl.replace("/getFile/", "");
|
realurl=realurl.replace("/getImg/", "");
|
||||||
if(db.execSql("select * from savefile where id = '"+realurl+"'", null)>0){//已存在
|
if(db.execSql("select * from savefile where id = '"+realurl+"'", null)>0){//已存在
|
||||||
db.execUpdate("update savefile set mstab=?,msid=?,mstag=?,savename=? where id=?", new String[]{mstab.trim(),msid.trim(),mstag.trim(),realurl.trim(),saveName==null?"":saveName.trim()});
|
db.execUpdate("update savefile set mstab=?,msid=?,mstag=?,savename=? where id=?", new String[]{mstab.trim(),msid.trim(),mstag.trim(),realurl.trim(),saveName==null?"":saveName.trim()});
|
||||||
}else{//不存在
|
}else{//不存在
|
||||||
@@ -780,8 +780,8 @@ public static void delfile(ServletContext servletContext, String path) {
|
|||||||
if(path.indexOf(":") <= 0)
|
if(path.indexOf(":") <= 0)
|
||||||
path1 = "D:\\" + path;
|
path1 = "D:\\" + path;
|
||||||
} else {
|
} else {
|
||||||
if(path.indexOf("/getFile/")==0){
|
if(path.indexOf("/getImg/")==0){
|
||||||
String rid = path.replace("/getFile/", "");
|
String rid = path.replace("/getImg/", "");
|
||||||
DBUtil db = new DBUtil();
|
DBUtil db = new DBUtil();
|
||||||
List<Map<String, Object>> rs = db.execQuery("select * from savefile where id ="+rid, null);
|
List<Map<String, Object>> rs = db.execQuery("select * from savefile where id ="+rid, null);
|
||||||
if(rs!=null&&rs.size()>0){
|
if(rs!=null&&rs.size()>0){
|
||||||
@@ -828,8 +828,8 @@ public static void delFileForStr(ServletContext servletContext, String str) {
|
|||||||
if(path.indexOf(":") <= 0)
|
if(path.indexOf(":") <= 0)
|
||||||
path1 = "D:\\" + path;
|
path1 = "D:\\" + path;
|
||||||
} else {
|
} else {
|
||||||
if(path.indexOf("/getFile/")==0){
|
if(path.indexOf("/getImg/")==0){
|
||||||
String rid = path.replace("/getFile/", "");
|
String rid = path.replace("/getImg/", "");
|
||||||
List<Map<String, Object>> rs = db.execQuery("select * from savefile where id ="+rid, null);
|
List<Map<String, Object>> rs = db.execQuery("select * from savefile where id ="+rid, null);
|
||||||
if(rs!=null&&rs.size()>0){
|
if(rs!=null&&rs.size()>0){
|
||||||
oldpath=rs.get(0).get("realurl")+"";
|
oldpath=rs.get(0).get("realurl")+"";
|
||||||
@@ -867,8 +867,8 @@ public static void intoSqlForStr(String str,String towhere,String saveName) {
|
|||||||
mstag = towhere.split("#")[2];
|
mstag = towhere.split("#")[2];
|
||||||
|
|
||||||
for (String path : imgs) {
|
for (String path : imgs) {
|
||||||
if(path.indexOf("/getFile/")==0){
|
if(path.indexOf("/getImg/")==0){
|
||||||
String rid = path.replace("/getFile/", "");
|
String rid = path.replace("/getImg/", "");
|
||||||
List<Map<String, Object>> rs = db.execQuery("select * from savefile where id ="+rid, null);
|
List<Map<String, Object>> rs = db.execQuery("select * from savefile where id ="+rid, null);
|
||||||
if(rs!=null&&rs.size()>0){
|
if(rs!=null&&rs.size()>0){
|
||||||
path=rs.get(0).get("realurl")+"";
|
path=rs.get(0).get("realurl")+"";
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ import javax.net.ssl.X509TrustManager;
|
|||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpEntity;
|
||||||
import org.apache.http.HttpResponse;
|
|
||||||
import org.apache.http.NameValuePair;
|
import org.apache.http.NameValuePair;
|
||||||
import org.apache.http.client.CookieStore;
|
import org.apache.http.client.CookieStore;
|
||||||
import org.apache.http.client.HttpClient;
|
import org.apache.http.client.HttpClient;
|
||||||
@@ -49,14 +48,7 @@ import org.apache.http.util.EntityUtils;
|
|||||||
*/
|
*/
|
||||||
public class HttpUtils {
|
public class HttpUtils {
|
||||||
|
|
||||||
private static final CloseableHttpClient httpClient;
|
|
||||||
public static final String CHARSET = "UTF-8";
|
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) {
|
public static String doGet(String url, Map<String, String> params) {
|
||||||
return doGet(url, params, CHARSET);
|
return doGet(url, params, CHARSET);
|
||||||
@@ -82,7 +74,10 @@ public class HttpUtils {
|
|||||||
if (StringUtils.isBlank(url)) {
|
if (StringUtils.isBlank(url)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
CloseableHttpResponse response = null;
|
||||||
|
HttpGet httpGet = null;
|
||||||
|
RequestConfig config = RequestConfig.custom().setConnectTimeout(10000).setSocketTimeout(10000).setConnectionRequestTimeout(5000).build();
|
||||||
|
CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
|
||||||
try {
|
try {
|
||||||
if (params != null && !params.isEmpty()) {
|
if (params != null && !params.isEmpty()) {
|
||||||
List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());
|
List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());
|
||||||
@@ -95,8 +90,8 @@ public class HttpUtils {
|
|||||||
// 将请求参数和url进行拼接
|
// 将请求参数和url进行拼接
|
||||||
url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, charset));
|
url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, charset));
|
||||||
}
|
}
|
||||||
HttpGet httpGet = new HttpGet(url);
|
httpGet = new HttpGet(url);
|
||||||
CloseableHttpResponse response = httpClient.execute(httpGet);
|
response = httpClient.execute(httpGet);
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
if (statusCode != 200) {
|
if (statusCode != 200) {
|
||||||
httpGet.abort();
|
httpGet.abort();
|
||||||
@@ -114,7 +109,20 @@ public class HttpUtils {
|
|||||||
return result;
|
return result;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println("url错误:::"+e.toString());
|
System.out.println("url错误:::"+e.toString());
|
||||||
|
try {
|
||||||
|
response.close();
|
||||||
|
} catch (IOException e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
}finally{
|
||||||
|
try {
|
||||||
|
response.close();
|
||||||
|
httpClient.close();
|
||||||
|
httpGet.reset();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -146,6 +154,8 @@ public class HttpUtils {
|
|||||||
}
|
}
|
||||||
HttpPost httpPost = new HttpPost(url);
|
HttpPost httpPost = new HttpPost(url);
|
||||||
CloseableHttpResponse response = null;
|
CloseableHttpResponse response = null;
|
||||||
|
RequestConfig config = RequestConfig.custom().setConnectTimeout(10000).setSocketTimeout(10000).setConnectionRequestTimeout(5000).build();
|
||||||
|
CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
|
||||||
try {
|
try {
|
||||||
if (pairs != null && pairs.size() > 0) {
|
if (pairs != null && pairs.size() > 0) {
|
||||||
httpPost.setEntity(new UrlEncodedFormEntity(pairs, CHARSET));
|
httpPost.setEntity(new UrlEncodedFormEntity(pairs, CHARSET));
|
||||||
@@ -165,14 +175,19 @@ public class HttpUtils {
|
|||||||
EntityUtils.consume(entity);
|
EntityUtils.consume(entity);
|
||||||
if(!"UTF-8".equals(getEncoding(result))) result=GBKtoUTF8(result);
|
if(!"UTF-8".equals(getEncoding(result))) result=GBKtoUTF8(result);
|
||||||
return result;
|
return result;
|
||||||
} catch (Exception e) {
|
}catch (Exception e) {
|
||||||
return "";
|
System.out.println("url错误:::"+e.toString());
|
||||||
} finally {
|
e.printStackTrace();
|
||||||
if (response != null)
|
}finally{
|
||||||
try {
|
try {
|
||||||
response.close();
|
response.close();
|
||||||
} catch (IOException e) {}
|
httpClient.close();
|
||||||
|
httpPost.reset();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -187,7 +202,9 @@ public class HttpUtils {
|
|||||||
if (StringUtils.isBlank(url)) {
|
if (StringUtils.isBlank(url)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
CloseableHttpResponse response = null;
|
||||||
|
HttpGet httpGet = null;
|
||||||
|
CloseableHttpClient httpsClient = null;
|
||||||
try {
|
try {
|
||||||
if (params != null && !params.isEmpty()) {
|
if (params != null && !params.isEmpty()) {
|
||||||
List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());
|
List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());
|
||||||
@@ -199,11 +216,11 @@ public class HttpUtils {
|
|||||||
}
|
}
|
||||||
url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, charset));
|
url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, charset));
|
||||||
}
|
}
|
||||||
HttpGet httpGet = new HttpGet(url);
|
httpGet = new HttpGet(url);
|
||||||
|
|
||||||
// https 注意这里获取https内容,使用了忽略证书的方式,当然还有其他的方式来获取https内容
|
// https 注意这里获取https内容,使用了忽略证书的方式,当然还有其他的方式来获取https内容
|
||||||
CloseableHttpClient httpsClient = HttpUtils.createSSLClientDefault();
|
httpsClient = HttpUtils.createSSLClientDefault();
|
||||||
CloseableHttpResponse response = httpsClient.execute(httpGet);
|
response = httpsClient.execute(httpGet);
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
if (statusCode != 200) {
|
if (statusCode != 200) {
|
||||||
httpGet.abort();
|
httpGet.abort();
|
||||||
@@ -218,8 +235,17 @@ public class HttpUtils {
|
|||||||
response.close();
|
response.close();
|
||||||
if(!"UTF-8".equals(getEncoding(result))) result=GBKtoUTF8(result);
|
if(!"UTF-8".equals(getEncoding(result))) result=GBKtoUTF8(result);
|
||||||
return result;
|
return result;
|
||||||
} catch (Exception e) {
|
}catch (Exception e) {
|
||||||
|
System.out.println("url错误:::"+e.toString());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
}finally{
|
||||||
|
try {
|
||||||
|
response.close();
|
||||||
|
httpsClient.close();
|
||||||
|
httpGet.reset();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -287,6 +313,7 @@ public class HttpUtils {
|
|||||||
CloseableHttpClient client = httpClientBuilder.build();
|
CloseableHttpClient client = httpClientBuilder.build();
|
||||||
client = (CloseableHttpClient) wrapClient(client);
|
client = (CloseableHttpClient) wrapClient(client);
|
||||||
HttpPost post = new HttpPost(url);
|
HttpPost post = new HttpPost(url);
|
||||||
|
CloseableHttpResponse res = null;
|
||||||
try {
|
try {
|
||||||
StringEntity s = new StringEntity(json, "utf-8");
|
StringEntity s = new StringEntity(json, "utf-8");
|
||||||
if (StringUtils.isBlank(contentType)) {
|
if (StringUtils.isBlank(contentType)) {
|
||||||
@@ -294,14 +321,23 @@ public class HttpUtils {
|
|||||||
}
|
}
|
||||||
s.setContentType(contentType);
|
s.setContentType(contentType);
|
||||||
post.setEntity(s);
|
post.setEntity(s);
|
||||||
HttpResponse res = client.execute(post);
|
res = client.execute(post);
|
||||||
HttpEntity entity = res.getEntity();
|
HttpEntity entity = res.getEntity();
|
||||||
String str = EntityUtils.toString(entity, charset);
|
String str = EntityUtils.toString(entity, charset);
|
||||||
if(!"UTF-8".equals(getEncoding(str))) str=GBKtoUTF8(str);
|
if(!"UTF-8".equals(getEncoding(str))) str=GBKtoUTF8(str);
|
||||||
return str;
|
return str;
|
||||||
} catch (Exception e) {
|
}catch (Exception e) {
|
||||||
e.printStackTrace();
|
System.out.println("url错误:::"+e.toString());
|
||||||
}
|
e.printStackTrace();
|
||||||
|
}finally{
|
||||||
|
try {
|
||||||
|
res.close();
|
||||||
|
client.close();
|
||||||
|
post.reset();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -358,6 +394,8 @@ public class HttpUtils {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
HttpGet httpGet = new HttpGet(url);
|
HttpGet httpGet = new HttpGet(url);
|
||||||
|
RequestConfig config = RequestConfig.custom().setConnectTimeout(10000).setSocketTimeout(10000).setConnectionRequestTimeout(5000).build();
|
||||||
|
CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
|
||||||
try {
|
try {
|
||||||
CloseableHttpResponse response = httpClient.execute(httpGet);
|
CloseableHttpResponse response = httpClient.execute(httpGet);
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
@@ -508,12 +546,13 @@ public class HttpUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
CloseableHttpResponse response = null;
|
CloseableHttpResponse response = null;
|
||||||
|
CloseableHttpClient httpClientc = null;
|
||||||
try {
|
try {
|
||||||
if (pairs != null && pairs.size() > 0) {
|
if (pairs != null && pairs.size() > 0) {
|
||||||
httpPost.setEntity(new UrlEncodedFormEntity(pairs, CHARSET));
|
httpPost.setEntity(new UrlEncodedFormEntity(pairs, CHARSET));
|
||||||
}
|
}
|
||||||
|
|
||||||
CloseableHttpClient httpClientc = HttpUtils.createSSLClientCookie(cookie);
|
httpClientc = HttpUtils.createSSLClientCookie(cookie);
|
||||||
response = httpClientc.execute(httpPost);
|
response = httpClientc.execute(httpPost);
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
if (statusCode != 200) {
|
if (statusCode != 200) {
|
||||||
@@ -529,16 +568,28 @@ public class HttpUtils {
|
|||||||
if(!"UTF-8".equals(getEncoding(result))) result=GBKtoUTF8(result);
|
if(!"UTF-8".equals(getEncoding(result))) result=GBKtoUTF8(result);
|
||||||
return result;
|
return result;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
System.out.println("url错误:::"+e.toString());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
}finally{
|
||||||
if (response != null)
|
try {
|
||||||
try {
|
response.close();
|
||||||
response.close();
|
httpClientc.close();
|
||||||
} catch (IOException e) {e.printStackTrace();}
|
httpPost.reset();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 带有cookie和header 的get请求
|
||||||
|
* @param url
|
||||||
|
* @param params
|
||||||
|
* @param cookie
|
||||||
|
* @param header
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
@SuppressWarnings("rawtypes")
|
@SuppressWarnings("rawtypes")
|
||||||
public static String doGet2(String url, Map<String, String> params, CookieStore cookie,Map<String, String> header,String charset) {
|
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(charset==null||charset.trim().length()<1) charset="UTF-8";
|
||||||
@@ -546,6 +597,9 @@ public class HttpUtils {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
HttpGet httpGet = null;
|
||||||
|
CloseableHttpClient httpsClient = null;
|
||||||
|
CloseableHttpResponse response = null;
|
||||||
try {
|
try {
|
||||||
if (params != null && !params.isEmpty()) {
|
if (params != null && !params.isEmpty()) {
|
||||||
List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());
|
List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());
|
||||||
@@ -557,7 +611,7 @@ public class HttpUtils {
|
|||||||
}
|
}
|
||||||
url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, charset));
|
url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, charset));
|
||||||
}
|
}
|
||||||
HttpGet httpGet = new HttpGet(url);
|
httpGet = new HttpGet(url);
|
||||||
|
|
||||||
Set<Entry<String, String>> set = header.entrySet();
|
Set<Entry<String, String>> set = header.entrySet();
|
||||||
Iterator<Entry<String, String>> s = set.iterator();
|
Iterator<Entry<String, String>> s = set.iterator();
|
||||||
@@ -567,8 +621,8 @@ public class HttpUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// https 注意这里获取https内容,使用了忽略证书的方式,当然还有其他的方式来获取https内容
|
// https 注意这里获取https内容,使用了忽略证书的方式,当然还有其他的方式来获取https内容
|
||||||
CloseableHttpClient httpsClient = HttpUtils.createSSLClientCookie(cookie);
|
httpsClient = HttpUtils.createSSLClientCookie(cookie);
|
||||||
CloseableHttpResponse response = httpsClient.execute(httpGet);
|
response = httpsClient.execute(httpGet);
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
if (statusCode != 200) {
|
if (statusCode != 200) {
|
||||||
httpGet.abort();
|
httpGet.abort();
|
||||||
@@ -583,8 +637,17 @@ public class HttpUtils {
|
|||||||
response.close();
|
response.close();
|
||||||
if(!"UTF-8".equals(getEncoding(result))) result=GBKtoUTF8(result);
|
if(!"UTF-8".equals(getEncoding(result))) result=GBKtoUTF8(result);
|
||||||
return result;
|
return result;
|
||||||
} catch (Exception e) {
|
}catch (Exception e) {
|
||||||
|
System.out.println("url错误:::"+e.toString());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
}finally{
|
||||||
|
try {
|
||||||
|
response.close();
|
||||||
|
httpsClient.close();
|
||||||
|
httpGet.reset();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,651 +0,0 @@
|
|||||||
package sc545.pay.utils;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Map.Entry;
|
|
||||||
|
|
||||||
import javax.servlet.ServletContext;
|
|
||||||
|
|
||||||
public class ReadTxt {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 按行读取txt文件
|
|
||||||
*
|
|
||||||
* @param path
|
|
||||||
* 文件地址(默认D:\\scbox_settings开头)
|
|
||||||
* @return ArrayList 返回每行数据为一个items的集合
|
|
||||||
*/
|
|
||||||
public static ArrayList<String> readFileLine(ServletContext servletContext, String path) {
|
|
||||||
|
|
||||||
if(path.indexOf("./")==0)
|
|
||||||
path =path.substring(1, path.length());
|
|
||||||
|
|
||||||
if (servletContext == null) {
|
|
||||||
if(path.indexOf(":") <= 0&&path.indexOf("/") <0)
|
|
||||||
path = "D:\\scbox_settings\\" + path;
|
|
||||||
} else {
|
|
||||||
path = servletContext.getRealPath(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
ArrayList<String> strarr = null;
|
|
||||||
try {
|
|
||||||
File file = new File(path);
|
|
||||||
if (file.isFile() && file.exists()) { // 判断文件是否存在
|
|
||||||
InputStreamReader read = new InputStreamReader(
|
|
||||||
new FileInputStream(file), "UTF-8");
|
|
||||||
BufferedReader bufferedReader = new BufferedReader(read);
|
|
||||||
String lineTxt = null;
|
|
||||||
strarr = new ArrayList<String>();
|
|
||||||
while ((lineTxt = bufferedReader.readLine()) != null) {
|
|
||||||
|
|
||||||
strarr.add(lineTxt);
|
|
||||||
}
|
|
||||||
|
|
||||||
read.close();
|
|
||||||
} else {
|
|
||||||
strarr = null;
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return strarr;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 写入txt文档,已存在就修改,否则写入末尾,自动加换行 (文件格式:key=value,换行分隔)
|
|
||||||
*
|
|
||||||
* @param path
|
|
||||||
* 文件地址(默认D:\\scbox_settings开头)
|
|
||||||
* @param keyname
|
|
||||||
* 要修改的参数名
|
|
||||||
* @param content
|
|
||||||
* 要修改的参数值
|
|
||||||
*/
|
|
||||||
public static void writeSetting(ServletContext servletContext, String path, String keyname, String content) {
|
|
||||||
|
|
||||||
/**************/
|
|
||||||
// 如果存在,则追加内容;如果文件不存在,则创建文件
|
|
||||||
if(path.indexOf("./")==0)
|
|
||||||
path =path.substring(1, path.length());
|
|
||||||
|
|
||||||
if (servletContext == null) {
|
|
||||||
if(path.indexOf(":") <= 0&&path.indexOf("/") <0)
|
|
||||||
path = "D:\\scbox_settings\\" + path;
|
|
||||||
} else {
|
|
||||||
path = servletContext.getRealPath(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
File file = new File(path);
|
|
||||||
if (file.isFile() && file.exists()) { // 判断文件是否存在
|
|
||||||
InputStreamReader read = new InputStreamReader(
|
|
||||||
new FileInputStream(file), "UTF-8");
|
|
||||||
BufferedReader bufferedReader = new BufferedReader(read);
|
|
||||||
String lineTxt = null;
|
|
||||||
boolean isread = false;// 是否读取到该行并修改
|
|
||||||
// 将内容全部读出来,存为新的数据流
|
|
||||||
// 读到要修改的行进行修改存入,否则原样读取存入
|
|
||||||
StringBuffer buf = new StringBuffer();
|
|
||||||
while ((lineTxt = bufferedReader.readLine()) != null) {
|
|
||||||
if (lineTxt.startsWith(keyname + "=")) {
|
|
||||||
buf.append(keyname + "=" + content + "\r\n");
|
|
||||||
isread = true;
|
|
||||||
} else {
|
|
||||||
buf.append(lineTxt + "\r\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
read.close();
|
|
||||||
if (!isread) {// 没有读取到,写入末尾
|
|
||||||
buf.append(keyname + "=" + content + "\r\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 把新内容覆盖写入文件
|
|
||||||
PrintWriter out = new PrintWriter(new BufferedWriter(
|
|
||||||
new OutputStreamWriter(new FileOutputStream(path),
|
|
||||||
"UTF-8")));
|
|
||||||
out.write(buf.toString());
|
|
||||||
out.flush();
|
|
||||||
out.close();
|
|
||||||
|
|
||||||
} else {// 不存在,创建写入
|
|
||||||
String path2 = path.substring(0, path.replace("\\", "/").lastIndexOf("/"));
|
|
||||||
File file1 = new File(path2);
|
|
||||||
// 如果文件夹不存在则创建
|
|
||||||
if (!file1.exists() && !file1.isDirectory()) {
|
|
||||||
file1.mkdirs();
|
|
||||||
}
|
|
||||||
File file2 = new File(path);
|
|
||||||
if (!file2.exists()) {
|
|
||||||
file2.createNewFile();
|
|
||||||
}
|
|
||||||
|
|
||||||
PrintWriter out = new PrintWriter(new BufferedWriter(
|
|
||||||
new OutputStreamWriter(new FileOutputStream(path),
|
|
||||||
"UTF-8")));
|
|
||||||
out.write(keyname + "=" + content + "\r\n");
|
|
||||||
out.flush();
|
|
||||||
out.close();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
/**************/
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取txt配置,返回map集合(文件格式:key=value,换行分隔)
|
|
||||||
*
|
|
||||||
* @param path
|
|
||||||
* 文件地址(默认D:\\scbox_settings开头)
|
|
||||||
* @return hashmap
|
|
||||||
*/
|
|
||||||
public static HashMap<String, String> readSetting(ServletContext servletContext, String path) {
|
|
||||||
if(path.indexOf("./")==0)
|
|
||||||
path =path.substring(1, path.length());
|
|
||||||
|
|
||||||
if (servletContext == null) {
|
|
||||||
if(path.indexOf(":") <= 0&&path.indexOf("/") <0)
|
|
||||||
path = "D:\\scbox_settings\\" + path;
|
|
||||||
} else {
|
|
||||||
path = servletContext.getRealPath(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
HashMap<String, String> strarr = null;
|
|
||||||
try {
|
|
||||||
File file = new File(path);
|
|
||||||
if (file.isFile() && file.exists()) { // 判断文件是否存在
|
|
||||||
InputStreamReader read = new InputStreamReader(
|
|
||||||
new FileInputStream(file), "UTF-8");
|
|
||||||
BufferedReader bufferedReader = new BufferedReader(read);
|
|
||||||
String lineTxt = null;
|
|
||||||
strarr = new HashMap<String, String>();
|
|
||||||
while ((lineTxt = bufferedReader.readLine()) != null) {
|
|
||||||
if(lineTxt.indexOf("#")==0){
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
String[] arr = new String[2];
|
|
||||||
if (lineTxt.split("=").length >= 2) {
|
|
||||||
arr[0] = lineTxt.split("=")[0];
|
|
||||||
arr[1] = lineTxt.replace(arr[0]+"=", "");
|
|
||||||
strarr.put(arr[0], arr[1]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
read.close();
|
|
||||||
} else {
|
|
||||||
strarr = null;
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return strarr;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取配置文件中所有设置属性(含注释)<br>
|
|
||||||
* map.get("z") 取注释<br>
|
|
||||||
* map.get("k") 取key<br>
|
|
||||||
* map.get("v") 取value<br>
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @param path
|
|
||||||
* 文件地址(默认D:\\scbox_settings开头)
|
|
||||||
*
|
|
||||||
* @param t 只取以此字符开头的,不限制填null
|
|
||||||
* @param st 从以此字符开头的行开始读取,不限制填null
|
|
||||||
* @param et 读到以此字符开头的行结束,不限制填null
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static ArrayList<HashMap<String, String>> getSettingAll(ServletContext servletContext, String path,String t,String st,String et) {
|
|
||||||
if(st==null||st.length()<1) st="";
|
|
||||||
if(et==null||et.length()<1) et="";
|
|
||||||
if(t==null||t.length()<1) t="";
|
|
||||||
if(path.indexOf("./")==0)
|
|
||||||
path =path.substring(1, path.length());
|
|
||||||
|
|
||||||
if (servletContext == null) {
|
|
||||||
if(path.indexOf(":") <= 0&&path.indexOf("/") <0)
|
|
||||||
path = "D:\\scbox_settings\\" + path;
|
|
||||||
} else {
|
|
||||||
path = servletContext.getRealPath(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
ArrayList<HashMap<String, String>> list=new ArrayList<>();
|
|
||||||
try {
|
|
||||||
File file = new File(path);
|
|
||||||
if (file.isFile() && file.exists()) { // 判断文件是否存在
|
|
||||||
InputStreamReader read = new InputStreamReader(
|
|
||||||
new FileInputStream(file), "UTF-8");
|
|
||||||
BufferedReader bufferedReader = new BufferedReader(read);
|
|
||||||
String lineTxt = null;
|
|
||||||
String zs="";//注释文本
|
|
||||||
while ((lineTxt = bufferedReader.readLine()) != null) {
|
|
||||||
if((!"".equals(st))||lineTxt.indexOf(st)!=0) continue;
|
|
||||||
if((!"".equals(et))&&lineTxt.indexOf(et)==0) break;
|
|
||||||
HashMap<String, String> map = new HashMap<String, String>();
|
|
||||||
if(lineTxt.indexOf("#")==0){
|
|
||||||
zs=lineTxt;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
String[] arr = new String[2];
|
|
||||||
if (lineTxt.split("=").length >= 2&&lineTxt.indexOf(t)==0) {
|
|
||||||
arr[0] = lineTxt.split("=")[0];
|
|
||||||
arr[1] = lineTxt.replace(arr[0]+"=", "");
|
|
||||||
map.put("z", zs);
|
|
||||||
map.put("k", arr[0]);
|
|
||||||
map.put("v", arr[1]);
|
|
||||||
list.add(map);
|
|
||||||
//把注释也加进去
|
|
||||||
zs="";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
read.close();
|
|
||||||
} else {
|
|
||||||
list = null;
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取txt配置文件的某一项设置值 (文件格式:key=value,换行分隔)
|
|
||||||
*
|
|
||||||
* @param path
|
|
||||||
* 文件地址
|
|
||||||
* @param settingName
|
|
||||||
* 设置名
|
|
||||||
* @param nullString
|
|
||||||
* 设置值为空时默认返回的内容
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public static String getSetting(ServletContext servletContext, String path, String settingName,String nullString) {
|
|
||||||
HashMap<String, String> setting = ReadTxt.readSetting(servletContext,path);
|
|
||||||
String val = setting == null ? nullString : setting.get(settingName);
|
|
||||||
val = val == null ? "" : val;
|
|
||||||
return "".equals(val.trim()) ? nullString : val;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取所有设置名以[settingName]开头的数据
|
|
||||||
* */
|
|
||||||
public static ArrayList<String> getSettingW(ServletContext servletContext, String path, String settingName) {
|
|
||||||
HashMap<String, String> setting = ReadTxt.readSetting(servletContext,path);
|
|
||||||
if(setting==null||setting.size()<1) return null;
|
|
||||||
Iterator<Entry<String, String>> it = setting.entrySet().iterator();
|
|
||||||
ArrayList<String> rs=new ArrayList<>();
|
|
||||||
while (it.hasNext()) {
|
|
||||||
Entry<String, String> en=it.next();
|
|
||||||
String k = en.getKey();
|
|
||||||
String v = en.getValue();
|
|
||||||
if(k.indexOf(settingName)==0){
|
|
||||||
rs.add(k+"="+v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return rs;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 删除某一项设置值 (文件格式:key=value,换行分隔)
|
|
||||||
*
|
|
||||||
* @param path
|
|
||||||
* 文件地址
|
|
||||||
* @param settingName
|
|
||||||
* 设置名
|
|
||||||
* @param nullString
|
|
||||||
* 设置值为空时默认返回的内容
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public static void delSetting(ServletContext servletContext, String path, String settingName) {
|
|
||||||
|
|
||||||
|
|
||||||
/**************/
|
|
||||||
// 如果存在,则追加内容;如果文件不存在,则创建文件
|
|
||||||
if(path.indexOf("./")==0)
|
|
||||||
path =path.substring(1, path.length());
|
|
||||||
|
|
||||||
if (servletContext == null) {
|
|
||||||
if(path.indexOf(":") <= 0&&path.indexOf("/") <0)
|
|
||||||
path = "D:\\scbox_settings\\" + path;
|
|
||||||
} else {
|
|
||||||
path = servletContext.getRealPath(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
File file = new File(path);
|
|
||||||
if (file.isFile() && file.exists()) { // 判断文件是否存在
|
|
||||||
InputStreamReader read = new InputStreamReader(
|
|
||||||
new FileInputStream(file), "UTF-8");
|
|
||||||
BufferedReader bufferedReader = new BufferedReader(read);
|
|
||||||
String lineTxt = null;
|
|
||||||
// 将内容全部读出来,存为新的数据流
|
|
||||||
// 读到要修改的行进行修改存入,否则原样读取存入
|
|
||||||
StringBuffer buf = new StringBuffer();
|
|
||||||
while ((lineTxt = bufferedReader.readLine()) != null) {
|
|
||||||
if (lineTxt.startsWith(settingName + "=")) {
|
|
||||||
} else {
|
|
||||||
buf.append(lineTxt + "\r\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
read.close();
|
|
||||||
|
|
||||||
// 把新内容覆盖写入文件
|
|
||||||
PrintWriter out = new PrintWriter(new BufferedWriter(
|
|
||||||
new OutputStreamWriter(new FileOutputStream(path),
|
|
||||||
"UTF-8")));
|
|
||||||
out.write(buf.toString());
|
|
||||||
out.flush();
|
|
||||||
out.close();
|
|
||||||
|
|
||||||
} else {// 不存在,创建
|
|
||||||
String path2 = path.substring(0, path.replace("\\", "/").lastIndexOf("/"));
|
|
||||||
File file1 = new File(path2);
|
|
||||||
// 如果文件夹不存在则创建
|
|
||||||
if (!file1.exists() && !file1.isDirectory()) {
|
|
||||||
file1.mkdirs();
|
|
||||||
}
|
|
||||||
File file2 = new File(path);
|
|
||||||
if (!file2.exists()) {
|
|
||||||
file2.createNewFile();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
/**************/
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 删除设置名以[settingName]开头的数据
|
|
||||||
* @param servletContext
|
|
||||||
* @param path
|
|
||||||
* @param settingName
|
|
||||||
*/
|
|
||||||
public static void delSettingW(ServletContext servletContext, String path, String settingName) {
|
|
||||||
|
|
||||||
|
|
||||||
/**************/
|
|
||||||
// 如果存在,则追加内容;如果文件不存在,则创建文件
|
|
||||||
if(path.indexOf("./")==0)
|
|
||||||
path =path.substring(1, path.length());
|
|
||||||
|
|
||||||
if (servletContext == null) {
|
|
||||||
if(path.indexOf(":") <= 0&&path.indexOf("/") <0)
|
|
||||||
path = "D:\\scbox_settings\\" + path;
|
|
||||||
} else {
|
|
||||||
path = servletContext.getRealPath(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
File file = new File(path);
|
|
||||||
if (file.isFile() && file.exists()) { // 判断文件是否存在
|
|
||||||
InputStreamReader read = new InputStreamReader(
|
|
||||||
new FileInputStream(file), "UTF-8");
|
|
||||||
BufferedReader bufferedReader = new BufferedReader(read);
|
|
||||||
String lineTxt = null;
|
|
||||||
// 将内容全部读出来,存为新的数据流
|
|
||||||
// 读到要修改的行进行修改存入,否则原样读取存入
|
|
||||||
StringBuffer buf = new StringBuffer();
|
|
||||||
while ((lineTxt = bufferedReader.readLine()) != null) {
|
|
||||||
if (lineTxt.split("=")[0].indexOf(settingName)==0) {
|
|
||||||
} else {
|
|
||||||
buf.append(lineTxt + "\r\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
read.close();
|
|
||||||
|
|
||||||
// 把新内容覆盖写入文件
|
|
||||||
PrintWriter out = new PrintWriter(new BufferedWriter(
|
|
||||||
new OutputStreamWriter(new FileOutputStream(path),
|
|
||||||
"UTF-8")));
|
|
||||||
out.write(buf.toString());
|
|
||||||
out.flush();
|
|
||||||
out.close();
|
|
||||||
|
|
||||||
} else {// 不存在,创建
|
|
||||||
String path2 = path.substring(0, path.replace("\\", "/").lastIndexOf("/"));
|
|
||||||
File file1 = new File(path2);
|
|
||||||
// 如果文件夹不存在则创建
|
|
||||||
if (!file1.exists() && !file1.isDirectory()) {
|
|
||||||
file1.mkdirs();
|
|
||||||
}
|
|
||||||
File file2 = new File(path);
|
|
||||||
if (!file2.exists()) {
|
|
||||||
file2.createNewFile();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
/**************/
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 删除过期的所有设置值 (文件格式:key=value,换行分隔)
|
|
||||||
* <br>key为检测源
|
|
||||||
* @param servletContext
|
|
||||||
* @param path 路径
|
|
||||||
* @param d 有效天数
|
|
||||||
*/
|
|
||||||
public static void delOutDateSetting(ServletContext servletContext, String path, int d) {
|
|
||||||
|
|
||||||
|
|
||||||
/**************/
|
|
||||||
// 如果存在,则追加内容;如果文件不存在,则创建文件
|
|
||||||
if(path.indexOf("./")==0)
|
|
||||||
path =path.substring(1, path.length());
|
|
||||||
|
|
||||||
if (servletContext == null) {
|
|
||||||
if(path.indexOf(":") <= 0&&path.indexOf("/") <0)
|
|
||||||
path = "D:\\scbox_settings\\" + path;
|
|
||||||
} else {
|
|
||||||
path = servletContext.getRealPath(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
File file = new File(path);
|
|
||||||
if (file.isFile() && file.exists()) { // 判断文件是否存在
|
|
||||||
InputStreamReader read = new InputStreamReader(
|
|
||||||
new FileInputStream(file), "UTF-8");
|
|
||||||
BufferedReader bufferedReader = new BufferedReader(read);
|
|
||||||
String lineTxt = null;
|
|
||||||
// 将内容全部读出来,存为新的数据流
|
|
||||||
// 读到要修改的行进行修改存入,否则原样读取存入
|
|
||||||
StringBuffer buf = new StringBuffer();
|
|
||||||
boolean ts=false;
|
|
||||||
while ((lineTxt = bufferedReader.readLine()) != null) {
|
|
||||||
|
|
||||||
if(lineTxt.indexOf("=")>0&&lineTxt.indexOf("#")<0){
|
|
||||||
String[] ls = lineTxt.split("=");
|
|
||||||
int has = ls[0].length()-ls[0].replaceAll("-", "").length();
|
|
||||||
if(has>1){//日期时间
|
|
||||||
if(Utils.dateout(ls[0], d)){
|
|
||||||
buf.append(lineTxt + "\r\n");
|
|
||||||
}else{
|
|
||||||
ts=true;
|
|
||||||
}//过期
|
|
||||||
}else{//月份时间
|
|
||||||
buf.append(lineTxt + "\r\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
}else{//注释或者不是setting
|
|
||||||
buf.append(lineTxt + "\r\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
read.close();
|
|
||||||
if(ts){
|
|
||||||
// 把新内容覆盖写入文件
|
|
||||||
PrintWriter out = new PrintWriter(new BufferedWriter(
|
|
||||||
new OutputStreamWriter(new FileOutputStream(path),
|
|
||||||
"UTF-8")));
|
|
||||||
out.write(buf.toString());
|
|
||||||
out.flush();
|
|
||||||
out.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {// 不存在,创建
|
|
||||||
String path2 = path.substring(0, path.replace("\\", "/").lastIndexOf("/"));
|
|
||||||
File file1 = new File(path2);
|
|
||||||
// 如果文件夹不存在则创建
|
|
||||||
if (!file1.exists() && !file1.isDirectory()) {
|
|
||||||
file1.mkdirs();
|
|
||||||
}
|
|
||||||
File file2 = new File(path);
|
|
||||||
if (!file2.exists()) {
|
|
||||||
file2.createNewFile();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
/**************/
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 删除过期的所有设置值 (文件格式:key=value,换行分隔)
|
|
||||||
* <br>value为检测源
|
|
||||||
* @param servletContext
|
|
||||||
* @param path 路径
|
|
||||||
* @param d 有效天数
|
|
||||||
*/
|
|
||||||
public static void delOutDateSettingValue(ServletContext servletContext, String path, int d) {
|
|
||||||
|
|
||||||
|
|
||||||
/**************/
|
|
||||||
// 如果存在,则追加内容;如果文件不存在,则创建文件
|
|
||||||
if(path.indexOf("./")==0)
|
|
||||||
path =path.substring(1, path.length());
|
|
||||||
|
|
||||||
if (servletContext == null) {
|
|
||||||
if(path.indexOf(":") <= 0&&path.indexOf("/") <0)
|
|
||||||
path = "D:\\scbox_settings\\" + path;
|
|
||||||
} else {
|
|
||||||
path = servletContext.getRealPath(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
File file = new File(path);
|
|
||||||
if (file.isFile() && file.exists()) { // 判断文件是否存在
|
|
||||||
InputStreamReader read = new InputStreamReader(
|
|
||||||
new FileInputStream(file), "UTF-8");
|
|
||||||
BufferedReader bufferedReader = new BufferedReader(read);
|
|
||||||
String lineTxt = null;
|
|
||||||
// 将内容全部读出来,存为新的数据流
|
|
||||||
// 读到要修改的行进行修改存入,否则原样读取存入
|
|
||||||
StringBuffer buf = new StringBuffer();
|
|
||||||
boolean ts=false;
|
|
||||||
while ((lineTxt = bufferedReader.readLine()) != null) {
|
|
||||||
|
|
||||||
if(lineTxt.indexOf("=")>0&&lineTxt.indexOf("#")<0){
|
|
||||||
String[] ls = lineTxt.split("=");
|
|
||||||
int has = ls[1].length()-ls[1].replaceAll("-", "").length();
|
|
||||||
if(has>1){//日期时间
|
|
||||||
if(Utils.dateout(ls[1], d)){
|
|
||||||
buf.append(lineTxt + "\r\n");
|
|
||||||
}else{
|
|
||||||
ts=true;
|
|
||||||
}//过期
|
|
||||||
}else{//月份时间
|
|
||||||
buf.append(lineTxt + "\r\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
}else{//注释或者不是setting
|
|
||||||
buf.append(lineTxt + "\r\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
read.close();
|
|
||||||
if(ts){
|
|
||||||
// 把新内容覆盖写入文件
|
|
||||||
PrintWriter out = new PrintWriter(new BufferedWriter(
|
|
||||||
new OutputStreamWriter(new FileOutputStream(path),
|
|
||||||
"UTF-8")));
|
|
||||||
out.write(buf.toString());
|
|
||||||
out.flush();
|
|
||||||
out.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {// 不存在,创建
|
|
||||||
String path2 = path.substring(0, path.replace("\\", "/").lastIndexOf("/"));
|
|
||||||
File file1 = new File(path2);
|
|
||||||
// 如果文件夹不存在则创建
|
|
||||||
if (!file1.exists() && !file1.isDirectory()) {
|
|
||||||
file1.mkdirs();
|
|
||||||
}
|
|
||||||
File file2 = new File(path);
|
|
||||||
if (!file2.exists()) {
|
|
||||||
file2.createNewFile();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
/**************/
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将InputStream装换成某种字符编码的String
|
|
||||||
*
|
|
||||||
* @param in
|
|
||||||
* @param encoding
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static String InputStreamTOString(InputStream in, String encoding) {
|
|
||||||
int BUFFER_SIZE = 4096;
|
|
||||||
String res = null;
|
|
||||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
||||||
byte[] data = new byte[BUFFER_SIZE];
|
|
||||||
int count = -1;
|
|
||||||
try {
|
|
||||||
while ((count = in.read(data, 0, BUFFER_SIZE)) != -1) {
|
|
||||||
outputStream.write(data, 0, count);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
res = new String(outputStream.toByteArray(), encoding);
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user