Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
编程小黑猪
Java
提交
39b8628d
J
Java
项目概览
编程小黑猪
/
Java
与 Fork 源项目一致
Fork自
inscode / Java
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
Java
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
39b8628d
编写于
5月 02, 2023
作者:
6
6450ff0512700e0fdb30eae0
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Tue May 2 12:27:00 UTC 2023 inscode
上级
5b0ce43e
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
166 addition
and
0 deletion
+166
-0
demo/AuthHeaderSettingFilter
demo/AuthHeaderSettingFilter
+65
-0
demo/FilterConfig
demo/FilterConfig
+19
-0
demo/HelloController
demo/HelloController
+82
-0
未找到文件。
demo/AuthHeaderSettingFilter
0 → 100644
浏览文件 @
39b8628d
package
com
.
example
.
springdemo
.
aspect
;
import
org
.
apache
.
tomcat
.
util
.
http
.
MimeHeaders
;
import
org
.
springframework
.
util
.
StringUtils
;
import
javax
.
servlet
.*;
import
javax
.
servlet
.
annotation
.
WebFilter
;
import
javax
.
servlet
.
http
.
HttpServletRequest
;
import
java
.
io
.
IOException
;
import
java
.
lang
.
reflect
.
Field
;
import
java
.
util
.
HashMap
;
import
java
.
util
.
Map
;
@
WebFilter
public
class
AuthHeaderSettingFilter
implements
Filter
{
@
Override
public
void
init
(
FilterConfig
filterConfig
)
throws
ServletException
{
Filter
.
super
.
init
(
filterConfig
);
}
@
Override
public
void
doFilter
(
ServletRequest
servletRequest
,
ServletResponse
servletResponse
,
FilterChain
filterChain
)
throws
IOException
,
ServletException
{
HttpServletRequest
req
=
(
HttpServletRequest
)
servletRequest
;
String
type
=
req
.
getContentType
();
if
(
!StringUtils.isEmpty(type)){
Map
<
String
,
String
>
map
=
new
HashMap
<>();
map
.
put
(
"Content-Type"
,
"image/jpeg"
);
modifyHeaders
(
map
,
req
);
}
String
content
=
req
.
getContentType
();
filterChain
.
doFilter
(
servletRequest
,
servletResponse
);
}
@
Override
public
void
destroy
()
{
Filter
.
super
.
destroy
();
}
private
void
modifyHeaders
(
Map
<
String
,
String
>
headerses
,
HttpServletRequest
request
){
if
(
headerses
==
null
||
headerses
.
isEmpty
())
{
return
;
}
Class
<?
extends
HttpServletRequest
>
requestClass
=
request
.
getClass
();
try
{
Field
requestFlied
=
requestClass
.
getDeclaredField
(
"request"
);
requestFlied
.
setAccessible
(
true
);
Object
reqObject
=
requestFlied
.
get
(
request
);
Field
coyoteRequest
=
reqObject
.
getClass
().
getDeclaredField
(
"coyoteRequest"
);
coyoteRequest
.
setAccessible
(
true
);
Object
coyoteObject
=
coyoteRequest
.
get
(
reqObject
);
Field
headers
=
coyoteObject
.
getClass
().
getDeclaredField
(
"headers"
);
headers
.
setAccessible
(
true
);
MimeHeaders
header
=
(
MimeHeaders
)
headers
.
get
(
coyoteObject
);
for
(
Map
.
Entry
<
String
,
String
>
entry
:
headerses
.
entrySet
())
{
header
.
removeHeader
(
entry
.
getKey
());
header
.
addValue
(
entry
.
getKey
()).
setString
(
entry
.
getValue
());
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
demo/FilterConfig
0 → 100644
浏览文件 @
39b8628d
package
com
.
example
.
springdemo
.
config
;
import
com
.
example
.
springdemo
.
aspect
.
AuthHeaderSettingFilter
;
import
org
.
springframework
.
boot
.
web
.
servlet
.
FilterRegistrationBean
;
import
org
.
springframework
.
context
.
annotation
.
Bean
;
import
org
.
springframework
.
context
.
annotation
.
Configuration
;
@
Configuration
public
class
FilterConfig
{
@
Bean
public
FilterRegistrationBean
modifyParametersFilter
()
{
FilterRegistrationBean
registration
=
new
FilterRegistrationBean
();
registration
.
setFilter
(
new
AuthHeaderSettingFilter
());
registration
.
addUrlPatterns
(
"/*"
);
registration
.
setName
(
"authHeaderSettingFilter"
);
registration
.
setOrder
(
1
);
return
registration
;
}
}
\ No newline at end of file
demo/HelloController
0 → 100644
浏览文件 @
39b8628d
package
com
.
example
.
springdemo
.
controller
;
import
com
.
example
.
springdemo
.
bean
.
Car
;
import
lombok
.
extern
.
slf4j
.
Slf4j
;
import
org
.
apache
.
catalina
.
connector
.
Response
;
import
org
.
springframework
.
web
.
bind
.
annotation
.*;
import
javax
.
servlet
.
http
.
HttpServletRequest
;
import
java
.
io
.*;
import
java
.
util
.
Map
;
@
Slf4j
@
RestController
public
class
HelloController
{
final
Car
car
;
public
HelloController
(
Car
car
)
{
this
.
car
=
car
;
}
/**
*
application
/
octet
-
stream
形式上传图片
*
*
@
param
bytes
图片数据
*
@
return
success
*/
@
RequestMapping
(
value
=
"/uploadFile"
,
method
=
RequestMethod
.
POST
)
public
String
getPicFile
(@
RequestBody
byte
[]
bytes
,
HttpServletRequest
request
)
{
log
.
info
(
"this image name is [{}]"
,
request
.
getContentType
());
try
(
FileOutputStream
fileOuputStream
=
new
FileOutputStream
(
"image.jpg"
)){
fileOuputStream
.
write
(
bytes
);
log
.
info
(
"save image success"
);
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
return
"SUCCESS"
;
}
/**
*
image
/
jpeg
形式上传图片
*
*
@
param
bytes
图片字节流
*
@
param
request
请求
*
@
return
200
ok
*/
@
RequestMapping
(
value
=
"/vi/snap/uploadFile"
,
method
=
RequestMethod
.
POST
)
public
int
getImage
(@
RequestBody
byte
[]
bytes
,
HttpServletRequest
request
)
{
log
.
info
(
"ContentType : {}"
,
request
.
getContentType
());
try
(
FileOutputStream
fileOuputStream
=
new
FileOutputStream
(
"snap.jpg"
)){
fileOuputStream
.
write
(
bytes
);
log
.
info
(
"save snap image success"
);
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
return
Response
.
SC_OK
;
}
/**
*
hashMap
转化成表单字符串
*
*
@
param
map
图片数据
*
@
return
字符串
*/
public
static
String
map2Form
(
Map
<
String
,
Object
>
map
)
{
StringBuilder
stringBuilder
=
new
StringBuilder
();
if
(
map
==
null
)
{
return
stringBuilder
.
toString
();
}
else
{
for
(
Map
.
Entry
<
String
,
Object
>
entry
:
map
.
entrySet
())
{
stringBuilder
.
append
(
entry
.
getKey
()).
append
(
"="
).
append
(
entry
.
getValue
()).
append
(
"&"
);
}
return
stringBuilder
.
substring
(
0
,
stringBuilder
.
length
()
-
1
);
}
}
@
RequestMapping
(
"/hello"
)
public
String
hello
(@
RequestParam
(
value
=
"name"
,
defaultValue
=
"World"
)
String
name
)
{
return
String
.
format
(
"Hello, %s!"
,
name
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录