图片的URL上传至阿里云OSS操作(微信小程序二维…

2019-01-04 09:48:06来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

当我们从网络中获取一个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();
        }
    }

 

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Web程序中使用EasyUI时乱码问题

下一篇:javaSE练习13——(知识点:类的继承 方法的覆盖)