6.9.4 下载多媒体文件
下载多媒体文件的请求地址如下:
- http://file.api.weixin.qq.com/cgi-bin/media/
- get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID
笔者将下载多媒体文件的操作封装成getMedia()方法,该方法的实现如下:
- /**
- * 下载媒体文件
- *
- * @param accessToken 接口访问凭证
- * @param mediaId 媒体文件标识
- * @param savePath 文件在服务器上的存储路径
- * @return
- */
- public static String getMedia(String accessToken, String mediaId, String savePath) {
- String filePath = null;
- // 拼接请求地址
- String requestUrl = "http://file.api.weixin.qq.com/cgi-bin/
- media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID";
- requestUrl = requestUrl.replace("ACCESS_TOKEN",
- accessToken).replace("MEDIA_ID", mediaId);
- try {
- URL url = new URL(requestUrl);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setDoInput(true);
- conn.setRequestMethod("GET");
- if (!savePath.endsWith("/")) {
- savePath += "/";
- }
- // 根据内容类型获取扩展名
- String fileExt = CommonUtil.getFileExt(conn.getHeaderField("Content-Type"));
- // 将mediaId作为文件名
- filePath = savePath + mediaId + fileExt;
- BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
- FileOutputStream fos = new FileOutputStream(new File(filePath));
- byte[] buf = new byte[8096];
- int size = 0;
- while ((size = bis.read(buf)) != -1)
- fos.write(buf, 0, size);
- fos.close();
- bis.close();
- conn.disconnect();
- log.info("下载媒体文件成功,filePath=" + filePath);
- } catch (Exception e) {
- filePath = null;
- log.error("下载媒体文件失败:{}", e);
- }
- return filePath;
- }
注意 下载多媒体文件接口不支持下载视频文件,只支持下载图片、语音和缩略图文件。