Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
593620998f | ||
|
|
dbb6bb1f17 | ||
|
|
0eac23a64a | ||
|
|
327784354c | ||
|
|
1bec66a97c | ||
|
|
273332c720 | ||
|
|
5b2586112a | ||
|
|
c843272f03 | ||
|
|
9f6314c9e9 | ||
|
|
27f941768a | ||
|
|
63ae9e7433 | ||
|
|
2d242988e1 | ||
|
|
c976264581 | ||
|
|
b7c068e1ae | ||
|
|
b43a94f006 |
@@ -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();
|
||||
|
||||
@@ -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");// 密码
|
||||
|
||||
@@ -26,6 +27,15 @@ public class DBUtil {
|
||||
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
|
||||
|
||||
@@ -13,6 +13,7 @@ import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
@@ -279,9 +280,12 @@ public static BufferedImage cutImgBuf(InputStream img,Positions p,int x,int y,in
|
||||
* @return 图片路径(相对)
|
||||
*/
|
||||
public static String putImg(ServletContext servletContext,MultipartFile file,String path,String towhere) {
|
||||
int maxSize = 1024*1024*2; //上传最大为2MB
|
||||
if (file==null||file.getSize()>maxSize) {
|
||||
return "图片最大上传2MB";
|
||||
long maxSize = Utils.getNum(ReadTxt.getSetting(null, "setting.ini", "imgsize", "10")) * 1048576; //上传最大 MB
|
||||
if (file==null) {
|
||||
return "";
|
||||
}
|
||||
if (file.getSize()>maxSize) {
|
||||
return "文件最大上传"+(maxSize/1048576)+"MB";
|
||||
}
|
||||
|
||||
|
||||
@@ -297,7 +301,6 @@ public static String putImg(ServletContext servletContext,MultipartFile file,Str
|
||||
path = "D:/"+(lpath.replaceAll("D:/", "").replaceAll("D://", "").replaceAll("D:", "").replaceAll("c:", ""));//图片存储在C盘下的路径
|
||||
else{
|
||||
//修改:原本存储在项目根路径下的,为了防止删除项目文件丢失
|
||||
//path = servletContext.getRealPath(lpath);//图片存储在项目根目录下的路径
|
||||
//现修改为:存储到固定文件夹(子路径保留),读取的时候通过url获取base64格式图片
|
||||
String basePath = ReadTxt.getSetting(null, "setting.ini", "FilePath", "D://scbox_settings/webFile");
|
||||
if("/".equals(basePath.substring(basePath.length()-1, basePath.length()))) basePath=basePath.substring(0, basePath.length()-1);
|
||||
@@ -359,7 +362,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;
|
||||
}
|
||||
|
||||
@@ -372,10 +375,14 @@ public static String putImg(ServletContext servletContext,MultipartFile file,Str
|
||||
* @return 图片路径(相对)
|
||||
*/
|
||||
public static String putImgAsName(ServletContext servletContext,MultipartFile file,String path,String filename,String towhere) {
|
||||
int maxSize = 1024*1024*2; //上传最大为2MB
|
||||
if (file==null||file.getSize()>maxSize) {
|
||||
return "图片最大上传2MB";
|
||||
long maxSize = Utils.getNum(ReadTxt.getSetting(null, "setting.ini", "imgsize", "10")) * 1048576; //上传最大 MB
|
||||
if (file==null) {
|
||||
return "";
|
||||
}
|
||||
if (file.getSize()>maxSize) {
|
||||
return "文件最大上传"+(maxSize/1048576)+"MB";
|
||||
}
|
||||
|
||||
File targetFile=null;
|
||||
String rid="";
|
||||
String realurl="";//返回存储路径
|
||||
@@ -387,7 +394,6 @@ public static String putImgAsName(ServletContext servletContext,MultipartFile fi
|
||||
path = "D:/"+(lpath.replaceAll("D:/", "").replaceAll("D://", "").replaceAll("D:", "").replaceAll("c:", ""));//图片存储在C盘下的路径
|
||||
else{
|
||||
//修改:原本存储在项目根路径下的,为了防止删除项目文件丢失
|
||||
//path = servletContext.getRealPath(lpath);//图片存储在项目根目录下的路径
|
||||
//现修改为:存储到固定文件夹(子路径保留),读取的时候通过url获取base64格式图片
|
||||
String basePath = ReadTxt.getSetting(null, "setting.ini", "FilePath", "D://scbox_settings/webFile");
|
||||
if("/".equals(basePath.substring(basePath.length()-1, basePath.length()))) basePath=basePath.substring(0, basePath.length()-1);
|
||||
@@ -452,7 +458,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;
|
||||
}
|
||||
|
||||
@@ -466,13 +472,14 @@ public static String putImgAsName(ServletContext servletContext,MultipartFile fi
|
||||
* @return 图片路径(相对)
|
||||
*/
|
||||
public static String putVideo(ServletContext servletContext,MultipartFile file,String path,String towhere) {
|
||||
int maxSize = 1024*1024*10; //上传最大为2MB
|
||||
long maxSize = Utils.getNum(ReadTxt.getSetting(null, "setting.ini", "videosize", "50")) * 1048576; //上传最大 MB
|
||||
if (file==null) {
|
||||
return "";
|
||||
}
|
||||
if (file.getSize()>maxSize) {
|
||||
return "视频最大上传10MB";
|
||||
return "文件最大上传"+(maxSize/1048576)+"MB";
|
||||
}
|
||||
|
||||
File targetFile=null;
|
||||
String rid="";
|
||||
String realurl="";//返回存储路径
|
||||
@@ -485,7 +492,6 @@ public static String putVideo(ServletContext servletContext,MultipartFile file,S
|
||||
else{
|
||||
|
||||
//修改:原本存储在项目根路径下的,为了防止删除项目文件丢失
|
||||
//path = servletContext.getRealPath(lpath);//图片存储在项目根目录下的路径
|
||||
//现修改为:存储到固定文件夹(子路径保留),读取的时候通过url获取base64格式图片
|
||||
String basePath = ReadTxt.getSetting(null, "setting.ini", "FilePath", "D://scbox_settings/webFile");
|
||||
if("/".equals(basePath.substring(basePath.length()-1, basePath.length()))) basePath=basePath.substring(0, basePath.length()-1);
|
||||
@@ -548,7 +554,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;
|
||||
}
|
||||
|
||||
@@ -565,12 +571,12 @@ public static String putVideo(ServletContext servletContext,MultipartFile file,S
|
||||
* @return 图片路径(相对)
|
||||
*/
|
||||
public static String putFile(ServletContext servletContext,MultipartFile file,String filename,String path,String towhere) {
|
||||
int maxSize = 1024*1024*10; //上传最大为2MB
|
||||
long maxSize = Utils.getNum(ReadTxt.getSetting(null, "setting.ini", "filesize", "50")) * 1048576; //上传最大 MB
|
||||
if (file==null) {
|
||||
return "";
|
||||
}
|
||||
if (file.getSize()>maxSize) {
|
||||
return "文件最大上传20MB";
|
||||
return "error:文件最大上传"+(maxSize/1048576)+"MB";
|
||||
}
|
||||
File targetFile=null;
|
||||
String rid="";
|
||||
@@ -583,7 +589,6 @@ public static String putFile(ServletContext servletContext,MultipartFile file,St
|
||||
path = "D:/"+(lpath.replaceAll("D:/", "").replaceAll("D://", "").replaceAll("D:", "").replaceAll("c:", ""));//图片存储在C盘下的路径
|
||||
else{
|
||||
//修改:原本存储在项目根路径下的,为了防止删除项目文件丢失
|
||||
//path = servletContext.getRealPath(lpath);//图片存储在项目根目录下的路径
|
||||
//现修改为:存储到固定文件夹(子路径保留),读取的时候通过url获取base64格式图片
|
||||
String basePath = ReadTxt.getSetting(null, "setting.ini", "FilePath", "D://scbox_settings/webFile");
|
||||
if("/".equals(basePath.substring(basePath.length()-1, basePath.length()))) basePath=basePath.substring(0, basePath.length()-1);
|
||||
@@ -626,7 +631,7 @@ public static String putFile(ServletContext servletContext,MultipartFile file,St
|
||||
//System.out.println("图片上传成功 \nUrl:"+realurl);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return "系统异常,视频上传失败";
|
||||
return "error:系统异常,视频上传失败";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -669,8 +674,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{//不存在
|
||||
@@ -722,7 +727,6 @@ public static String writeFile(ServletContext servletContext, String path,
|
||||
else path1 = path;
|
||||
} else {
|
||||
//修改:原本存储在项目根路径下的,为了防止删除项目文件丢失
|
||||
//path = servletContext.getRealPath(lpath);//图片存储在项目根目录下的路径
|
||||
//现修改为:存储到固定文件夹(子路径保留),读取的时候通过url获取base64格式图片
|
||||
String basePath = ReadTxt.getSetting(null, "setting.ini", "FilePath", "D://scbox_settings/webFile");
|
||||
if("/".equals(basePath.substring(basePath.length()-1, basePath.length()))) basePath=basePath.substring(0, basePath.length()-1);
|
||||
@@ -780,15 +784,15 @@ 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){
|
||||
path=rs.get(0).get("realurl")+"";
|
||||
path1=rs.get(0).get("realurl")+"";
|
||||
}
|
||||
} else path1 = servletContext.getRealPath(path);
|
||||
}
|
||||
}
|
||||
|
||||
File file1 = new File(path1);
|
||||
@@ -828,14 +832,14 @@ 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")+"";
|
||||
path1=rs.get(0).get("realurl")+"";
|
||||
}
|
||||
} else path1 = servletContext.getRealPath(path);
|
||||
}
|
||||
}
|
||||
|
||||
File file1 = new File(path1);
|
||||
@@ -867,8 +871,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")+"";
|
||||
@@ -951,13 +955,19 @@ public static MultipartFile getMultipartFile(File file) {
|
||||
* @param file
|
||||
* @return
|
||||
*/
|
||||
public static ResponseEntity<FileSystemResource> returnFile(File file) {
|
||||
public static ResponseEntity<FileSystemResource> returnFile(File file, String name) {
|
||||
if (file == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
if(name!=null&&name.length()>0)
|
||||
name = new String(name.getBytes("UTF-8"),"ISO-8859-1");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||
headers.add("Content-Disposition", "attachment; filename=" + file.getName());
|
||||
headers.add("Content-Disposition", "attachment; filename=" + (name!=null&&name.length()>0?name:file.getName()));
|
||||
headers.add("Pragma", "no-cache");
|
||||
headers.add("Expires", "0");
|
||||
headers.add("Last-Modified", new Date().toString());
|
||||
|
||||
657
src/sc545/pay/utils/HttpUtils.java
Normal file
657
src/sc545/pay/utils/HttpUtils.java
Normal file
@@ -0,0 +1,657 @@
|
||||
package sc545.pay.utils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
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.HttpEntity;
|
||||
import org.apache.http.NameValuePair;
|
||||
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 {
|
||||
|
||||
public static final String CHARSET = "UTF-8";
|
||||
|
||||
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;
|
||||
}
|
||||
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());
|
||||
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 = new HttpGet(url);
|
||||
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());
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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;
|
||||
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));
|
||||
}
|
||||
|
||||
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) {
|
||||
System.out.println("url错误:::"+e.toString());
|
||||
e.printStackTrace();
|
||||
}finally{
|
||||
try {
|
||||
response.close();
|
||||
httpClient.close();
|
||||
httpPost.reset();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
CloseableHttpResponse response = null;
|
||||
HttpGet httpGet = null;
|
||||
CloseableHttpClient httpsClient = 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 = new HttpGet(url);
|
||||
|
||||
// https 注意这里获取https内容,使用了忽略证书的方式,当然还有其他的方式来获取https内容
|
||||
httpsClient = HttpUtils.createSSLClientDefault();
|
||||
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) {
|
||||
System.out.println("url错误:::"+e.toString());
|
||||
e.printStackTrace();
|
||||
}finally{
|
||||
try {
|
||||
response.close();
|
||||
httpsClient.close();
|
||||
httpGet.reset();
|
||||
} catch (IOException 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);
|
||||
CloseableHttpResponse res = null;
|
||||
try {
|
||||
StringEntity s = new StringEntity(json, "utf-8");
|
||||
if (StringUtils.isBlank(contentType)) {
|
||||
s.setContentType("application/json");
|
||||
}
|
||||
s.setContentType(contentType);
|
||||
post.setEntity(s);
|
||||
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) {
|
||||
System.out.println("url错误:::"+e.toString());
|
||||
e.printStackTrace();
|
||||
}finally{
|
||||
try {
|
||||
res.close();
|
||||
client.close();
|
||||
post.reset();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
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);
|
||||
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();
|
||||
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
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
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;
|
||||
CloseableHttpClient httpClientc = null;
|
||||
try {
|
||||
if (pairs != null && pairs.size() > 0) {
|
||||
httpPost.setEntity(new UrlEncodedFormEntity(pairs, CHARSET));
|
||||
}
|
||||
|
||||
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) {
|
||||
System.out.println("url错误:::"+e.toString());
|
||||
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";
|
||||
if (StringUtils.isBlank(url)) {
|
||||
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());
|
||||
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 = 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内容
|
||||
httpsClient = HttpUtils.createSSLClientCookie(cookie);
|
||||
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) {
|
||||
System.out.println("url错误:::"+e.toString());
|
||||
e.printStackTrace();
|
||||
}finally{
|
||||
try {
|
||||
response.close();
|
||||
httpsClient.close();
|
||||
httpGet.reset();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -619,6 +619,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();
|
||||
}
|
||||
/**************/
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 将InputStream装换成某种字符编码的String
|
||||
*
|
||||
|
||||
@@ -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};
|
||||
|
||||
Reference in New Issue
Block a user