Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
L
leaf-hlcm
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
hlcm
leaf-hlcm
Commits
8921a523
Commit
8921a523
authored
Mar 23, 2023
by
Luochenglong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
文件上传方法修改
parent
d032d792
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
38 deletions
+43
-38
pom.xml
pom.xml
+2
-23
HttpPostAttUtil.java
src/main/java/com/hand/att/HttpPostAttUtil.java
+41
-15
No files found.
pom.xml
View file @
8921a523
...
...
@@ -221,28 +221,13 @@
<artifactId>
spring-test
</artifactId>
<version>
5.3.14
</version>
</dependency>
<dependency>
<groupId>
com.alibaba.fastjson2
</groupId>
<artifactId>
fastjson2
</artifactId>
<version>
2.0.23
</version>
</dependency>
<dependency>
<groupId>
commons-codec
</groupId>
<artifactId>
commons-codec
</artifactId>
<version>
1.15
</version>
</dependency>
<dependency>
<groupId>
org.apache.httpcomponents
</groupId>
<artifactId>
httpclient
</artifactId>
<version>
4.3.5
</version>
<version>
1.10
</version>
</dependency>
<dependency>
<groupId>
org.apache.httpcomponents
</groupId>
<artifactId>
httpcore
</artifactId>
<version>
4.4.4
</version>
</dependency>
<dependency>
<groupId>
org.apache.httpcomponents
</groupId>
...
...
@@ -250,12 +235,6 @@
<version>
4.5.2
</version>
</dependency>
<dependency>
<groupId>
commons-logging
</groupId>
<artifactId>
commons-logging
</artifactId>
<version>
1.2
</version>
</dependency>
</dependencies>
<build>
...
...
src/main/java/com/hand/att/HttpPostAttUtil.java
View file @
8921a523
...
...
@@ -6,28 +6,22 @@ import org.apache.commons.httpclient.HttpMethod;
import
org.apache.commons.httpclient.methods.PostMethod
;
import
org.apache.commons.httpclient.methods.RequestEntity
;
import
org.apache.commons.httpclient.methods.StringRequestEntity
;
import
org.apache.commons.httpclient.methods.multipart.FilePart
;
import
org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity
;
import
org.apache.http.HttpEntity
;
import
org.apache.http.HttpResponse
;
import
org.apache.http.client.methods.HttpPost
;
import
org.apache.http.entity.mime.MultipartEntityBuilder
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.apache.http.impl.client.HttpClients
;
import
org.apache.http.util.EntityUtils
;
import
org.springframework.http.HttpHeaders
;
import
org.apache.commons.httpclient.methods.multipart.Part
;
import
org.apache.commons.httpclient.methods.multipart.StringPart
;
import
org.apache.commons.httpclient.params.HttpMethodParams
;
import
org.springframework.web.client.RestTemplate
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.File
;
import
java.util.HashMap
;
import
java.util.Iterator
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.*
;
/**
* description
...
...
@@ -69,7 +63,7 @@ public class HttpPostAttUtil {
public
static
String
UploadFile
(
String
url
,
Map
<
String
,
String
>
param
,
File
file
,
String
token
,
String
tokentype
)
throws
Exception
{
/*
public static String UploadFile(String url, Map<String, String> param, File file, String token, String tokentype ) throws Exception {
try {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append( url)
...
...
@@ -92,7 +86,7 @@ public class HttpPostAttUtil {
HttpPost httpPost = new HttpPost(post_url);
MultipartEntityBuilder builder=MultipartEntityBuilder.create();
builder.addBinaryBody("file",file);
HttpEntity
httpEntity
=
(
HttpEntity
)
builder
.
build
();
HttpEntity httpEntity=builder.build();
httpPost.setHeader("Authorization", tokentype+" "+token);
httpPost.setEntity(httpEntity);//设置请求参数
HttpResponse response = httpClient.execute(httpPost);
...
...
@@ -102,6 +96,38 @@ public class HttpPostAttUtil {
} catch (Exception ex) {
return ex.getMessage() ;
}
}*/
public
static
String
UploadFile
(
String
url
,
Map
<
String
,
String
>
param
,
File
file
,
String
token
,
String
tokentype
)
throws
Exception
{
HttpClient
httpClient
=
new
HttpClient
();
HttpServletResponse
response
=
null
;
PostMethod
post
=
new
PostMethod
(
url
);
String
resultString
=
""
;
List
<
Part
>
list
=
new
ArrayList
<>();
httpClient
.
getHttpConnectionManager
().
getParams
().
setConnectionTimeout
(
5000
);
try
{
list
.
add
(
new
FilePart
(
"file"
,
file
));
if
(
param
!=
null
)
{
for
(
String
key
:
param
.
keySet
())
{
list
.
add
(
new
StringPart
(
key
,
param
.
get
(
key
),
"UTF-8"
));
}
}
Part
[]
parts
=
list
.
toArray
(
new
Part
[
list
.
size
()]);
MultipartRequestEntity
entity
=
new
MultipartRequestEntity
(
parts
,
new
HttpMethodParams
());
post
.
setRequestEntity
(
entity
);
post
.
addRequestHeader
(
"Authorization"
,
tokentype
+
" "
+
token
);
System
.
out
.
println
(
entity
.
getContentType
());
int
status
=
httpClient
.
executeMethod
(
post
);
String
result
=
post
.
getResponseBodyAsString
()
;
System
.
out
.
println
(
result
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
post
.
releaseConnection
();
}
return
""
;
}
...
...
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