Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
W
wxBot
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
wxBot
Commits
56b5ab22
Commit
56b5ab22
authored
Aug 02, 2017
by
fangzhipeng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加缓存图片和语音功能
parent
926c7f0c
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
88 additions
and
0 deletions
+88
-0
src/main/java/com/yg84/Login.java
src/main/java/com/yg84/Login.java
+17
-0
src/main/java/com/yg84/weixin/Message.java
src/main/java/com/yg84/weixin/Message.java
+9
-0
src/main/java/com/yg84/weixin/WeChat.java
src/main/java/com/yg84/weixin/WeChat.java
+62
-0
No files found.
src/main/java/com/yg84/Login.java
View file @
56b5ab22
...
...
@@ -31,6 +31,7 @@ public class Login {
@Override
public
void
handleMsg
(
List
<
Message
>
messages
)
throws
Exception
{
messageQueue
.
addAll
(
messages
);
messages
.
forEach
(
System
.
out
::
println
);
System
.
out
.
println
(
"成功加入"
+
messages
.
size
()
+
"条信息"
);
}
};
...
...
@@ -97,4 +98,20 @@ public class Login {
public
MyAcount
getMyAcount
()
{
return
weChat
.
getMyAcount
();
}
@RequestMapping
(
value
=
"/getMediaFile"
)
public
void
getMediaFile
(
String
msgId
,
Integer
type
,
HttpServletResponse
response
)
throws
Exception
{
File
file
=
weChat
.
getMediaFile
(
msgId
,
type
);
if
(
file
!=
null
)
{
OutputStream
out
=
response
.
getOutputStream
();
InputStream
in
=
new
FileInputStream
(
file
);
byte
[]
buf
=
new
byte
[
2014
];
int
b
=
-
1
;
while
((
b
=
in
.
read
(
buf
))
!=
-
1
)
{
out
.
write
(
buf
,
0
,
b
);
}
in
.
close
();
out
.
close
();
}
}
}
src/main/java/com/yg84/weixin/Message.java
View file @
56b5ab22
...
...
@@ -5,6 +5,7 @@ package com.yg84.weixin;
*/
public
class
Message
{
private
String
msgId
;
private
Integer
msgType
;
private
String
fromUserName
;
private
String
content
;
...
...
@@ -32,4 +33,12 @@ public class Message {
public
void
setContent
(
String
content
)
{
this
.
content
=
content
;
}
public
String
getMsgId
()
{
return
msgId
;
}
public
void
setMsgId
(
String
msgId
)
{
this
.
msgId
=
msgId
;
}
}
src/main/java/com/yg84/weixin/WeChat.java
View file @
56b5ab22
...
...
@@ -16,6 +16,7 @@ import javax.swing.*;
import
java.io.File
;
import
java.io.FileOutputStream
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.net.CookieManager
;
import
java.net.CookiePolicy
;
import
java.sql.Timestamp
;
...
...
@@ -152,6 +153,19 @@ public class WeChat {
return
null
;
}
/**
* 获取缓存的媒体文件
* @param msgId 消息id
* @param type 媒体文件类型,0-图片,1-声音
* @return
*/
public
File
getMediaFile
(
String
msgId
,
Integer
type
)
{
File
file
=
new
File
(
tempRootDir
,
"/temp/"
+
msgId
+
(
type
==
0
?
".jpg"
:
".mp3"
));
if
(
file
.
exists
())
return
file
;
return
null
;
}
public
void
stopProcessThread
()
{
dealMsg
=
false
;
}
...
...
@@ -242,9 +256,17 @@ public class WeChat {
if
(!
response
.
isSuccessful
())
return
"接收消息失败!"
;
String
msg
=
response
.
body
().
string
();
System
.
out
.
println
(
msg
);
JSONObject
msgObject
=
JSONObject
.
parseObject
(
msg
);
if
(
0
==
msgObject
.
getJSONObject
(
"BaseResponse"
).
getInteger
(
"Ret"
))
{
List
<
Message
>
messages
=
JSONObject
.
parseArray
(
msgObject
.
getString
(
"AddMsgList"
),
Message
.
class
);
for
(
Message
message
:
messages
)
{
if
(
message
.
getMsgType
()
==
3
)
{
//图片消息
dealPicMsg
(
message
);
}
else
if
(
message
.
getMsgType
()
==
34
)
{
//语音消息
dealVoiceMsg
(
message
);
}
}
handler
.
handleMsg
(
messages
);
SyncKeyObject
=
msgObject
.
getJSONObject
(
"SyncCheckKey"
);
genSyncKey
();
...
...
@@ -252,6 +274,43 @@ public class WeChat {
return
"消息处理成功!"
;
}
private
String
dealVoiceMsg
(
Message
message
)
throws
Exception
{
String
url
=
BASE_URL
+
"/webwxgetvoice?"
;
String
params
=
"msgid=%s&skey=%s"
;
Response
response
=
get
(
url
+
String
.
format
(
params
,
message
.
getMsgId
(),
skey
));
if
(!
response
.
isSuccessful
())
return
"处理语音消息失败!"
;
byte
[]
buf
=
new
byte
[
1024
];
int
len
=
-
1
;
InputStream
in
=
response
.
body
().
byteStream
();
File
file
=
new
File
(
tempRootDir
,
"/temp/"
+
message
.
getMsgId
()
+
".mp3"
);
OutputStream
out
=
new
FileOutputStream
(
file
);
while
((
len
=
in
.
read
(
buf
))
!=
-
1
)
{
out
.
write
(
buf
,
0
,
len
);
}
in
.
close
();
out
.
close
();
return
"处理成功!"
;
}
private
String
dealPicMsg
(
Message
message
)
throws
Exception
{
String
url
=
BASE_URL
+
"/webwxgetmsgimg?&MsgID=%s&skey=%s&type=slave"
;
Response
response
=
get
(
String
.
format
(
url
,
message
.
getMsgId
(),
skey
));
if
(!
response
.
isSuccessful
())
return
"处理图片消息失败!"
;
byte
[]
buf
=
new
byte
[
1024
];
int
len
=
-
1
;
InputStream
in
=
response
.
body
().
byteStream
();
File
file
=
new
File
(
tempRootDir
,
"/temp/"
+
message
.
getMsgId
()
+
".jpg"
);
OutputStream
out
=
new
FileOutputStream
(
file
);
while
((
len
=
in
.
read
(
buf
))
!=
-
1
)
{
out
.
write
(
buf
,
0
,
len
);
}
in
.
close
();
out
.
close
();
return
"处理成功!"
;
}
private
Response
get
(
String
url
)
throws
Exception
{
Request
request
=
genBuilder
().
url
(
url
).
build
();
return
client
.
newCall
(
request
).
execute
();
...
...
@@ -420,6 +479,9 @@ public class WeChat {
File
icondir
=
new
File
(
tempRootDir
,
"/icon"
);
if
(!
icondir
.
exists
())
icondir
.
mkdirs
();
File
tempdir
=
new
File
(
tempRootDir
,
"/temp"
);
if
(!
tempdir
.
exists
())
tempdir
.
mkdirs
();
}
private
void
cacheContactIcon
(
List
<
Contact
>
contacts
)
{
...
...
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