parent
856275d3c8
commit
bfab299485
File diff suppressed because it is too large
Load Diff
@ -1,222 +0,0 @@
|
||||
package com.wmyun.farmwork.word.wordx;
|
||||
|
||||
import cn.hutool.log.Log;
|
||||
import cn.hutool.log.LogFactory;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Description: TODO
|
||||
* @Date: 2025/3/6 18:07
|
||||
* @Created: by ZZSLL
|
||||
*/
|
||||
|
||||
public class HttpUtils {
|
||||
|
||||
private static final Log log = LogFactory.get(HttpUtils.class);
|
||||
|
||||
public static class SimpleResponse {
|
||||
public byte[] data;
|
||||
public Map<String, List<String>> header;
|
||||
|
||||
public String getHead(String name) {
|
||||
if (header != null && header.containsKey(name)) {
|
||||
return header.get(name).get(0);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String doGet(String url, HashMap<String, String> headerMap) {
|
||||
SimpleResponse rsp = doGetBase(url, headerMap);
|
||||
if (rsp.data != null) {
|
||||
String result = new String(rsp.data);
|
||||
return result;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 向指定URL发送GET方法的请求
|
||||
*
|
||||
* @param url
|
||||
* 发送请求的URL
|
||||
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
||||
* @return URL 所代表远程资源的响应结果
|
||||
*/
|
||||
public static SimpleResponse doGetBase(String url, HashMap<String, String> headerMap) {
|
||||
SimpleResponse result = new SimpleResponse();
|
||||
byte[] data = null;
|
||||
InputStream is = null;
|
||||
try {
|
||||
String urlNameString = url;
|
||||
URL realUrl = new URL(urlNameString);
|
||||
// 打开和URL之间的连接
|
||||
HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
|
||||
// 设置通用的请求属性
|
||||
connection.setRequestProperty("accept", "*/*");
|
||||
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||||
if (headerMap != null) {
|
||||
for (Map.Entry<String, String> e : headerMap.entrySet()) {
|
||||
connection.setRequestProperty(e.getKey(), e.getValue());
|
||||
}
|
||||
}
|
||||
// 建立实际的连接
|
||||
connection.connect();
|
||||
// 获取所有响应头字段
|
||||
Map<String, List<String>> map = connection.getHeaderFields();
|
||||
result.header = map;
|
||||
// 遍历所有的响应头字段
|
||||
if (connection.getResponseCode() != 200) {
|
||||
for (String key : map.keySet()) {
|
||||
log.info(key + "--->" + map.get(key));
|
||||
}
|
||||
}
|
||||
if (connection.getResponseCode() == 200) {
|
||||
if (map.containsKey("Content-Length")) {
|
||||
int len = Integer.parseInt(map.get("Content-Length").get(0));
|
||||
data = new byte[len];
|
||||
int offset = 0;
|
||||
is = connection.getInputStream();
|
||||
while (true) {
|
||||
int len2 = is.read(data, offset, len - offset);
|
||||
if (len2 == -1 || offset + len2 >= len) {
|
||||
break;
|
||||
}
|
||||
offset += len2;
|
||||
}
|
||||
result.data = data;
|
||||
} else if ("chunked".equals(map.get("Transfer-Encoding").get(0))) {
|
||||
data = chunked(connection.getInputStream());
|
||||
result.data = data;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
U.out("访问失败:%s", url);
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
try {
|
||||
if (is != null) {
|
||||
is.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 向指定 URL 发送POST方法的请求
|
||||
*
|
||||
* @param url
|
||||
* 发送请求的 URL
|
||||
* @param param
|
||||
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
|
||||
* @return 所代表远程资源的响应结果
|
||||
*/
|
||||
public static String doPost(String url, HashMap<String, String> header, String param) {
|
||||
PrintWriter out = null;
|
||||
BufferedReader in = null;
|
||||
String result = "";
|
||||
try {
|
||||
URL realUrl = new URL(url);
|
||||
// 打开和URL之间的连接
|
||||
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
|
||||
// 设置通用的请求属性
|
||||
conn.setRequestProperty("accept", "*/*");
|
||||
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
|
||||
if (header != null && !header.isEmpty()) {
|
||||
for (Map.Entry<String, String> entry : header.entrySet()) {
|
||||
conn.setRequestProperty(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
// 发送POST请求必须设置如下两行
|
||||
conn.setDoOutput(true);
|
||||
conn.setDoInput(true);
|
||||
// 获取URLConnection对象对应的输出流
|
||||
out = new PrintWriter(conn.getOutputStream());
|
||||
// 发送请求参数
|
||||
out.print(param);
|
||||
// flush输出流的缓冲
|
||||
out.flush();
|
||||
//
|
||||
if (conn.getResponseCode() != 200) {
|
||||
U.out("响应code=%d", conn.getResponseCode());
|
||||
return null;
|
||||
}
|
||||
// 定义BufferedReader输入流来读取URL的响应
|
||||
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
||||
String line;
|
||||
while ((line = in.readLine()) != null) {
|
||||
result += line;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
// 使用finally块来关闭输出流、输入流
|
||||
finally {
|
||||
try {
|
||||
if (out != null) {
|
||||
out.close();
|
||||
}
|
||||
if (in != null) {
|
||||
in.close();
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static byte[] chunked(InputStream in) throws Exception {
|
||||
ByteArrayOutputStream tmpos = new ByteArrayOutputStream(4);
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
int data = -1;
|
||||
int[] aaa = new int[2];
|
||||
byte[] aa = null;
|
||||
|
||||
while ((data = in.read()) >= 0) {
|
||||
aaa[0] = aaa[1];
|
||||
aaa[1] = data;
|
||||
if (aaa[0] == 13 && aaa[1] == 10) {
|
||||
aa = tmpos.toByteArray();
|
||||
int num = 0;
|
||||
try {
|
||||
num = Integer.parseInt(new String(aa, 0, aa.length - 1)
|
||||
.trim(), 16);
|
||||
} catch (Exception e) {
|
||||
log.info("aa.length:" + aa.length);
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if (num == 0) {
|
||||
|
||||
in.read();
|
||||
in.read();
|
||||
return bytes.toByteArray();
|
||||
}
|
||||
aa = new byte[num];
|
||||
int sj = 0, ydlen = num, ksind = 0;
|
||||
while ((sj = (in.read(aa, ksind, ydlen))) < ydlen) {
|
||||
ydlen -= sj;
|
||||
ksind += sj;
|
||||
}
|
||||
|
||||
bytes.write(aa);
|
||||
in.read();
|
||||
in.read();
|
||||
tmpos.reset();
|
||||
} else {
|
||||
tmpos.write(data);
|
||||
}
|
||||
}
|
||||
return tmpos.toByteArray();
|
||||
}
|
||||
}
|
@ -1,304 +0,0 @@
|
||||
package com.wmyun.farmwork.word.wordx;
|
||||
|
||||
import cn.hutool.log.Log;
|
||||
import cn.hutool.log.LogFactory;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Method;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
public class U {
|
||||
private static final SimpleDateFormat fDate = new SimpleDateFormat("yyyy-MM-dd");
|
||||
|
||||
private static final Log logger = LogFactory.get(U.class);
|
||||
|
||||
public static String formatDate(Date date) {
|
||||
if (date == null) {
|
||||
return "";
|
||||
}
|
||||
return fDate.format(date);
|
||||
}
|
||||
|
||||
public static boolean isNullOrEmpty(String... str) {
|
||||
String s;
|
||||
for (String string : str) {
|
||||
s = string;
|
||||
if (s == null || s.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void out(String format, Object... args) {
|
||||
log(format, args);
|
||||
}
|
||||
|
||||
public static String toFirstLowerCase(String s) {
|
||||
if (U.isNullOrEmpty(s)) {
|
||||
return "";
|
||||
}
|
||||
String s2;
|
||||
if (s.length() > 1) {
|
||||
s2 = (s.charAt(0) + "").toLowerCase() + s.substring(1);
|
||||
} else {
|
||||
s2 = s.toLowerCase();
|
||||
}
|
||||
return s2;
|
||||
}
|
||||
|
||||
public static String toFirstUpperCase(String s) {
|
||||
if (U.isNullOrEmpty(s)) {
|
||||
return "";
|
||||
}
|
||||
String s2;
|
||||
if (s.length() > 1) {
|
||||
s2 = (s.charAt(0) + "").toUpperCase() + s.substring(1);
|
||||
} else {
|
||||
s2 = s.toUpperCase();
|
||||
}
|
||||
return s2;
|
||||
}
|
||||
|
||||
public static String padding0(int code, int len) {
|
||||
return String.format("%0" + len + "d", code);
|
||||
}
|
||||
|
||||
public static void close(Object... objects) {
|
||||
for (Object obj : objects) {
|
||||
if (obj != null) {
|
||||
Class<?> c = obj.getClass();
|
||||
try {
|
||||
Method method = c.getMethod("close");
|
||||
method.invoke(obj);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static int length(String s) {
|
||||
if (U.isNullOrEmpty(s)) {
|
||||
return 0;
|
||||
}
|
||||
int count = 0;
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char c = s.charAt(i);
|
||||
if (c <= 255) {
|
||||
count++;
|
||||
} else {
|
||||
count += 2;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public static String stringCut(String s, int len) {
|
||||
if (U.isNullOrEmpty(s)) {
|
||||
return "";
|
||||
}
|
||||
int count = 0;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char c = s.charAt(i);
|
||||
int w = c <= 255 ? 1 : 2;
|
||||
count += w;
|
||||
if (count > len) {
|
||||
return sb.toString();
|
||||
}
|
||||
sb.append(c);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static String paddingLeft(String s, String sign, int len) {
|
||||
while (s.length() < len) {
|
||||
s = sign + s;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
public static String paddingRight(String s, String sign, int len) {
|
||||
while (s.length() < len) {
|
||||
s = s + sign;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
// 2006-01-02 15:04:05 009
|
||||
private static Pattern regDateFormat = Pattern.compile("(20)?06|01|02|15|04|05|0*9");
|
||||
|
||||
public static String parseDateFormat(String format, int number) {
|
||||
Date date = new Date();
|
||||
return parseDateFormat(format, number, date);
|
||||
}
|
||||
|
||||
public static String parseDateFormat(String format, int number, Date date) {
|
||||
Calendar cal = Calendar.getInstance();
|
||||
if (date != null) {
|
||||
cal.setTime(date);
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int code;
|
||||
Matcher match = regDateFormat.matcher(format);
|
||||
int p = format.length();
|
||||
if (match.find()) {
|
||||
p = match.start();
|
||||
}
|
||||
for (int i = 0; i < format.length(); i++) {
|
||||
if (i == p) {
|
||||
i = match.end() - 1;
|
||||
String s = match.group();
|
||||
switch (s) {
|
||||
case "2006":
|
||||
code = cal.get(Calendar.YEAR);
|
||||
sb.append(code);
|
||||
break;
|
||||
case "06":
|
||||
code = cal.get(Calendar.YEAR);
|
||||
sb.append(code % 100);
|
||||
break;
|
||||
case "01":
|
||||
code = cal.get(Calendar.MONTH) + 1;
|
||||
sb.append(U.padding0(code, 2));
|
||||
break;
|
||||
case "02":
|
||||
code = cal.get(Calendar.DATE);
|
||||
sb.append(U.padding0(code, 2));
|
||||
break;
|
||||
case "15":
|
||||
code = cal.get(Calendar.HOUR_OF_DAY);
|
||||
sb.append(U.padding0(code, 2));
|
||||
break;
|
||||
case "04":
|
||||
code = cal.get(Calendar.MINUTE);
|
||||
sb.append(U.padding0(code, 2));
|
||||
break;
|
||||
case "05":
|
||||
code = cal.get(Calendar.SECOND);
|
||||
sb.append(U.padding0(code, 2));
|
||||
break;
|
||||
default:
|
||||
if (Pattern.matches("^0*9$", s)) {
|
||||
sb.append(U.padding0(number, s.length()));
|
||||
} else {
|
||||
U.out("奇怪format=%s, s=%s", format, s);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
char c = format.charAt(i);
|
||||
switch (c) {
|
||||
case '\\':
|
||||
if (i + 1 < format.length()) {
|
||||
i++;
|
||||
sb.append(format.charAt(i));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
if (i >= p) {
|
||||
if (match.find()) {
|
||||
p = match.start();
|
||||
} else {
|
||||
p = format.length();
|
||||
}
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static LinkedHashMap<String, byte[]> unzip(File file) {
|
||||
FileInputStream is;
|
||||
try {
|
||||
is = new FileInputStream(file);
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return unzip(is);
|
||||
}
|
||||
|
||||
public static LinkedHashMap<String, byte[]> unzip(InputStream is) {
|
||||
try {
|
||||
LinkedHashMap<String, byte[]> map = new LinkedHashMap<>();
|
||||
ZipInputStream zis = new ZipInputStream(is);
|
||||
ZipEntry entry = null;
|
||||
while ((entry = zis.getNextEntry()) != null) {
|
||||
if (entry.isDirectory()) {
|
||||
map.put(entry.getName(), null);
|
||||
} else {
|
||||
|
||||
byte[] data = new byte[(int) entry.getSize()];
|
||||
int count = 0;
|
||||
while (count < entry.getSize()) {
|
||||
int len = zis.read(data, count, data.length - count);
|
||||
if (len == -1) {
|
||||
break;
|
||||
}
|
||||
count += len;
|
||||
}
|
||||
zis.closeEntry();
|
||||
map.put(entry.getName(), data);
|
||||
}
|
||||
}
|
||||
zis.close();
|
||||
is.close();
|
||||
return map;
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void zip(File file, LinkedHashMap<String, byte[]> map) {
|
||||
FileOutputStream os;
|
||||
try {
|
||||
os = new FileOutputStream(file);
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
zip(os, map);
|
||||
}
|
||||
|
||||
public static void zip(OutputStream os, LinkedHashMap<String, byte[]> map) {
|
||||
try {
|
||||
ZipOutputStream zos = new ZipOutputStream(os);
|
||||
for (Entry<String, byte[]> pair : map.entrySet()) {
|
||||
ZipEntry entry = new ZipEntry(pair.getKey());
|
||||
zos.putNextEntry(entry);
|
||||
if (pair.getValue() != null) {
|
||||
zos.write(pair.getValue());
|
||||
}
|
||||
}
|
||||
zos.close();
|
||||
os.close();
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void log(String format, Object... args) {
|
||||
logger.info(String.format(format, args));
|
||||
}
|
||||
|
||||
public static void logError(String format, Object... args) {
|
||||
log(format, args);
|
||||
}
|
||||
|
||||
public static String toJSON(Object obj) {
|
||||
if (obj == null) {
|
||||
return null;
|
||||
}
|
||||
return obj.toString();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user