下载微信服务器上的图片到自己的服务器

下载微信服务器上的图片到自己的服务器

微信上传图片后

  • 单张图片将返回serverId
  • 多张图片将返回serverId数组,后续只需遍历下载即可

下载微信服务器上的图片

  • 工具类

     

    。。。
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    public class ImageUtils {
        /**
         * @param temp_path(注:临时文件存储目录)
         * @param serverId(注:图片上传完微信服务器后返回的serverId)
         * @param image_number(注:图片的编号)
         * @return
         */
        public static boolean saveImage(String temp_path, String serverId, String image_number) {
            boolean isSave = true;
    
            File saveFile = new File(temp_path);
            if (!saveFile.exists()) {
                saveFile.mkdirs();
            }
    
            try {
                String s = saveImageToDisk(serverId, image_number, temp_path + "/");
                if (s != null || !"".equals(s.trim())) {
                    isSave = true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return isSave;
        }
    
        //获取
        private 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) {
            }
            return is;
    
        }
    
        private static String saveImageToDisk(String serverId, String picName, String picPath) throws Exception {
    
            Weixin weixin = new Weixin(ConfigData.weixin_app_id, ConfigData.weixin_secret, ConfigData.weixin_cache_dir);//此步骤你自己创建自己的weixin对象
            String accessToken = weixin.getGlobalAccessToken();
            InputStream inputStream = getInputStream(accessToken, serverId);
    
            // 循环取出流中的数据
            byte[] data = new byte[1024];
            int len = 0;
            FileOutputStream fileOutputStream = null;
    
            String filePath = picPath + picName;
    
            try {
                fileOutputStream = new FileOutputStream(filePath);
                while ((len = inputStream.read(data)) != -1) {
                    fileOutputStream.write(data, 0, len);
                }
            } catch (IOException e) {
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                    }
                }
                if (fileOutputStream != null) {
                    try {
                        fileOutputStream.close();
                    } catch (IOException e) {
                    }
                }
            }
    
    
            return filePath;
        }
    }
    

微信weixin对象根据你的实际情况来创建

你可能感兴趣的:(微信java)