Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
YZG
yzg-util
Commits
355b214f
Commit
355b214f
authored
Jan 06, 2023
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改地址
parent
f13538e8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
10 additions
and
17 deletions
+10
-17
HttpHelper.java
...src/main/java/com/yanzuoguang/util/helper/HttpHelper.java
+10
-17
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/HttpHelper.java
View file @
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
connU
RL
=
new
java
.
net
.
URL
(
url
);
java
.
net
.
URL
connU
rl
=
new
java
.
net
.
URL
(
url
);
// 打开URL连接
// 打开URL连接
java
.
net
.
HttpURLConnection
httpConn
=
(
java
.
net
.
HttpURLConnection
)
connU
RL
.
openConnection
();
java
.
net
.
HttpURLConnection
httpConn
=
(
java
.
net
.
HttpURLConnection
)
connU
rl
.
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
);
...
@@ -213,19 +212,18 @@ public class HttpHelper {
...
@@ -213,19 +212,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
();
...
@@ -268,6 +266,7 @@ public class HttpHelper {
...
@@ -268,6 +266,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
();
// 读取响应输入流
// 读取响应输入流
...
@@ -314,7 +313,7 @@ public class HttpHelper {
...
@@ -314,7 +313,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
);
...
@@ -326,7 +325,7 @@ public class HttpHelper {
...
@@ -326,7 +325,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
;
...
@@ -334,21 +333,15 @@ public class HttpHelper {
...
@@ -334,21 +333,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
();
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment