首页 新闻 会员 周边

【求助】微信下载图片到服务器……

0
悬赏园豆:50 [已解决问题] 解决于 2017-02-08 17:18

我的公众号没有认证,现在是调用chooseimage拍摄了一张照片并上传成功返回了media_id,准备下载到本地服务器的时候没有成功。到微信的接口调试工具上调试时,显示

到底是什么原因呢?

冷_冷的主页 冷_冷 | 初学一级 | 园豆:171
提问于:2017-02-08 15:42
< >
分享
最佳答案
0

没有授权,下载功能有授权啊?

收获园豆:50
让我发会呆 | 老鸟四级 |园豆:2929 | 2017-02-08 15:49

这个downimages接口返回的localId和chooseimage返回的localId是一样的,为什么,那这个返回的下载后的本地ID怎么处理呢?我想将用户上传的图片保存到服务器里,谢谢

冷_冷 | 园豆:171 (初学一级) | 2017-02-08 16:20

@冷_冷: 

/**
* 将微信服务器上的文件 下载到本地服务器
*
* @param token
* @param mediaId serverId(前端页面),即微信服务器上mediaId
* @return 微信服务器的文件流
* @throws Exception
*/
public byte[] downloadWechatMedia(String token, String mediaId) throws Exception {
// 拼接请求地址
String requestUrl = Constants.WECHAT_MEDIA_DOWNLOAD_URL;
requestUrl = String.format(requestUrl, token, mediaId);
byte[] b = downloadMedia(requestUrl);
return b;
}

这个requestUrl就是微信的下载请求url,这三个参数应该都了解的吧

让我发会呆 | 园豆:2929 (老鸟四级) | 2017-02-08 16:39
 1             var url = string.Format("https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token={0}&media_id={1}", accesstoken, mediaid);
 2 
 3             using (MemoryStream ms = new MemoryStream())
 4             {
 5                 WebClient wc = new WebClient();
 6                 var data = wc.DownloadData(url);
 7                 foreach (var b in data)
 8                 {
 9                     ms.WriteByte(b);
10                 }
11                 Assert.IsTrue(ms.Length > 0);
12                 var fileName = string.Format(@"images/testpic_{0}.jpg", DateTime.Now.Ticks);
13                 using (FileStream fs = new FileStream(fileName, FileMode.Create))
14                 {
15                     ms.Position = 0;
16                     byte[] buffer = new byte[1024];
17                     int bytesRead = 0;
18                     while ((bytesRead = ms.Read(buffer, 0, buffer.Length)) != 0)
19                     {
20                         fs.Write(buffer, 0, bytesRead);
21                     }
22                     fs.Flush();
23                 }
24                 Assert.IsTrue(File.Exists(fileName));
25             }

你的downloadMedia()是怎么处理的啊?谢谢

冷_冷 | 园豆:171 (初学一级) | 2017-02-08 17:01

@冷_冷: 

/**
* 获取媒体文件
* @throws IOException
*/
public byte[] downloadMedia(String requestUrl) {
HttpURLConnection conn = null;
ByteArrayOutputStream swapStream = null;
try {
URL url = new URL(requestUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setRequestMethod("GET");

byte[] buff = new byte[8096];
swapStream = new ByteArrayOutputStream();
int size = 0;
while ((size = conn.getInputStream().read(buff)) != -1) {
swapStream.write(buff, 0, size);
}
return swapStream.toByteArray();
} catch (Exception e) {
logger.error("下载媒体文件失败:", e);
} finally {
if(conn!=null){
conn.disconnect();
}
try {
swapStream.close();
} catch (IOException execption) {
logger.error("下载媒体文件失败:", execption);
}
}
return null;
}

让我发会呆 | 园豆:2929 (老鸟四级) | 2017-02-08 17:04

@让我发会呆: 

这是Java的吧,C#没有HttpURLConnection

冷_冷 | 园豆:171 (初学一级) | 2017-02-08 17:18

@冷_冷: = =! 是的,这是java

让我发会呆 | 园豆:2929 (老鸟四级) | 2017-02-08 17:18
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册