1. 程式人生 > >圖片的URL上傳至阿里雲OSS操作(微信小程式二維碼返回的二進位制上傳到OSS)

圖片的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();
        }
    }