Commit 32be2fb6 authored by yanzg's avatar yanzg

Merge branch 'ver1.1' of http://192.168.0.204/yzg/yzg-util

parents 3c1caee8 355b214f
...@@ -36,7 +36,7 @@ public class HttpHelper { ...@@ -36,7 +36,7 @@ public class HttpHelper {
* 获取请求参数 * 获取请求参数
* *
* @param obj 对象 * @param obj 对象
* @return * @return 返回地址
*/ */
public static String getUrlParameter(Object obj) { public static String getUrlParameter(Object obj) {
return getUrlParameter(obj, "UTF-8"); return getUrlParameter(obj, "UTF-8");
...@@ -108,9 +108,9 @@ public class HttpHelper { ...@@ -108,9 +108,9 @@ public class HttpHelper {
*/ */
public static HttpURLConnection getConn(String url, Map<String, String> header, boolean isApplicationJson) throws IOException { public static HttpURLConnection getConn(String url, Map<String, String> header, boolean isApplicationJson) throws IOException {
// 创建URL对象 // 创建URL对象
java.net.URL connURL = new java.net.URL(url); java.net.URL connUrl = new java.net.URL(url);
// 打开URL连接 // 打开URL连接
java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) connURL.openConnection(); java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) connUrl.openConnection();
// 设置通用属性 // 设置通用属性
httpConn.setRequestProperty("Accept", "*/*"); httpConn.setRequestProperty("Accept", "*/*");
httpConn.setRequestProperty("Connection", "Keep-Alive"); httpConn.setRequestProperty("Connection", "Keep-Alive");
...@@ -169,7 +169,6 @@ public class HttpHelper { ...@@ -169,7 +169,6 @@ public class HttpHelper {
try { try {
// 打开URL连接 // 打开URL连接
java.net.HttpURLConnection httpConn = getConn(url); java.net.HttpURLConnection httpConn = getConn(url);
httpConn.setRequestMethod("GET");
return get(httpConn, charset); return get(httpConn, charset);
} catch (Exception ex) { } catch (Exception ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
...@@ -224,19 +223,18 @@ public class HttpHelper { ...@@ -224,19 +223,18 @@ public class HttpHelper {
* @return 远程响应结果 * @return 远程响应结果
*/ */
public static String post(HttpURLConnection httpConn, String jsonString, String charset) throws IOException { public static String post(HttpURLConnection httpConn, String jsonString, String charset) throws IOException {
httpConn.setRequestMethod("POST");
// 返回的结果 // 返回的结果
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
// 读取响应输入流 // 读取响应输入流
BufferedReader in = null; BufferedReader in = null;
PrintWriter out = null; PrintWriter out = null;
// 处理请求参数 // 处理请求参数
String params = "";
try { try {
params = jsonString;
// 获取HttpURLConnection对象对应的输出流 // 获取HttpURLConnection对象对应的输出流
out = new PrintWriter(httpConn.getOutputStream()); out = new PrintWriter(httpConn.getOutputStream());
// 发送请求参数 // 发送请求参数
out.write(params); out.write(jsonString);
// flush输出流的缓冲 // flush输出流的缓冲
out.flush(); out.flush();
...@@ -320,6 +318,7 @@ public class HttpHelper { ...@@ -320,6 +318,7 @@ public class HttpHelper {
* @return 远程响应结果 * @return 远程响应结果
*/ */
public static String get(HttpURLConnection httpConn, String charset) throws IOException { public static String get(HttpURLConnection httpConn, String charset) throws IOException {
httpConn.setRequestMethod("GET");
// 返回的结果 // 返回的结果
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
// 读取响应输入流 // 读取响应输入流
...@@ -383,7 +382,7 @@ public class HttpHelper { ...@@ -383,7 +382,7 @@ public class HttpHelper {
* *
* @param serverFileName 下载的服务器文件路径 * @param serverFileName 下载的服务器文件路径
* @param localFileName 保存的文件路径 * @param localFileName 保存的文件路径
* @throws IOException * @throws IOException IO异常
*/ */
public static void downToLocal(String serverFileName, String localFileName) throws IOException { public static void downToLocal(String serverFileName, String localFileName) throws IOException {
downToLocal(serverFileName, localFileName, null); downToLocal(serverFileName, localFileName, null);
...@@ -395,7 +394,7 @@ public class HttpHelper { ...@@ -395,7 +394,7 @@ public class HttpHelper {
* @param serverFileName 下载的服务器文件路径 * @param serverFileName 下载的服务器文件路径
* @param localFileName 保存的文件路径 * @param localFileName 保存的文件路径
* @param runProcess 进度处理 * @param runProcess 进度处理
* @throws IOException * @throws IOException IO异常
*/ */
public static void downToLocal(String serverFileName, String localFileName, RunProcess runProcess) throws IOException { public static void downToLocal(String serverFileName, String localFileName, RunProcess runProcess) throws IOException {
int byteRead; int byteRead;
...@@ -403,21 +402,15 @@ public class HttpHelper { ...@@ -403,21 +402,15 @@ public class HttpHelper {
URLConnection conn = url.openConnection(); URLConnection conn = url.openConnection();
conn.setConnectTimeout(HTTP_CONNECT_TIMEOUT); conn.setConnectTimeout(HTTP_CONNECT_TIMEOUT);
conn.setReadTimeout(HTTP_READ_TIMEOUT); conn.setReadTimeout(HTTP_READ_TIMEOUT);
InputStream inStream = conn.getInputStream(); try (InputStream inStream = conn.getInputStream()) {
try {
ProcessData data = new ProcessData(serverFileName, conn.getContentLengthLong()); ProcessData data = new ProcessData(serverFileName, conn.getContentLengthLong());
FileOutputStream outputStream = new FileOutputStream(localFileName); try (FileOutputStream outputStream = new FileOutputStream(localFileName)) {
try {
byte[] buffer = new byte[1204]; byte[] buffer = new byte[1204];
while ((byteRead = inStream.read(buffer)) != -1) { while ((byteRead = inStream.read(buffer)) != -1) {
data.processAdd(runProcess, byteRead); data.processAdd(runProcess, byteRead);
outputStream.write(buffer, 0, byteRead); outputStream.write(buffer, 0, byteRead);
} }
} finally {
outputStream.close();
} }
} finally {
inStream.close();
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment