一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java 實現微信服務器下載圖片到自己服務器

java 實現微信服務器下載圖片到自己服務器

2020-10-26 16:41王慧永 Java教程

這篇文章主要介紹了 java 實現微信服務器下載圖片到自己服務器的相關資料,需要的朋友可以參考下

 java 實現微信服務器下載圖片到自己服務器

          此功能的實現需要注意java 中IO流的操作及網路開發,

          實現代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
 * @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:/");
  }
}

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

原文鏈接:http://blog.csdn.net/u011072139/article/details/47108761

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品无码久久久久久久动漫 | 国产青草亚洲香蕉精品久久 | 亚洲国产精品嫩草影院久久 | 激情艳妇 | 教室里的激情电影 | 逼逼日| 日韩福利网站 | 精品视频一区二区观看 | 日韩久久综合 | 学校捏奶揉下面污文h | 精品国产一级在线观看 | 99热久热这里只精品 | 国产成人精品曰本亚洲77美色 | 国产高清经典露脸3p | 扒开老师挠尿口到崩溃刑罚 | a亚洲视频 | 窝窝色资源站 | 特黄特黄一级高清免费大片 | 日本一区二区高清免费不卡 | 亚洲国产成人久久精品影视 | www.爱操 | 草莓丝瓜芭乐樱桃榴莲色多黄 | 欧美gay xxxx| 青青草国产一区二区三区 | 欧美深夜在线 | 国产一卡二卡3卡4卡更新 | 白白国产永久免费视频 | blacked黑人| 欧美成人免费一区在线播放 | 白鹿扒开内裤露出尿孔 | 亚洲一区二区成人 | 肉肉小说在线阅读 | 亚洲视频在线看 | 饭冈加奈子乳喷cead144 | 蜜臀91 | 99ri国产在线观看 | 玩乳h文奶水和尚 | 手机看片黄色 | 99在线免费播放 | 国产精品视频1区 | 午夜深情在线观看免费 |