java获取网络图片上传到OSS的方法

作者:李秀才 时间:2023-10-14 23:01:07 

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

}

来源:https://blog.csdn.net/qq_33556185/article/details/79152679

标签:java,获取图片,上传
0
投稿

猜你喜欢

  • JVM类加载,垃圾回收

    2022-08-16 02:44:36
  • C#中的IEnumerable简介及简单实现实例

    2023-07-15 22:27:07
  • Java连接Linux服务器过程分析(附代码)

    2023-05-28 19:57:09
  • Java调用groovy实现原理代码实例

    2023-05-16 16:41:51
  • Android UI 实现老虎机详解及实例代码

    2022-06-09 05:01:30
  • Java编程用指定字符打印菱形实例

    2022-02-23 05:49:09
  • java处理数据库不支持的emoji表情符问题解决

    2021-08-21 00:16:58
  • Android实现recyclerview城市字母索引列表

    2023-09-28 04:22:06
  • Java中为什么start方法不能重复调用而run方法可以?

    2023-11-15 03:04:02
  • 使用String类型小数值转换为Long类型

    2023-04-14 10:34:56
  • 详解SpringBoot中的统一功能处理的实现

    2022-07-06 12:14:49
  • Android开发笔记之:对实践TDD的一些建议说明

    2023-11-25 11:45:18
  • Java与kotlin详细对比

    2022-04-13 23:26:47
  • C#实现观察者模式(Observer Pattern)的两种方式

    2023-06-20 21:05:18
  • spring的xml文件打开没有namespace等操作选项的解决方案

    2022-09-10 14:56:47
  • C#获取Word文档中所有表格的实现代码分享

    2023-05-12 10:01:46
  • mybatis自定义类型处理器TypehHandler示例详解

    2023-10-11 04:30:40
  • SpringBoot项目在IntelliJ IDEA中如何实现热部署

    2023-10-29 13:30:22
  • android使用SwipeRefreshLayout实现ListView下拉刷新上拉加载

    2022-07-15 08:39:50
  • springBoot controller,service,dao,mapper,model层的作用说明

    2022-02-28 15:38:49
  • asp之家 软件编程 m.aspxhome.com