Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
提交反馈
为 GitLab 提交贡献
登录
切换导航
P
pica-cloud-analysis
项目
项目
详情
动态
版本
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
收起侧边栏
Close sidebar
动态
分支图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
com.pica.cloud.online.exam
pica-cloud-analysis
提交
a2703148
提交
a2703148
编写于
8月 29, 2018
作者:
minghao.wu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: 图片解析
上级
4ec9fd31
变更
2
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
139 行增加
和
7 行删除
+139
-7
AntiSpamService.java
.../online/exam/analysis/server/service/AntiSpamService.java
+3
-0
AntiSpamServiceImpl.java
...xam/analysis/server/service/impl/AntiSpamServiceImpl.java
+136
-7
未找到文件。
server/src/main/java/com/pica/cloud/online/exam/analysis/server/service/AntiSpamService.java
浏览文件 @
a2703148
...
@@ -9,4 +9,7 @@ import com.pica.cloud.foundation.entity.PicaResponse;
...
@@ -9,4 +9,7 @@ import com.pica.cloud.foundation.entity.PicaResponse;
public
interface
AntiSpamService
{
public
interface
AntiSpamService
{
PicaResponse
processString
(
String
content
);
PicaResponse
processString
(
String
content
);
boolean
processImageContent
(
String
content
);
boolean
processImage
(
String
url
);
}
}
server/src/main/java/com/pica/cloud/online/exam/analysis/server/service/impl/AntiSpamServiceImpl.java
浏览文件 @
a2703148
...
@@ -17,9 +17,8 @@ import org.springframework.beans.factory.annotation.Autowired;
...
@@ -17,9 +17,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
java.io.UnsupportedEncodingException
;
import
java.io.UnsupportedEncodingException
;
import
java.util.HashMap
;
import
java.util.*
;
import
java.util.Map
;
import
java.util.regex.Pattern
;
import
java.util.Random
;
/**
/**
* @author wuminghao
* @author wuminghao
...
@@ -31,9 +30,16 @@ public class AntiSpamServiceImpl implements AntiSpamService {
...
@@ -31,9 +30,16 @@ public class AntiSpamServiceImpl implements AntiSpamService {
private
PropertiesConfiguration
properties
;
private
PropertiesConfiguration
properties
;
private
static
HttpClient
httpClient
=
HttpClient4Utils
.
createHttpClient
(
100
,
20
,
2000
,
2000
,
2000
);
private
static
HttpClient
httpClient
=
HttpClient4Utils
.
createHttpClient
(
100
,
20
,
2000
,
2000
,
2000
);
private
static
HttpClient
httpClientImage
=
HttpClient4Utils
.
createHttpClient
(
100
,
20
,
10000
,
2000
,
2000
);
@Override
@Override
public
PicaResponse
processString
(
String
content
)
{
public
PicaResponse
processString
(
String
content
)
{
/**
* 过滤图片
*/
if
(!
processImageContent
(
content
))
{
return
ReturnUtil
.
getPicaResponse
(
"400002"
,
"图片包含违禁内容,请修改后重新输入"
);
}
Map
<
String
,
String
>
params
=
new
HashMap
<
String
,
String
>();
Map
<
String
,
String
>
params
=
new
HashMap
<
String
,
String
>();
/**
/**
...
@@ -96,9 +102,6 @@ public class AntiSpamServiceImpl implements AntiSpamService {
...
@@ -96,9 +102,6 @@ public class AntiSpamServiceImpl implements AntiSpamService {
if
(
action
==
0
)
{
if
(
action
==
0
)
{
}
else
{
}
else
{
/**
* TODO: 可以进一步处理 显示信息被过滤原因
*/
for
(
JsonElement
labelElement
:
labelArray
)
{
for
(
JsonElement
labelElement
:
labelArray
)
{
JsonObject
lObject
=
labelElement
.
getAsJsonObject
();
JsonObject
lObject
=
labelElement
.
getAsJsonObject
();
int
label
=
lObject
.
get
(
"label"
).
getAsInt
();
int
label
=
lObject
.
get
(
"label"
).
getAsInt
();
...
@@ -124,4 +127,130 @@ public class AntiSpamServiceImpl implements AntiSpamService {
...
@@ -124,4 +127,130 @@ public class AntiSpamServiceImpl implements AntiSpamService {
return
ReturnUtil
.
getPicaResponse
(
PicaResultCode
.
SUCCESS
,
""
);
return
ReturnUtil
.
getPicaResponse
(
PicaResultCode
.
SUCCESS
,
""
);
}
}
@Override
public
boolean
processImageContent
(
String
content
)
{
List
<
String
>
imageUrlList
=
new
ArrayList
<>();
/**
* 如果不包含图片,直接返回
*/
if
(!
content
.
contains
(
"<imgsrc="
))
{
return
true
;
}
int
index
=
-
1
;
int
end
=
-
1
;
while
(-
1
!=
(
index
=
content
.
indexOf
(
"<imgsrc=\\\""
)))
{
end
=
content
.
substring
(
index
,
content
.
length
()).
indexOf
(
"\\\">"
);
imageUrlList
.
add
(
content
.
substring
(
index
,
index
+
end
).
replace
(
"<imgsrc=\\\""
,
""
));
content
=
content
.
replaceFirst
(
"(<imgsrc[\\s\\S]*?>)"
,
""
);
}
for
(
String
url
:
imageUrlList
)
{
/**
* 检测出某张图片失败 则返回
*/
if
(!
processImage
(
url
))
{
return
false
;
}
}
return
true
;
}
@Override
public
boolean
processImage
(
String
url
)
{
Map
<
String
,
String
>
params
=
new
HashMap
<
String
,
String
>();
/**
* 1. 设置公共参数
*/
params
.
put
(
"secretId"
,
properties
.
getNeteaseAntispamSecretId
());
params
.
put
(
"businessId"
,
properties
.
getNeteaseAntispamImageBusinessId
());
params
.
put
(
"version"
,
"v3.2"
);
params
.
put
(
"timestamp"
,
String
.
valueOf
(
System
.
currentTimeMillis
()));
params
.
put
(
"nonce"
,
String
.
valueOf
(
new
Random
().
nextInt
()));
/**
* 2. 设置私有参数
*/
JsonArray
jsonArray
=
new
JsonArray
();
JsonObject
image
=
new
JsonObject
();
image
.
addProperty
(
"name"
,
url
);
image
.
addProperty
(
"type"
,
1
);
image
.
addProperty
(
"data"
,
url
);
jsonArray
.
add
(
image
);
params
.
put
(
"images"
,
jsonArray
.
toString
());
params
.
put
(
"account"
,
"picahealth@picahealth.com"
);
/**
* 3. 生成签名信息
*/
try
{
String
signature
=
SignatureUtils
.
genSignature
(
properties
.
getNeteaseAntispamSecretKey
(),
params
);
params
.
put
(
"signature"
,
signature
);
}
catch
(
UnsupportedEncodingException
e
)
{
e
.
printStackTrace
();
}
/**
* 4. 发送HTTP请求
*/
String
response
=
HttpClient4Utils
.
sendPost
(
httpClientImage
,
properties
.
getNeteaseAntispamImageApiUrl
(),
params
,
Consts
.
UTF_8
);
/**
* 5. 解析返回值
*/
JsonObject
resultObject
=
new
JsonParser
().
parse
(
response
).
getAsJsonObject
();
int
code
=
resultObject
.
get
(
"code"
).
getAsInt
();
String
msg
=
resultObject
.
get
(
"msg"
).
getAsString
();
if
(
code
==
200
)
{
JsonArray
resultArray
=
resultObject
.
getAsJsonArray
(
"result"
);
for
(
JsonElement
jsonElement
:
resultArray
)
{
JsonObject
jObject
=
jsonElement
.
getAsJsonObject
();
String
name
=
jObject
.
get
(
"name"
).
getAsString
();
int
status
=
jObject
.
get
(
"status"
).
getAsInt
();
String
taskId
=
jObject
.
get
(
"taskId"
).
getAsString
();
JsonArray
labelArray
=
jObject
.
get
(
"labels"
).
getAsJsonArray
();
System
.
out
.
println
(
String
.
format
(
"taskId=%s,status=%s,name=%s,labels:"
,
taskId
,
status
,
name
));
/**
* 检测失败 0:成功 610:图片下载失败 620:图片格式错误 630:其他
*/
if
(
status
!=
0
)
{
continue
;
}
int
maxLevel
=
-
1
;
// 产品需根据自身需求,自行解析处理,本示例只是简单判断分类级别
for
(
JsonElement
labelElement
:
labelArray
)
{
JsonObject
lObject
=
labelElement
.
getAsJsonObject
();
int
label
=
lObject
.
get
(
"label"
).
getAsInt
();
int
level
=
lObject
.
get
(
"level"
).
getAsInt
();
double
rate
=
lObject
.
get
(
"rate"
).
getAsDouble
();
if
(
label
==
100
)
{
System
.
out
.
println
(
String
.
format
(
"label:%s, level=%s, rate=%s"
,
label
,
level
,
rate
));
maxLevel
=
level
>
maxLevel
?
level
:
maxLevel
;
}
}
switch
(
maxLevel
)
{
case
0
:
System
.
out
.
println
(
"#图片机器检测结果:最高等级为\"正常\"\n"
);
break
;
case
1
:
System
.
out
.
println
(
"#图片机器检测结果:最高等级为\"嫌疑\"\n"
);
break
;
case
2
:
System
.
out
.
println
(
"#图片机器检测结果:最高等级为\"确定\"\n"
);
break
;
default
:
break
;
}
}
}
else
{
System
.
out
.
println
(
String
.
format
(
"ERROR: code=%s, msg=%s"
,
code
,
msg
));
}
return
true
;
}
}
}
写
预览
Markdown
格式
0%
请重试
or
附加一个文件
附加文件
取消
您添加了
0
人
到此讨论。请谨慎行事。
先完成此消息的编辑!
取消
想要评论请
注册
或
登录