Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
common
Project
Project
Details
Activity
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
曲欣亮
common
Commits
1301d343
Commit
1301d343
authored
Jul 23, 2018
by
张永
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加方法
parent
86965ca6
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
110 additions
and
0 deletions
+110
-0
HttpUtil.java
src/main/java/com/egolm/common/HttpUtil.java
+110
-0
No files found.
src/main/java/com/egolm/common/HttpUtil.java
View file @
1301d343
package
com
.
egolm
.
common
;
import
java.io.ByteArrayOutputStream
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.io.UnsupportedEncodingException
;
...
...
@@ -215,4 +218,111 @@ public class HttpUtil {
return
StringUtil
.
join
(
"&"
,
params
);
}
public
static
String
post
(
String
requestUrl
,
Map
<
String
,
Object
>
parameters
,
Map
<
String
,
String
>
headers
,
Map
<
String
,
File
>
attachments
)
{
HttpURLConnection
connection
=
null
;
try
{
Data
data
=
new
Data
();
String
BOUNDARY
=
"----WebKitFormBoundaryT1HoybnYeFOGFlBR"
;
StringBuffer
ParamBuffer
=
new
StringBuffer
();
if
(
parameters
!=
null
)
{
for
(
String
key
:
parameters
.
keySet
())
{
ParamBuffer
.
append
(
"--"
+
BOUNDARY
+
"\r\n"
);
ParamBuffer
.
append
(
"Content-Disposition: form-data; name=\""
+
key
+
"\"\r\n"
);
ParamBuffer
.
append
(
"\r\n"
);
ParamBuffer
.
append
(
parameters
.
get
(
key
)
+
"\r\n"
);
}
}
ParamBuffer
.
append
(
"--"
+
BOUNDARY
+
"\r\nContent-Disposition: form-data; name=\"----------------------------------\"\r\n\r\n-------------------------\r\n"
);
String
ParamBufferString
=
ParamBuffer
.
toString
();
data
.
add
(
ParamBufferString
.
getBytes
());
if
(
attachments
!=
null
)
{
for
(
String
name
:
attachments
.
keySet
())
{
StringBuffer
FileBuffer
=
new
StringBuffer
();
File
file
=
attachments
.
get
(
name
);
FileBuffer
.
append
(
"--"
+
BOUNDARY
+
"\r\n"
);
FileBuffer
.
append
(
"Content-Disposition: form-data; name=\""
+
name
+
"\"; filename=\""
+
file
.
getName
()
+
"\""
+
"\r\n"
);
FileBuffer
.
append
(
"Content-Type: application/octet-stream"
+
"\r\n"
);
FileBuffer
.
append
(
"\r\n"
);
String
FileBufferString
=
FileBuffer
.
toString
();
data
.
add
(
FileBufferString
.
getBytes
());
data
.
add
(
file
);
String
FileEnd
=
"\r\n"
;
data
.
add
(
FileEnd
.
getBytes
());
}
}
StringBuffer
EndBuffer
=
new
StringBuffer
(
"\r\n--"
+
BOUNDARY
+
"--\r\n"
);
String
EndBufferString
=
EndBuffer
.
toString
();
data
.
add
(
EndBufferString
.
getBytes
());
URL
url
=
new
URL
(
requestUrl
);
connection
=
(
HttpURLConnection
)
url
.
openConnection
();
connection
.
setRequestMethod
(
"POST"
);
connection
.
setRequestProperty
(
"Accept-Charset"
,
"utf-8"
);
connection
.
setRequestProperty
(
"Content-Type"
,
"multipart/form-data; boundary="
+
BOUNDARY
);
connection
.
setRequestProperty
(
"Content-Length"
,
String
.
valueOf
(
data
.
length
()));
if
(
headers
!=
null
)
{
for
(
String
key
:
headers
.
keySet
())
{
connection
.
setRequestProperty
(
key
,
headers
.
get
(
key
));
}
}
connection
.
setDoOutput
(
true
);
OutputStream
out
=
connection
.
getOutputStream
();
out
.
write
(
data
.
bytes
());
out
.
close
();
return
responseBody
(
connection
);
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
e
);
}
finally
{
connection
.
disconnect
();
}
}
static
class
Data
{
private
byte
[][]
ds
=
new
byte
[
0
][
0
];
public
void
add
(
File
file
)
throws
IOException
{
FileInputStream
fis
=
new
FileInputStream
(
file
);
ByteArrayOutputStream
bos
=
new
ByteArrayOutputStream
(
1000
);
byte
[]
b
=
new
byte
[
1000
];
int
n
;
while
((
n
=
fis
.
read
(
b
))
!=
-
1
)
{
bos
.
write
(
b
,
0
,
n
);
}
fis
.
close
();
bos
.
close
();
add
(
bos
.
toByteArray
());
}
public
void
add
(
byte
[]
data
)
{
int
length
=
ds
.
length
;
byte
[][]
ds_tmp
=
new
byte
[
length
+
1
][];
for
(
int
i
=
0
;
i
<
length
;
i
++)
{
ds_tmp
[
i
]
=
ds
[
i
];
}
ds_tmp
[
length
]
=
data
;
ds
=
ds_tmp
;
}
public
int
length
()
{
int
length
=
0
;
for
(
byte
[]
b
:
ds
)
{
length
+=
b
.
length
;
}
return
length
;
}
public
byte
[]
bytes
()
{
byte
[]
bytes
=
new
byte
[
length
()];
int
index
=
0
;
for
(
int
i
=
0
;
i
<
ds
.
length
;
i
++)
{
for
(
int
k
=
0
;
k
<
ds
[
i
].
length
;
k
++)
{
bytes
[
index
++]
=
ds
[
i
][
k
];
}
}
return
bytes
;
}
}
}
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