国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

java獲取網絡圖片上傳到OSS的方法

2024-07-14 08:42:38
字體:
來源:轉載
供稿:網友

OSS不支持通過一個網絡地址來上傳圖片,所以若想將網絡上的圖片上傳到OSS上需要走點彎路。

1、通過鏈接將圖片下載到本地的一個文件夾下面

2、用OSS上傳該文件夾下的文件

3、上傳完成后刪除本地的文件

具體代碼如下:

 //獲取當前項目的絕對路徑  public static String getTomcatPath(){   String nowpath;     String tempdir;   nowpath=System.getProperty("user.dir");   tempdir=nowpath.replace("bin", ""); //把bin 文件夾變到 webapps文件里面   return tempdir;  }  /** * 將圖片下載下來后,上傳到OSS * @param imgLink * @param downloadPath * @return * @throws Exception */ private String downloadImagAndUploadToOss(String imgLink,String downloadPath) throws Exception{  List<String> urlList=new ArrayList<String>();  urlList.add(imgLink);  String imgName=DateUtil.formatDate(new Date(), "yyyyMMddhhmmss")+UuidUtil.createUUID()+".jpg";  downloadPicture(urlList,downloadPath,imgName); String key="carAlbum/"+imgName; String imgUrl=OSSObjectAPI.genOssPicUrl(OSSObjectAPI.XI_AN_BUCKET_NAME,OSSObjectAPI.XIAN_ACCESS_ID,OSSObjectAPI.XIAN_ACCESS_KEY,  "http://oss-cn-zhangjiakou.aliyuncs.com/",downloadPath+imgName,key); FileUtil.delete(downloadPath+imgName); return imgUrl; }  /**   * 傳入要下載的圖片的url列表,將url所對應的圖片下載到本地   * @param urlList   * @throws Exception   */  private void downloadPicture(List<String> urlList,String path,String imgName) throws Exception {   if(urlList==null||urlList.size()==0){   return;  }  URL url = null;   FileOutputStream fileOutputStream =null;  InputStream inputStream =null;  for (String urlString : urlList) {    try {      url = new URL(urlString);     HttpURLConnection connection = (HttpURLConnection) url.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 = connection.getInputStream();     byte[] buffer = new byte[1024];     int length;     fileOutputStream= new FileOutputStream(path+ File.separator+ imgName);     while ((length = inputStream.read(buffer)) != -1) {      fileOutputStream.write(buffer, 0, length);     }   } catch (Exception e) {     e.printStackTrace();    } finally{    inputStream.close();    fileOutputStream.flush();    fileOutputStream.close();    }  }   } 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 高邮市| 安塞县| 河北省| 东光县| 武乡县| 营口市| 资中县| 郧西县| 和龙市| 枣阳市| 达州市| 肥西县| 益阳市| 安岳县| 梅州市| 缙云县| 嘉鱼县| 潍坊市| 彰化市| 鄂温| 呼图壁县| 宜兰市| 龙山县| 保山市| 金寨县| 铜梁县| 巴东县| 夹江县| 遂宁市| 蒙自县| 怀柔区| 松溪县| 南通市| 信丰县| 长泰县| 巴彦淖尔市| 南宁市| 南宁市| 团风县| 九寨沟县| 蓬溪县|