Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
X
xzxt-app-report
Overview
Overview
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
Administrator
xzxt-app-report
Commits
18377e84
Commit
18377e84
authored
Jan 15, 2024
by
wang_jiaxing
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
统一访问接口
parent
183cbd4a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
137 additions
and
0 deletions
+137
-0
src/main/java/org/springblade/founder/forward/controller/ForwardController.java
+40
-0
src/main/java/org/springblade/founder/utils/HttpUtil.java
+97
-0
No files found.
src/main/java/org/springblade/founder/forward/controller/ForwardController.java
View file @
18377e84
package
org
.
springblade
.
founder
.
forward
.
controller
;
import
com.alibaba.fastjson.JSONObject
;
import
org.apache.commons.io.IOUtils
;
import
org.apache.commons.lang.StringUtils
;
import
org.springblade.founder.log.LogOper
;
import
org.springblade.founder.utils.HttpUtil
;
import
org.springblade.founder.utils.R
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
org.springframework.web.context.request.RequestContextHolder
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
org.springframework.web.multipart.MultipartFile
;
import
javax.servlet.ServletOutputStream
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
import
java.net.URLEncoder
;
import
java.nio.charset.StandardCharsets
;
import
java.util.Enumeration
;
import
java.util.HashMap
;
...
...
@@ -101,4 +109,36 @@ public class ForwardController {
}
return
R
.
error
(
"无法执行"
);
}
@RequestMapping
(
"/fileMethod"
)
public
R
fileMethod
(
HttpServletRequest
request
,
MultipartFile
file
)
{
try
{
String
url
=
request
.
getHeader
(
"url"
);
if
(
StringUtils
.
isEmpty
(
url
))
{
return
R
.
error
(
"url不能为空"
);
}
HashMap
<
String
,
String
>
headers
=
new
HashMap
<>();
Enumeration
<
String
>
headerNames
=
request
.
getHeaderNames
();
while
(
headerNames
.
hasMoreElements
())
{
String
element
=
headerNames
.
nextElement
();
if
(
"blade-auth"
.
equals
(
element
)
||
"authorization"
.
equals
(
element
))
{
String
header
=
request
.
getHeader
(
element
);
headers
.
put
(
element
,
header
);
}
}
Map
<
String
,
String
[]>
map
=
request
.
getParameterMap
();
HashMap
<
String
,
String
>
params
=
new
HashMap
<>();
for
(
String
key
:
map
.
keySet
())
{
String
[]
strings
=
map
.
get
(
key
);
params
.
put
(
key
,
strings
[
0
]);
}
String
s
=
HttpUtil
.
uploadFile
(
host
+
url
,
file
,
file
.
getName
(),
headers
,
params
);
return
JSONObject
.
parseObject
(
s
,
R
.
class
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
R
.
error
(
e
.
getMessage
());
}
}
}
src/main/java/org/springblade/founder/utils/HttpUtil.java
View file @
18377e84
...
...
@@ -16,6 +16,8 @@ import org.apache.http.client.utils.URIBuilder;
import
org.apache.http.entity.ContentType
;
import
org.apache.http.entity.InputStreamEntity
;
import
org.apache.http.entity.StringEntity
;
import
org.apache.http.entity.mime.HttpMultipartMode
;
import
org.apache.http.entity.mime.MultipartEntityBuilder
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.apache.http.impl.client.DefaultHttpClient
;
import
org.apache.http.impl.client.HttpClients
;
...
...
@@ -26,9 +28,12 @@ import org.apache.http.params.CoreConnectionPNames;
import
org.apache.http.params.HttpParams
;
import
org.apache.http.util.EntityUtils
;
import
org.apache.log4j.Logger
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.*
;
import
java.net.URI
;
import
java.nio.charset.Charset
;
import
java.nio.charset.StandardCharsets
;
import
java.util.*
;
import
java.util.Map.Entry
;
...
...
@@ -439,4 +444,96 @@ public class HttpUtil {
}
return
jsonResult
;
}
/**
* 使用httpclint 发送文件
*
* @param file 上传的文件
* @return 响应结果
* @author: qingfeng
* @date: 2020/11/23
*/
public
static
String
uploadFile
(
String
url
,
MultipartFile
file
,
String
fileParamName
,
Map
<
String
,
String
>
headerParams
,
Map
<
String
,
String
>
otherParams
)
{
CloseableHttpClient
httpClient
=
HttpClients
.
custom
()
.
setConnectionManager
(
cm
).
build
();
String
result
=
""
;
try
{
String
fileName
=
file
.
getOriginalFilename
();
HttpPost
httpPost
=
new
HttpPost
(
url
);
// 设置请求和传输超时时间
httpPost
.
setConfig
(
requestConfig
);
//添加header
for
(
Map
.
Entry
<
String
,
String
>
e
:
headerParams
.
entrySet
())
{
httpPost
.
addHeader
(
e
.
getKey
(),
e
.
getValue
());
}
MultipartEntityBuilder
builder
=
MultipartEntityBuilder
.
create
();
builder
.
setCharset
(
StandardCharsets
.
UTF_8
);
builder
.
setMode
(
HttpMultipartMode
.
BROWSER_COMPATIBLE
);
//加上此行代码解决返回中文乱码问题
builder
.
addBinaryBody
(
fileParamName
,
file
.
getInputStream
(),
ContentType
.
MULTIPART_FORM_DATA
,
fileName
);
// 文件流
for
(
Map
.
Entry
<
String
,
String
>
e
:
otherParams
.
entrySet
())
{
builder
.
addTextBody
(
e
.
getKey
(),
e
.
getValue
());
// 类似浏览器表单提交,对应input的name和value
}
HttpEntity
entity
=
builder
.
build
();
httpPost
.
setEntity
(
entity
);
HttpResponse
response
=
httpClient
.
execute
(
httpPost
);
// 执行提交
HttpEntity
responseEntity
=
response
.
getEntity
();
if
(
responseEntity
!=
null
)
{
// 将响应内容转换为字符串
result
=
EntityUtils
.
toString
(
responseEntity
,
StandardCharsets
.
UTF_8
);
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
try
{
httpClient
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
return
result
;
}
public
static
InputStream
doGetFile
(
String
url
,
Map
<
String
,
String
>
headMap
,
Map
<
String
,
String
>
param
)
{
// 创建客户端连接
CloseableHttpClient
httpclient
=
HttpClients
.
createDefault
();
InputStream
resultString
=
null
;
CloseableHttpResponse
response
=
null
;
try
{
// 创建uri
URIBuilder
builder
=
new
URIBuilder
(
url
);
if
(
param
!=
null
&&
param
.
size
()>
0
)
{
for
(
String
key
:
param
.
keySet
())
{
builder
.
addParameter
(
key
,
param
.
get
(
key
));
}
}
URI
uri
=
builder
.
build
();
// 创建http GET请求
HttpGet
httpGet
=
new
HttpGet
(
uri
);
if
(
headMap
!=
null
&&
!
headMap
.
isEmpty
())
{
for
(
String
key
:
headMap
.
keySet
())
{
httpGet
.
addHeader
(
key
,
headMap
.
get
(
key
));
}
}
// 执行请求
response
=
httpclient
.
execute
(
httpGet
);
// 判断是否请求成功
if
(
response
.
getStatusLine
().
getStatusCode
()
==
HttpStatus
.
SC_OK
)
{
resultString
=
response
.
getEntity
().
getContent
();
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
try
{
if
(
response
!=
null
)
{
response
.
close
();
}
httpclient
.
close
();
}
catch
(
IOException
e
)
{
}
}
return
resultString
;
}
}
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