Commit 355b214f authored by yanzg's avatar yanzg

修改地址

parent f13538e8
......@@ -36,7 +36,7 @@ public class HttpHelper {
* 获取请求参数
*
* @param obj 对象
* @return
* @return 返回地址
*/
public static String getUrlParameter(Object obj) {
return getUrlParameter(obj, "UTF-8");
......@@ -108,9 +108,9 @@ public class HttpHelper {
*/
public static HttpURLConnection getConn(String url, Map<String, String> header, boolean isApplicationJson) throws IOException {
// 创建URL对象
java.net.URL connURL = new java.net.URL(url);
java.net.URL connUrl = new java.net.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("Connection", "Keep-Alive");
......@@ -169,7 +169,6 @@ public class HttpHelper {
try {
// 打开URL连接
java.net.HttpURLConnection httpConn = getConn(url);
httpConn.setRequestMethod("GET");
return get(httpConn, charset);
} catch (Exception ex) {
throw new RuntimeException(ex);
......@@ -213,19 +212,18 @@ public class HttpHelper {
* @return 远程响应结果
*/
public static String post(HttpURLConnection httpConn, String jsonString, String charset) throws IOException {
httpConn.setRequestMethod("POST");
// 返回的结果
StringBuilder result = new StringBuilder();
// 读取响应输入流
BufferedReader in = null;
PrintWriter out = null;
// 处理请求参数
String params = "";
try {
params = jsonString;
// 获取HttpURLConnection对象对应的输出流
out = new PrintWriter(httpConn.getOutputStream());
// 发送请求参数
out.write(params);
out.write(jsonString);
// flush输出流的缓冲
out.flush();
......@@ -268,6 +266,7 @@ public class HttpHelper {
* @return 远程响应结果
*/
public static String get(HttpURLConnection httpConn, String charset) throws IOException {
httpConn.setRequestMethod("GET");
// 返回的结果
StringBuilder result = new StringBuilder();
// 读取响应输入流
......@@ -314,7 +313,7 @@ public class HttpHelper {
*
* @param serverFileName 下载的服务器文件路径
* @param localFileName 保存的文件路径
* @throws IOException
* @throws IOException IO异常
*/
public static void downToLocal(String serverFileName, String localFileName) throws IOException {
downToLocal(serverFileName, localFileName, null);
......@@ -326,7 +325,7 @@ public class HttpHelper {
* @param serverFileName 下载的服务器文件路径
* @param localFileName 保存的文件路径
* @param runProcess 进度处理
* @throws IOException
* @throws IOException IO异常
*/
public static void downToLocal(String serverFileName, String localFileName, RunProcess runProcess) throws IOException {
int byteRead;
......@@ -334,21 +333,15 @@ public class HttpHelper {
URLConnection conn = url.openConnection();
conn.setConnectTimeout(HTTP_CONNECT_TIMEOUT);
conn.setReadTimeout(HTTP_READ_TIMEOUT);
InputStream inStream = conn.getInputStream();
try {
try (InputStream inStream = conn.getInputStream()) {
ProcessData data = new ProcessData(serverFileName, conn.getContentLengthLong());
FileOutputStream outputStream = new FileOutputStream(localFileName);
try {
try (FileOutputStream outputStream = new FileOutputStream(localFileName)) {
byte[] buffer = new byte[1204];
while ((byteRead = inStream.read(buffer)) != -1) {
data.processAdd(runProcess, 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