{"ticket":"gQFt7zoAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL0F6dFktTVhsSV90YXNVX1ZtUlhSAAIE5hxiVwMEPAAAAA==",
参数与ticket之间是一一对应的,这也就达到了生成用户海报的专属二维码了,至于这个参数的选定,本人能够给一个建议,将用户的openId+当前时间做为参数,返回的ticket和用户就能够达到一一对应的想过了,当二维码过时,数据库更新ticket这个数据就能够了,或者用户每次获取海报的时候就从新更新一个二维码,反正临时的不少。其余用户扫这个二维码的时候微信会将二维码的ticket返回给咱们,再根据ticket和openId的对应关系就能够知道当前被扫用户是谁了。
//post请求
public
static
String sendPost(String
param
, String
url
) {
PrintWriter
out
=
null
;
BufferedReader
in
=
null
;
String
result
=
""
;
try
{
URL
realUrl
=
new
URL(
url
);
// 打开和URL之间的链接
URLConnection
conn
=
realUrl
.openConnection();
// 设置通用的请求属性
conn
.setRequestProperty(
"accept"
,
"*/*"
);
conn
.setRequestProperty(
"connection"
,
"Keep-Alive"
);
conn
.setRequestProperty(
"user-agent"
,
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"
);
// 发送POST请求必须设置以下两行
conn
.setDoOutput(
true
);
conn
.setDoInput(
true
);
// 获取URLConnection对象对应的输出流
// out = new PrintWriter(conn.getOutputStream());
out
=
new
PrintWriter(
new
OutputStreamWriter(
conn
.getOutputStream(),
"utf-8"
));
// 发送请求参数
out
.print(
param
);
// flush输出流的缓冲
out
.flush();
// 定义BufferedReader输入流来读取URL的响应
in
=
new
BufferedReader(
new
InputStreamReader(
conn
.getInputStream(),
"UTF-8"
));
String
line
;
while
((
line
=
in
.readLine()) !=
null
) {
result
+=
line
;
}
}
catch
(Exception
e
) {
System.
out
.println(
"发送 POST 请求出现异常!"
+
e
);
e
.printStackTrace();
}
// 使用finally块来关闭输出流、输入流
finally
{
try
{
if
(
out
!=
null
) {
out
.close();
}
if
(
in
!=
null
) {
in
.close();
}
}
catch
(IOException
ex
) {
ex
.printStackTrace();
}
}
return
result
;
}
//根据url下载文件,参数(文件网址,存文件的本地地址)
public
static
Boolean downloadFile(String
urlString
, String
filePath
){
// 构造URL
URL
url
;
try
{
url
=
new
URL(
urlString
);
// 打开链接
URLConnection
con
;
try
{
con
=
url
.openConnection();
// 输入流
InputStream
is
=
con
.getInputStream();
// 1K的数据缓冲
byte
[]
bs
=
new
byte
[1024];
// 读取到的数据长度
int
len
;
// 输出的文件流
OutputStream
os
=
new
FileOutputStream(
filePath
);
// 开始读取
while
((
len
=
is
.read(
bs
)) != -1) {
os
.write(
bs
, 0,
len
);
}
// 完毕,关闭全部连接
os
.close();
is
.close();
return
true
;
}
catch
(IOException
e
) {
//
TODO
Auto-generated catch block
e
.printStackTrace();
return
false
;
}
}
catch
(MalformedURLException
e
) {
//
TODO
Auto-generated catch block
e
.printStackTrace();
return
false
;
}