调用小程序里的获取二维码接口后,一直返回乱码,但是在postman里又可以成功获取到二维码图片。
哪位大神知道是什么问题导致获取不到二维码图片?
wx.request({
url: url,
data: {
path:“pages/test/test”,
width:175
},
method: ‘POST’,
header: {
‘content-type’:‘application/json’
},
success: function(res) {
console.log(res);
}
})
data是报错信息。 没有图片,你可以用postman试试,图片是保存在Content-disposition →attachment; filename="_app_…/cardDetail/cardDetail?cardid=6437&type=cn.jpg" ,需要后端服务器将图片保存后,将图片名称发到前端。
String URL = “二维码接口地址”;
String meetingId=request.getParameter(“id”);//我自己的参数,不用管
log.info("------------------createwxaqrcode"+meetingId);
if(meetingId!=null&&meetingId.trim().length()>0){
String access_token;
InputStream is=null;
OutputStream os=null;
try {
access_token = getAccess_token();//获取token,我自己写的方法,把acess_token获取出来就行
URL = URL + “?access_token=” + access_token;
Map<String, Object> map = new HashMap<String, Object>();
map.put(“path”, “pages/index/index?meetingId=”+meetingId);
map.put(“width”, 375);
JSONObject json = new JSONObject(map);
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost(URL);
post.addHeader(HTTP.CONTENT_TYPE, “application/json”);
StringEntity se;
se = new StringEntity(json.toString());
se.setContentType(“application/json”);
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, “UTF-8”));
post.setEntity(se);
StringEntity s = new StringEntity(json.toString(), “utf-8”);
s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
“application/json”));
post.setEntity(s);
HttpResponse res = client.execute(post);
if(res!=null){
HttpEntity resEntity = res.getEntity();
if (resEntity != null) {
// 获取响应输入流
is = resEntity.getContent();
response.setHeader(“Content-Type”,“application/x-msdownload”);
os=response.getOutputStream();
//os=new FileOutputStream(“D:\\test.png”);//生成图片
int count = 0;
byte[] buffer = new byte[1024 * 8];
while ((count = is.read(buffer)) != -1) {
os.write(buffer, 0, count);
os.flush();
}
os.close();
is.close();
}
}
我是直接把流发送到小程序端的,直接用image标签的 src属性读取的