|
@@ -4,7 +4,15 @@ import com.alibaba.fastjson.JSON;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
+import java.io.BufferedReader;
|
|
|
|
+import java.io.DataOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
|
+import java.io.InputStreamReader;
|
|
|
|
+import java.net.HttpURLConnection;
|
|
|
|
+import java.net.URL;
|
|
|
|
+import java.net.URLConnection;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
public class WebUtils
|
|
public class WebUtils
|
|
@@ -26,12 +34,18 @@ public class WebUtils
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 将 HH:ii 格式的时间切换到某个时区的时间
|
|
|
|
+ * @param time 时间 HH:ii
|
|
|
|
+ * @param timezone 时区
|
|
|
|
+ * @return 装换时区后的时间 HH:ii
|
|
|
|
+ */
|
|
public static String getDateFormat(String time,int timezone){
|
|
public static String getDateFormat(String time,int timezone){
|
|
String res = "";
|
|
String res = "";
|
|
try {
|
|
try {
|
|
String[] split = time.split(":");
|
|
String[] split = time.split(":");
|
|
- Integer h = Integer.parseInt(split[0]);
|
|
|
|
- Integer i = Integer.parseInt(split[1]);
|
|
|
|
|
|
+ int h = Integer.parseInt(split[0]);
|
|
|
|
+ int i = Integer.parseInt(split[1]);
|
|
if (h - timezone < 0) {
|
|
if (h - timezone < 0) {
|
|
h = h - timezone + 24;
|
|
h = h - timezone + 24;
|
|
}else if(h - timezone >= 24){
|
|
}else if(h - timezone >= 24){
|
|
@@ -46,4 +60,94 @@ public class WebUtils
|
|
}
|
|
}
|
|
return res;
|
|
return res;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 模拟post请求
|
|
|
|
+ * @param url 请求地址
|
|
|
|
+ * @param param 请求参数
|
|
|
|
+ * @return 请求返回信息
|
|
|
|
+ * @throws Exception
|
|
|
|
+ */
|
|
|
|
+ public static String requestPost(String url, String param) {
|
|
|
|
+ StringBuilder response;
|
|
|
|
+ try {
|
|
|
|
+ URL obj = new URL(url);
|
|
|
|
+ HttpURLConnection con = (HttpURLConnection) obj.openConnection();
|
|
|
|
+ con.setRequestMethod("POST");
|
|
|
|
+ con.setRequestProperty("User-Agent", "Mozilla/5.0");
|
|
|
|
+ con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
|
|
|
|
+
|
|
|
|
+ con.setDoOutput(true);
|
|
|
|
+ con.setReadTimeout(5000);
|
|
|
|
+ DataOutputStream wr = new DataOutputStream(con.getOutputStream());
|
|
|
|
+ wr.writeBytes(param);
|
|
|
|
+ wr.flush();
|
|
|
|
+ wr.close();
|
|
|
|
+
|
|
|
|
+ BufferedReader in = new BufferedReader(
|
|
|
|
+ new InputStreamReader(con.getInputStream()));
|
|
|
|
+ String inputLine;
|
|
|
|
+ response = new StringBuilder();
|
|
|
|
+
|
|
|
|
+ while ((inputLine = in.readLine()) != null) {
|
|
|
|
+ response.append(inputLine);
|
|
|
|
+ }
|
|
|
|
+ in.close();
|
|
|
|
+ }catch (Exception e){
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+ return response.toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 向指定URL发送GET方法的请求
|
|
|
|
+ *
|
|
|
|
+ * @param url 发送请求的URL
|
|
|
|
+ * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
|
|
|
+ * @return URL 所代表远程资源的响应结果
|
|
|
|
+ */
|
|
|
|
+ public static String requestGet(String url, String param) {
|
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
|
+ BufferedReader in = null;
|
|
|
|
+ try {
|
|
|
|
+ String urlNameString = url + "?" + param;
|
|
|
|
+ URL realUrl = new URL(urlNameString);
|
|
|
|
+ // 打开和URL之间的连接
|
|
|
|
+ URLConnection connection = realUrl.openConnection();
|
|
|
|
+ // 设置通用的请求属性
|
|
|
|
+ connection.setRequestProperty("accept", "*/*");
|
|
|
|
+ connection.setRequestProperty("connection", "Keep-Alive");
|
|
|
|
+ connection.setRequestProperty("user-agent",
|
|
|
|
+ "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
|
|
|
+ // 建立实际的连接
|
|
|
|
+ connection.connect();
|
|
|
|
+ // 获取所有响应头字段
|
|
|
|
+ Map<String, List<String>> map = connection.getHeaderFields();
|
|
|
|
+ // 遍历所有的响应头字段
|
|
|
|
+ for (String key : map.keySet()) {
|
|
|
|
+ System.out.println(key + "--->" + map.get(key));
|
|
|
|
+ }
|
|
|
|
+ // 定义 BufferedReader输入流来读取URL的响应
|
|
|
|
+ in = new BufferedReader(new InputStreamReader(
|
|
|
|
+ connection.getInputStream()));
|
|
|
|
+ String line;
|
|
|
|
+ while ((line = in.readLine()) != null) {
|
|
|
|
+ result.append(line);
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ System.out.println("发送GET请求出现异常!" + e);
|
|
|
|
+// e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ // 使用finally块来关闭输入流
|
|
|
|
+ finally {
|
|
|
|
+ try {
|
|
|
|
+ if (in != null) {
|
|
|
|
+ in.close();
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e2) {
|
|
|
|
+// e2.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return result.toString();
|
|
|
|
+ }
|
|
}
|
|
}
|