public class WeixinUtilTest { /** * * @author qincd * @date Nov 6, 2014 9:57:54 AM */ public static void main(String[] args) { // 1).获取access_token AccessToken accessToken = WeixinUtil.getAccessToken(WeixinUtil.APPID, WeixinUtil.APP_SECRET); String filePath = "C:\\Users\\qince\\Pictures\\壁纸20141029091612.jpg"; JSONObject uploadJsonObj = testUploadMedia(filePath,"image",accessToken.getToken()); if (uploadJsonObj == null) { System.out.println("上传图片失败"); return; } int errcode = 0; if (uploadJsonObj.containsKey("errcode")) { errcode = uploadJsonObj.getInt("errcode"); } if (errcode == 0) { System.out.println("图片上传成功"); String mediaId = uploadJsonObj.getString("media_id"); long createAt = uploadJsonObj.getLong("created_at"); System.out.println("--Details:"); System.out.println("media_id:" + mediaId); System.out.println("created_at:" + createAt); } else { System.out.println("图片上传失败!"); String errmsg = uploadJsonObj.getString("errmsg"); System.out.println("--Details:"); System.out.println("errcode:" + errcode); System.out.println("errmsg:" + errmsg); } String mediaId = "6W-UvSrQ2hkdSdVQJJXShwtFDPLfbGI1qnbNFy8weZyb9Jac2xxxcAUwt8p4sYPH"; String filepath = testDownloadMedia(accessToken.getToken(), mediaId, "d:/test"); System.out.println(filepath); } /** * 上传多媒体文件到微信 * * @author qincd * @date Nov 6, 2014 4:15:14 PM */ public static JSONObject testUploadMedia(String filePath,String type,String accessToken) { try { return WeixinUtil.uploadMediaToWX(filePath, type, accessToken); } catch (IOException e) { e.printStackTrace(); } return null; } /** * 从微信下载多媒体文件 * * @author qincd * @date Nov 6, 2014 4:56:25 PM */ public static String testDownloadMedia(String accessToken,String mediaId,String fileSaveDir) { try { return WeixinUtil.downloadMediaFromWx(accessToken, mediaId, fileSaveDir); } catch (IOException e) { e.printStackTrace(); } return null; } }
java微信开发之上传下载多媒体文件(2)
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:https://www.heiqu.com/ec110466cc3d24fb077bf3794de0e6a5.html