图片的URL上传至阿里云OSS操作(微信小程序二维码返回的二进制上传到OSS)

当我们从网络中获取一个URL的图片我们要存储到本地或者是私有的云时,我们可以这样操作 把url中的图片文件下载到本地(或者上传到私有云中)

public String uploadUrlToOss(String url) {
        try{
            URL urls = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) urls.openConnection();
            connection.addRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0");
            connection.setConnectTimeout(10 * 1000);
            connection.setReadTimeout(15 * 1000);
            InputStream inputStream = connection.getInputStream();
            //头像
            File newFile = new File("headimgurl.png");
            FileOutputStream os = new FileOutputStream(newFile);
            byte[] buffer = new byte[81920];
            int bytesRead = 0;
            while((bytesRead = inputStream.read(buffer, 0, 81920)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.flush();
            os.close();
            String urlss = AliyunOSSUtil.upload(newFile);
            newFile.delete();
            return urlss;
        }catch (Exception e){
            log.error("根据Url 获取图片的file 然后上传OSS 异常error ={}",e);
            return null;
        }
    }

微信小程序二维码返回的二进制上传到OSS

public ResultDTO getQrCode(ReqQrCodeDTO reqQrCodeDTO)  {
        try {
            //拼接URL
            String access_token_url = WX_APPLET_GETAT+"?appid="+WX_APPLET_ID+"&secret="+WX_APPLET_KEY+"&grant_type=client_credential";
            //使用Https请求微信API接口
            String loginRet = HttpClientUtil.doGet(access_token_url);
            JSONObject grantObj = new JSONObject(loginRet);
            String errcode = grantObj.optString("errcode");
            if (!StringUtils.isEmpty(errcode)){
                log.error("login weixin error {}",loginRet);
            }
            String accessToken = grantObj.optString("access_token");
            if (StringUtils.isEmpty(accessToken)){
                log.error("bind weixin getOpenId error {}",loginRet);
            }
            /* 获取二维码的链接 */
            String appletUrl = xxxx;
            String param=appletUrl+"";
            //String param="index";
            Map<String, Object> params = new HashMap<>();
            //params.put("access_token", "access_token");
            params.put("path", param);
            //params.put("page", appletUrl);
            params.put("width", 250);
            CloseableHttpClient httpClient = HttpClientBuilder.create().build();
            HttpPost httpPost = new HttpPost(WX_APPLET_GETQR+"?access_token="+accessToken);
            httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
            String body = JSON.toJSONString(params);
            StringEntity entity;
            entity = new StringEntity(body);
            entity.setContentType("image/png");
            httpPost.setEntity(entity);
            HttpResponse response;
            response = httpClient.execute(httpPost);
            InputStream inputStream = response.getEntity().getContent();
            //二维码
            File newFile = new File("qrcode.png");
            FileOutputStream os = new FileOutputStream(newFile);
            byte[] buffer = new byte[81920];
            int bytesRead = 0;
            while((bytesRead = inputStream.read(buffer, 0, 81920)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.flush();
            os.close();
            String urlss = AliyunOSSUtil.upload(newFile);
            newFile.delete();
            return ResultDTO.success(urlss);
        }catch (Exception e){
            log.error("获取二维码失败");
            return ResultDTO.error();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值