欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java 实现微信服务器下载图片到自己服务器

程序员文章站 2024-02-14 13:30:22
 java 实现微信服务器下载图片到自己服务器           此...

 java 实现微信服务器下载图片到自己服务器

          此功能的实现需要注意java 中io流的操作及网路开发,

          实现代码:

/** 
 * @author why 
 * 
 */ 
public class picdownload { 
 
  /** 
   * 
   * 根据文件id下载文件 
   * 
   * 
   * 
   * @param mediaid 
   * 
   *      媒体id 
   * 
   * @throws exception 
   */ 
 
  public static inputstream getinputstream(string accesstoken, string mediaid) { 
    inputstream is = null; 
    string url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=" 
        + accesstoken + "&media_id=" + mediaid; 
    try { 
      url urlget = new url(url); 
      httpurlconnection http = (httpurlconnection) urlget 
          .openconnection(); 
      http.setrequestmethod("get"); // 必须是get方式请求 
      http.setrequestproperty("content-type", 
          "application/x-www-form-urlencoded"); 
      http.setdooutput(true); 
      http.setdoinput(true); 
      system.setproperty("sun.net.client.defaultconnecttimeout", "30000");// 连接超时30秒 
      system.setproperty("sun.net.client.defaultreadtimeout", "30000"); // 读取超时30秒 
      http.connect(); 
      // 获取文件转化为byte流 
      is = http.getinputstream(); 
 
    } catch (exception e) { 
      e.printstacktrace(); 
    } 
    return is; 
 
  } 
 
  /** 
   * 
   * 获取下载图片信息(jpg) 
   * 
   * 
   * 
   * @param mediaid 
   * 
   *      文件的id 
   * 
   * @throws exception 
   */ 
 
  public static void saveimagetodisk(string accesstoken, string mediaid, string picname, string picpath) 
      throws exception { 
    inputstream inputstream = getinputstream(accesstoken, mediaid); 
    byte[] data = new byte[10240]; 
    int len = 0; 
    fileoutputstream fileoutputstream = null; 
    try { 
      fileoutputstream = new fileoutputstream(picpath+picname+".jpg"); 
      while ((len = inputstream.read(data)) != -1) { 
        fileoutputstream.write(data, 0, len); 
      } 
    } catch (ioexception e) { 
      e.printstacktrace(); 
    } finally { 
      if (inputstream != null) { 
        try { 
          inputstream.close(); 
        } catch (ioexception e) { 
          e.printstacktrace(); 
        } 
      } 
      if (fileoutputstream != null) { 
        try { 
          fileoutputstream.close(); 
        } catch (ioexception e) { 
          e.printstacktrace(); 
        } 
      } 
    } 
  } 
 
  /** 
   * 图片下载 
   * 
   * @param accesstoken 
   * @param mediaid 
   */ 
  public static void getpic(string accesstoken, string mediaid) { 
    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); 
    jsonobject jsonobject = weixinutil.httprequest(requesturl, "get", null); 
    system.out.println(jsonobject); 
  } 
 
  public static void main(string[] args) throws exception { 
    string accesstoken = ""; 
    string mediaid = ""; 
    string picname = ""; 
    saveimagetodisk(accesstoken, mediaid, picname,"f:/"); 
  } 
} 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!