避免用户更换微信头像后原来的地址失效

//获取微信头像图片输入流,并将其储存到自己图片服务器,并返回链接地址
public String getMyAddress(String url) {

        String uploadFile =null;
        HttpURLConnection conn=null;
        InputStream inputStream=null;
        ByteArrayOutputStream output=null;
        try{
            // 建立链接
            URL httpUrl=new URL(url);
            conn=(HttpURLConnection) httpUrl.openConnection();
            //以Post方式提交表单,默认get方式
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            // post方式不能使用缓存
            conn.setUseCaches(false);
            //连接指定的资源
            conn.connect();
            //获取网络输入流
            inputStream=conn.getInputStream();
            output = new ByteArrayOutputStream();
            byte[] buffer=new byte[1024];
            int n = 0;
            while (-1 != (n = inputStream.read(buffer))) {
                output.write(buffer, 0, n);
            }
            String directory = applicationConfig.getTencentDirectory();
            uploadFile = TencentCosUtil.uploadFile(directory, CosConstant.BusinessType.icon,  String.valueOf(System.currentTimeMillis()),
                    output.toByteArray());
        } catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(inputStream !=null){
                    inputStream.close();
                    output.close();
                    conn.disconnect();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return uploadFile;
    }

 

你可能感兴趣的:(java工具类)