16 Commits
r6 ... r20

Author SHA1 Message Date
admin
0eac23a64a db 2022-11-17 14:07:10 +08:00
admin
327784354c db 2022-11-17 10:44:43 +08:00
admin
1bec66a97c db 2022-11-17 10:44:33 +08:00
admin
273332c720 db 2022-11-11 12:19:06 +08:00
admin
5b2586112a db 2022-11-11 12:18:54 +08:00
admin
c843272f03 add 2022-11-09 16:11:16 +08:00
admin
9f6314c9e9 更新 2022-11-09 15:48:47 +08:00
admin
27f941768a utils 2022-11-09 11:44:55 +08:00
admin
63ae9e7433 同步 FileUtils (附件未完成) 2022-11-08 11:24:13 +08:00
admin
2d242988e1 修复json 2022-10-30 15:30:16 +08:00
admin
c976264581 支付优化 2022-10-27 16:29:23 +08:00
admin
b7c068e1ae mod 2022-10-18 12:42:08 +08:00
admin
b43a94f006 add HttpUtils 2022-10-18 12:03:54 +08:00
admin
163a0f46df httpClient 修复 2022-10-18 12:00:41 +08:00
admin
4625fbefed utils 2022-10-14 09:08:47 +08:00
admin
d7e971f32d add 2022-10-13 17:13:54 +08:00
6 changed files with 262 additions and 124 deletions

View File

@@ -5,7 +5,7 @@
<meta charset='UTF-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<meta name='viewport' content='width=device-width, initial-scale=1.0'>
<title>顺诚百宝箱</title>
<title>支付测试- 顺诚百宝箱</title>
<style>
* {
margin: 10px auto;
@@ -39,7 +39,7 @@
}
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) {
@@ -54,7 +54,7 @@
alert('获取二维码失败');
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);
} else alert('遇到错误 ' + res.msg);
@@ -77,7 +77,7 @@
await subscribe(id);
} else {
// 获取并显示消息
let message = eval("(" + await response.text() + ")");
let message = JSON.parse(await response.text());
// 再次调用 subscribe() 以获取下一条消息
if (message.msg == "已支付") {
@@ -94,7 +94,7 @@
*/
async function selectPay(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 == "已支付") {
return okPay();

View File

@@ -16,8 +16,9 @@ import java.util.Map;
public class DBUtil {
// 四大金刚
String dname = ReadTxt.getSetting(null, "setting.ini", "MySqlName","test");
String driver = "com.mysql.jdbc.Driver";// 驱动名称
String url = "jdbc:mysql://"+ReadTxt.getSetting(null, "setting.ini", "MySqlIP","127.0.0.1")+":"+ReadTxt.getSetting(null, "setting.ini", "MySqlPort","3306")+"/"+ReadTxt.getSetting(null, "setting.ini", "MySqlName","test")+"?useUnicode=true&characterEncoding=UTF-8&useOldAliasMetadataBehavior=true&autoReconnect=true&useSSL=false";// 连接
String url = "jdbc:mysql://"+ReadTxt.getSetting(null, "setting.ini", "MySqlIP","127.0.0.1")+":"+ReadTxt.getSetting(null, "setting.ini", "MySqlPort","3306")+"/"+dname+"?useUnicode=false&characterEncoding=UTF-8&useOldAliasMetadataBehavior=true&autoReconnect=true&useSSL=false";// 连接
String username = ReadTxt.getSetting(null, "setting.ini", "MySqlUserName","root");// 用户名
String password = ReadTxt.getSetting(null, "setting.ini", "MySqlPassWord","cheng355217");// 密码
@@ -25,6 +26,15 @@ public class DBUtil {
Connection con = null;// 连接对象
PreparedStatement pstmt = null;// 语句对象
ResultSet rs = null;// 结果集对象
public DBUtil(String dbname){
if(dbname!=null&&dbname.trim().length()>0)
this.dname = dbname;
}
public DBUtil(){
}
/**
* 获得连接对象
@@ -74,6 +84,7 @@ public class DBUtil {
* @return 影响行数
*/
public int execUpdate(String sql, Object[] params) {
try {
this.getConnection();// 获得连接对象
this.pstmt = this.con.prepareStatement(sql);// 获得预设语句对象
@@ -193,31 +204,6 @@ public class DBUtil {
return null;
}
/**
* 搜索统计
*
* @param search
* @return
*/
public boolean searchCount(String search) {
String resid = "0";
List<Map<String, Object>> res = execQuery(
"select id from byx_count where searchstr =?",
new String[] { search });
if (res != null && res.size() > 0) {
resid = res.get(0).get("id").toString();
int i = execUpdate(
"update byx_count set countsum = countsum+1 where id = "
+ resid, null);
return i > 0 ? true : false;
} else {
int i1 = execUpdate(
"insert into byx_count(countsum,searchstr) values(1,?)",
new String[] { search });
return i1 > 0 ? true : false;
}
}
/**
* 获取对象的属性,返回键值对
* @param obj

View File

@@ -359,7 +359,7 @@ public static String putImg(ServletContext servletContext,MultipartFile file,Str
saveSql(realurl, towhere,fileName);
}
if(servletContext != null) return "/getFile/"+rid;
if(servletContext != null) return "/getImg/"+rid;
else return realurl;
}
@@ -452,7 +452,7 @@ public static String putImgAsName(ServletContext servletContext,MultipartFile fi
saveSql(realurl, towhere,fileName);
}
if(servletContext != null) return "/getFile/"+rid;
if(servletContext != null) return "/getImg/"+rid;
else return realurl;
}
@@ -548,7 +548,7 @@ public static String putVideo(ServletContext servletContext,MultipartFile file,S
saveSql(realurl, towhere,fileName);
}
if(servletContext != null) return "/getFile/"+rid;
if(servletContext != null) return "/getImg/"+rid;
else return realurl;
}
@@ -669,8 +669,8 @@ public static void saveSql(String realurl, String towhere, String saveName) {
String msid = towhere.split("#")[1];
String mstag = towhere.split("#")[2];
if(realurl.indexOf("/getFile/")==0) {
realurl=realurl.replace("/getFile/", "");
if(realurl.indexOf("/getImg/")==0) {
realurl=realurl.replace("/getImg/", "");
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()});
}else{//不存在
@@ -780,8 +780,8 @@ public static void delfile(ServletContext servletContext, String path) {
if(path.indexOf(":") <= 0)
path1 = "D:\\" + path;
} else {
if(path.indexOf("/getFile/")==0){
String rid = path.replace("/getFile/", "");
if(path.indexOf("/getImg/")==0){
String rid = path.replace("/getImg/", "");
DBUtil db = new DBUtil();
List<Map<String, Object>> rs = db.execQuery("select * from savefile where id ="+rid, null);
if(rs!=null&&rs.size()>0){
@@ -828,8 +828,8 @@ public static void delFileForStr(ServletContext servletContext, String str) {
if(path.indexOf(":") <= 0)
path1 = "D:\\" + path;
} else {
if(path.indexOf("/getFile/")==0){
String rid = path.replace("/getFile/", "");
if(path.indexOf("/getImg/")==0){
String rid = path.replace("/getImg/", "");
List<Map<String, Object>> rs = db.execQuery("select * from savefile where id ="+rid, null);
if(rs!=null&&rs.size()>0){
oldpath=rs.get(0).get("realurl")+"";
@@ -867,8 +867,8 @@ public static void intoSqlForStr(String str,String towhere,String saveName) {
mstag = towhere.split("#")[2];
for (String path : imgs) {
if(path.indexOf("/getFile/")==0){
String rid = path.replace("/getFile/", "");
if(path.indexOf("/getImg/")==0){
String rid = path.replace("/getImg/", "");
List<Map<String, Object>> rs = db.execQuery("select * from savefile where id ="+rid, null);
if(rs!=null&&rs.size()>0){
path=rs.get(0).get("realurl")+"";

View File

@@ -22,7 +22,6 @@ import javax.net.ssl.X509TrustManager;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
@@ -49,14 +48,7 @@ import org.apache.http.util.EntityUtils;
*/
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);
@@ -82,7 +74,10 @@ public class HttpUtils {
if (StringUtils.isBlank(url)) {
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 {
if (params != null && !params.isEmpty()) {
List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());
@@ -95,8 +90,8 @@ public class HttpUtils {
// 将请求参数和url进行拼接
url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, charset));
}
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = httpClient.execute(httpGet);
httpGet = new HttpGet(url);
response = httpClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200) {
httpGet.abort();
@@ -114,7 +109,20 @@ public class HttpUtils {
return result;
} catch (Exception e) {
System.out.println("url错误:::"+e.toString());
try {
response.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}finally{
try {
response.close();
httpClient.close();
httpGet.reset();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@@ -146,6 +154,8 @@ public class HttpUtils {
}
HttpPost httpPost = new HttpPost(url);
CloseableHttpResponse response = null;
RequestConfig config = RequestConfig.custom().setConnectTimeout(10000).setSocketTimeout(10000).setConnectionRequestTimeout(5000).build();
CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
try {
if (pairs != null && pairs.size() > 0) {
httpPost.setEntity(new UrlEncodedFormEntity(pairs, CHARSET));
@@ -165,14 +175,19 @@ public class HttpUtils {
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) {}
}catch (Exception e) {
System.out.println("url错误:::"+e.toString());
e.printStackTrace();
}finally{
try {
response.close();
httpClient.close();
httpPost.reset();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
@@ -187,7 +202,9 @@ public class HttpUtils {
if (StringUtils.isBlank(url)) {
return null;
}
CloseableHttpResponse response = null;
HttpGet httpGet = null;
CloseableHttpClient httpsClient = null;
try {
if (params != null && !params.isEmpty()) {
List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());
@@ -199,11 +216,11 @@ public class HttpUtils {
}
url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, charset));
}
HttpGet httpGet = new HttpGet(url);
httpGet = new HttpGet(url);
// https 注意这里获取https内容使用了忽略证书的方式当然还有其他的方式来获取https内容
CloseableHttpClient httpsClient = HttpUtils.createSSLClientDefault();
CloseableHttpResponse response = httpsClient.execute(httpGet);
httpsClient = HttpUtils.createSSLClientDefault();
response = httpsClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200) {
httpGet.abort();
@@ -218,8 +235,17 @@ public class HttpUtils {
response.close();
if(!"UTF-8".equals(getEncoding(result))) result=GBKtoUTF8(result);
return result;
} catch (Exception e) {
}catch (Exception e) {
System.out.println("url错误:::"+e.toString());
e.printStackTrace();
}finally{
try {
response.close();
httpsClient.close();
httpGet.reset();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@@ -287,6 +313,7 @@ public class HttpUtils {
CloseableHttpClient client = httpClientBuilder.build();
client = (CloseableHttpClient) wrapClient(client);
HttpPost post = new HttpPost(url);
CloseableHttpResponse res = null;
try {
StringEntity s = new StringEntity(json, "utf-8");
if (StringUtils.isBlank(contentType)) {
@@ -294,14 +321,23 @@ public class HttpUtils {
}
s.setContentType(contentType);
post.setEntity(s);
HttpResponse res = client.execute(post);
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();
}
}catch (Exception e) {
System.out.println("url错误:::"+e.toString());
e.printStackTrace();
}finally{
try {
res.close();
client.close();
post.reset();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@@ -358,6 +394,8 @@ public class HttpUtils {
return false;
}
HttpGet httpGet = new HttpGet(url);
RequestConfig config = RequestConfig.custom().setConnectTimeout(10000).setSocketTimeout(10000).setConnectionRequestTimeout(5000).build();
CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
try {
CloseableHttpResponse response = httpClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
@@ -508,12 +546,13 @@ public class HttpUtils {
}
CloseableHttpResponse response = null;
CloseableHttpClient httpClientc = null;
try {
if (pairs != null && pairs.size() > 0) {
httpPost.setEntity(new UrlEncodedFormEntity(pairs, CHARSET));
}
CloseableHttpClient httpClientc = HttpUtils.createSSLClientCookie(cookie);
httpClientc = HttpUtils.createSSLClientCookie(cookie);
response = httpClientc.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200) {
@@ -529,16 +568,28 @@ public class HttpUtils {
if(!"UTF-8".equals(getEncoding(result))) result=GBKtoUTF8(result);
return result;
} catch (Exception e) {
System.out.println("url错误:::"+e.toString());
e.printStackTrace();
} finally {
if (response != null)
try {
response.close();
} catch (IOException e) {e.printStackTrace();}
}finally{
try {
response.close();
httpClientc.close();
httpPost.reset();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 带有cookie和header 的get请求
* @param url
* @param params
* @param cookie
* @param header
* @return
*/
@SuppressWarnings("rawtypes")
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";
@@ -546,6 +597,9 @@ public class HttpUtils {
return null;
}
HttpGet httpGet = null;
CloseableHttpClient httpsClient = null;
CloseableHttpResponse response = null;
try {
if (params != null && !params.isEmpty()) {
List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());
@@ -557,7 +611,7 @@ public class HttpUtils {
}
url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, charset));
}
HttpGet httpGet = new HttpGet(url);
httpGet = new HttpGet(url);
Set<Entry<String, String>> set = header.entrySet();
Iterator<Entry<String, String>> s = set.iterator();
@@ -567,8 +621,8 @@ public class HttpUtils {
}
// https 注意这里获取https内容使用了忽略证书的方式当然还有其他的方式来获取https内容
CloseableHttpClient httpsClient = HttpUtils.createSSLClientCookie(cookie);
CloseableHttpResponse response = httpsClient.execute(httpGet);
httpsClient = HttpUtils.createSSLClientCookie(cookie);
response = httpsClient.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200) {
httpGet.abort();
@@ -583,8 +637,17 @@ public class HttpUtils {
response.close();
if(!"UTF-8".equals(getEncoding(result))) result=GBKtoUTF8(result);
return result;
} catch (Exception e) {
}catch (Exception e) {
System.out.println("url错误:::"+e.toString());
e.printStackTrace();
}finally{
try {
response.close();
httpsClient.close();
httpGet.reset();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}

View File

@@ -617,6 +617,65 @@ public class ReadTxt {
/**************/
}
/**
* 删除全部内容
*
* @param path
* 文件地址
* @param nullString
* 设置值为空时默认返回的内容
* @return string
*/
public static void delAll(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);
}
try {
File file = new File(path);
if (file.isFile() && file.exists()) { // 判断文件是否存在
// 把新内容覆盖写入文件
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(path),
"UTF-8")));
out.write("".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();
}
/**************/
}
/**

View File

@@ -162,6 +162,79 @@ public class Utils {
}
/**
* 判断某个时间是否过期。传进来一个旧时间,和有效天数,判断今天这个旧时间是否过期
*
* @param d
* 旧时间 "2019-8-5"
* @param day
* 有效天数
* @return true:没有过期false:过期了
*/
public static boolean dateout(String d, int day) {
if (d == null || "".equals(d) || "null".equals(d))
return false;
Date date = Utils.dateadd(Utils.string2Date(d), day);
Date newdate = new Date();
int i = date.compareTo(newdate);
if (i >= 0) {
return true;
} else {
return false;
}
}
/**
* 计算两个时间相差的秒数
*
* @param endDate
* 结束时间 null为当前
* @param nowDate
* 开始时间
* @return
*/
public static long datePoor(Date endDate, Date nowDate) {
if(endDate==null) endDate = new Date();
long ns = 1000;// 一秒
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - nowDate.getTime();
long sec = diff / ns;// 计算差多少秒
return sec;
}
/**
* 计算两个时间相差的秒数
*
* @param endDate
* 结束时间 null为当前
* @param nowDate
* 开始时间
* @return
*/
public static long datePoor(String endDates, String nowDates) {
if(endDates==null) endDates=Utils.date2String(null, null);
Date endDate = Utils.string2Date(endDates, null);
Date nowDate = Utils.string2Date(nowDates, null);
long ns = 1000;// 一秒
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - nowDate.getTime();
long sec = diff / ns;// 计算差多少秒
return sec;
}
/**
* 设置定时器启动时间
* @param format 格式 yyyy-MM-dd 07:30:00
@@ -237,28 +310,6 @@ public class Utils {
return s2;
}
/**
* 判断某个时间是否过期。传进来一个旧时间,和有效天数,判断今天这个旧时间是否过期
*
* @param d
* 旧时间 "2019-8-5"
* @param day
* 有效天数
* @return true:没有过期false:过期了
*/
public static boolean dateout(String d, int day) {
if (d == null || "".equals(d) || "null".equals(d))
return false;
Date date = Utils.dateadd(Utils.string2Date(d), day);
Date newdate = new Date();
int i = date.compareTo(newdate);
if (i >= 0) {
return true;
} else {
return false;
}
}
/**
* URL解析与转义
*
@@ -690,28 +741,6 @@ public class Utils {
return Long.valueOf(str2);
}
/**
* 计算两个时间相差的秒数
*
* @param endDate
* 结束时间
* @param nowDate
* 开始时间
* @return
*/
public static long getDatePoor(Date endDate, Date nowDate) {
long ns = 1000;// 一秒
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - nowDate.getTime();
long sec = diff / ns;// 计算差多少秒
return sec;
}
/**获取今天是周几1-7*/
public static long getWeekNum() {
long[] weekDays = {7,1,2,3,4,5,6};
@@ -1088,7 +1117,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];
}