Java怎么通过URL类下载图片(java,URL,开发技术)

时间:2024-05-02 12:42:41 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

Java通过URL类下载图片

一、概述

URL(Uniform Resource Locator) :统一资源定位符,它表示 Internet 上 某一 资源 的地址。 它是一种具体的 URI ,即 URL 可以用来标识一个资源,而且还指明了如何 locate 这个资源。 通过 URL 我们可以访问 Internet 上的各种网络资源,比如最常见的 www , ftp 站点。浏览器通过解析给定的 URL 可以在网络上查找相应的文件或其他资源。 URL 的基本结构由 5 部分组成: < 传输协议 >://< 主机名 >:< 端口号 >/< 文件名 ># 片段名 ? 参数列表

二、通过URL下载图片

HttpsURLConnectionhttpsURLConnection=null;InputStreamis=null;FileOutputStreamfos=null;try{//1.创建URL对象URLurl=newURL("https://qixn-bj.oss-cn-beijing.aliyuncs.com/seosjz/uploadfile/all/jpg/mel4oq4vgi4.jpg"));//4.输出图片byte[]buffer=newbyte[1024];intlen;while((len=is.read(buffer))!=-1){fos.write(buffer,0,len);}}catch(IOExceptione){e.printStackTrace();}finally{//5.关闭资源try{if(is!=null)is.close();}catch(IOExceptione){e.printStackTrace();}try{if(fos!=null)fos.close();}catch(IOExceptione){e.printStackTrace();}if(httpsURLConnection!=null)httpsURLConnection.disconnect();}

扩展:java通过url获取图片文件

1. 根据url下载Url中的图片

importjava.net.URL;importjava.io.InputStream;importjava.io.FileOutputStream;publicclassImageDownloader{publicstaticvoidmain(String[]args)throwsException{//URLoftheimagetodownloadStringimageUrl="https://qixn-bj.oss-cn-beijing.aliyuncs.com/seosjz/uploadfile/all/jpg/u0ormwt142m.jpg";//CreateURLobjectandopeninputstreamtotheimageURLurl=newURL(imageUrl);InputStreaminputStream=url.openStream();//OutputstreamtosavetheimagetofileFileOutputStreamoutputStream=newFileOutputStream("image.jpg");//Readbytesfromtheinputstreamandwritetotheoutputstreambyte[]buffer=newbyte[2048];intlength;while((length=inputStream.read(buffer))!=-1){outputStream.write(buffer,0,length);}//ClosestreamsinputStream.close();outputStream.close();System.out.println("Imagedownloadedsuccessfully.");}}

2. 根据get请求url下载Url中的图片

importjava.net.URL;importjava.io.InputStream;importjava.io.FileOutputStream;publicclassImageDownloader{publicstaticvoidmain(String[]args)throwsException{//URLoftheimagetodownloadStringimageUrl="https://qixn-bj.oss-cn-beijing.aliyuncs.com/seosjz/uploadfile/all/jpg/u0ormwt142m.jpg";//CreateURLobjectandopeninputstreamtotheimageURLurl=newURL(imageUrl);InputStreaminputStream=url.openStream();//OutputstreamtosavetheimagetofileFileOutputStreamoutputStream=newFileOutputStream("image.jpg");//Readbytesfromtheinputstreamandwritetotheoutputstreambyte[]buffer=newbyte[2048];intlength;while((length=inputStream.read(buffer))!=-1){outputStream.write(buffer,0,length);}//ClosestreamsinputStream.close();outputStream.close();System.out.println("Imagedownloadedsuccessfully.");}}

3. 考虑url中携带中文,需要做转义

imageUrl=URLEncoder.encode(imageUrl,"utf-8").replaceAll("%3A",":").replaceAll("%2F","/").replaceAll("%2C",",").replaceAll("%7B","{").replaceAll("%3F","?").replaceAll("%7D","}").replaceAll("%26","&").replaceAll("%3D","=");//new一个URL对象URLurl=newURL(imageUrl);
 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Java怎么通过URL类下载图片的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Java怎么使用ThreadLocal实现当前登录信息的存取功能下一篇:

5 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18