Java 微信公众号上传永久素材的方法

  1. /** 
  2.  * 上传其他永久素材(图片素材的上限为5000,其他类型为1000) 
  3.  * @param appid 
  4.  * @param secret 
  5.  * @return 
  6.  * @throws Exception 
  7.  */  
  8. public JSONObject addMaterialEver(String appid, String secret,File file,String type) throws Exception {  
  9.     try {  
  10.         PrintUtil.println("开始上传"+type+"永久素材---------------------");   
  11.           
  12.         //开始获取证书  
  13.         String accessToken=new AccessTokenBizImpl().getAccessToken(appid, secret);            
  14.         if(!StringUtil.isEmptyString(accessToken)){  
  15.             PrintUtil.println("accessToken is null");     
  16.             return null;  
  17.         }             
  18.           
  19.         //上传素材    
  20.         String path="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token="+accessToken+"&type="+type;  
  21.         String result=HttpsUtil.connectHttpsByPost(path, null, file);  
  22.         result=result.replaceAll("[\\\\]""");  
  23.         PrintUtil.println("result:"+result);      
  24.         JSONObject resultJSON=JSONObject.fromObject(result);  
  25.         if(resultJSON!=null){  
  26.             if(resultJSON.get("media_id")!=null){  
  27.                 PrintUtil.println("上传"+type+"永久素材成功");  
  28.                 return resultJSON;  
  29.             }else{  
  30.                 PrintUtil.println("上传"+type+"永久素材失败");  
  31.             }  
  32.         }  
  33.           
  34.         return null;  
  35.     } catch (Exception e) {  
  36.         PrintUtil.println("程序异常", e);  
  37.         throw e;  
  38.     }finally{  
  39.         PrintUtil.println("结束上传"+type+"永久素材---------------------");   
  40.     }  
  41. }  
  42.   
  43. /** 
  44.  * 上传图片到微信服务器(本接口所上传的图片不占用公众号的素材库中图片数量的5000个的限制。图片仅支持jpg/png格式,大小必须在1MB以下) 
  45.  * @param appid 
  46.  * @param secret 
  47.  * @param type 
  48.  * @param file 
  49.  * @return 
  50.  * @throws Exception 
  51.  */  
  52. public JSONObject addMaterialEver(String appid, String secret,File file) throws Exception {  
  53.     try {  
  54.         PrintUtil.println("开始上传图文消息内的图片---------------------");   
  55.           
  56.         //开始获取证书  
  57.         String accessToken=new AccessTokenBizImpl().getAccessToken(appid, secret);            
  58.         if(!StringUtil.isEmptyString(accessToken)){  
  59.             PrintUtil.println("accessToken is null");     
  60.             return null;  
  61.         }             
  62.           
  63.         //上传图片素材      
  64.         String path="https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token="+accessToken;  
  65.         String result=HttpsUtil.connectHttpsByPost(path, null, file);  
  66.         result=result.replaceAll("[\\\\]""");  
  67.         PrintUtil.println("result:"+result);      
  68.         JSONObject resultJSON=JSONObject.fromObject(result);  
  69.         if(resultJSON!=null){  
  70.             if(resultJSON.get("url")!=null){  
  71.                 PrintUtil.println("上传图文消息内的图片成功");  
  72.                 return resultJSON;  
  73.             }else{  
  74.                 PrintUtil.println("上传图文消息内的图片失败");  
  75.             }  
  76.         }  
  77.           
  78.         return null;  
  79.     } catch (Exception e) {  
  80.         PrintUtil.println("程序异常", e);  
  81.         throw e;  
  82.     }finally{  
  83.         PrintUtil.println("结束上传图文消息内的图片---------------------");   
  84.     }  

  1. }  


具体的HTTPS请求:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1.         String result =null;  
  2. con.setDoInput(true);  
  3.   
  4. con.setDoOutput(true);  
  5.   
  6. con.setUseCaches(false); // post方式不能使用缓存  
  7.   
  8. // 设置请求头信息  
  9.   
  10. con.setRequestProperty("Connection""Keep-Alive");  
  11.   
  12. con.setRequestProperty("Charset""UTF-8");  
  13. // 设置边界  
  14.   
  15. String BOUNDARY = "----------" + System.currentTimeMillis();  
  16.   
  17. con.setRequestProperty("Content-Type",  
  18.         "multipart/form-data; boundary="  
  19.   
  20.         + BOUNDARY);  
  21.   
  22. // 请求正文信息  
  23.   
  24. // 第一部分:  
  25.   
  26. StringBuilder sb = new StringBuilder();  
  27.   
  28. sb.append("--"); // 必须多两道线  
  29.   
  30. sb.append(BOUNDARY);  
  31.   
  32. sb.append("\r\n");  
  33.   
  34. sb.append("Content-Disposition: form-data;name=\"media\";filelength=\""+file.length()+"\";filename=\""  
  35.   
  36.         + file.getName() + "\"\r\n");  
  37.   
  38. sb.append("Content-Type:application/octet-stream\r\n\r\n");  
  39.   
  40. byte[] head = sb.toString().getBytes("utf-8");  
  41.   
  42. // 获得输出流  
  43.   
  44. OutputStream out = new DataOutputStream(con.getOutputStream());  
  45.   
  46. // 输出表头  
  47.   
  48. out.write(head);  
  49.   
  50. // 文件正文部分  
  51.   
  52. // 把文件已流文件的方式 推入到url中  
  53.   
  54. DataInputStream in = new DataInputStream(new FileInputStream(file));  
  55.   
  56. int bytes = 0;  
  57.   
  58. byte[] bufferOut = new byte[1024];  
  59.   
  60. while ((bytes = in.read(bufferOut)) != -1) {  
  61.   
  62.     out.write(bufferOut, 0, bytes);  
  63.   
  64. }  
  65.   
  66. in.close();  
  67.   
  68. // 结尾部分  
  69.   
  70. byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");// 定义最后数据分隔线  
  71.   
  72. out.write(foot);  
  73.   
  74. out.flush();  
  75.   
  76. out.close();  
  77.   
  78. StringBuffer buffer = new StringBuffer();  
  79.   
  80. BufferedReader reader = null;  
  81.   
  82. try {  
  83.   
  84.     // 定义BufferedReader输入流来读取URL的响应  
  85.   
  86.     reader = new BufferedReader(new InputStreamReader(con.getInputStream()));  
  87.   
  88.     String line = null;  
  89.   
  90.     while ((line = reader.readLine()) != null) {  
  91.   
  92.         buffer.append(line);  
  93.   
  94.     }  
  95.   
  96.     if (result == null) {  
  97.   
  98.         result = buffer.toString();  
  99.   
  100.     }  
  101.   
  102. catch (IOException e) {  
  103.   
  104.     System.out.println("发送POST请求出现异常!" + e);  
  105.   
  106.     e.printStackTrace();  
  107.   
  108.     throw new IOException("数据读取异常");  
  109.   
  110. finally {  
  111.   
  112.     if (reader != null) {  
  113.   
  114.         reader.close();  
  115.   
  116.     }  
  117.   
  118. }  
  119. return result;  


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值