当前位置: 首页 > news >正文

http协议get方法获取图片并保存到本地

  1. public class HttpUtils {  
  2.   
  3.         //url地址,用这个地址创建URL对象。  
  4.         private  static String url_path ="http://192.168.10.101:8080/myhttp/bg.jpg";  
  5.         //这个函数负责把获取到的InputStream流保存到本地。  
  6.         public static void saveImageToDisk() {  
  7.             InputStream inputStream = null;  
  8.             inputStream = getInputStream();//调用getInputStream()函数。  
  9.             byte[] data = new byte[1024];  
  10.             int len = 0;  
  11.               
  12.             FileOutputStream fileOutputStream = null;  
  13.             try {  
  14.                 fileOutputStream = new FileOutputStream("D:\\test.jpg");//初始化一个FileOutputStream对象。  
  15.                 while ((len = inputStream.read(data))!=-1) {//循环读取inputStream流中的数据,存入文件流fileOutputStream  
  16.                     fileOutputStream.write(data,0,len);  
  17.                 }  
  18.                   
  19.             } catch (FileNotFoundException e) {  
  20.                 // TODO Auto-generated catch block  
  21.                 e.printStackTrace();  
  22.             } catch (IOException e) {  
  23.                 // TODO Auto-generated catch block  
  24.                 e.printStackTrace();  
  25.             }finally{//finally函数,不管有没有异常发生,都要调用这个函数下的代码  
  26.                 if(fileOutputStream!=null){  
  27.                     try {  
  28.                         fileOutputStream.close();//记得及时关闭文件流  
  29.                     } catch (IOException e) {  
  30.                         // TODO Auto-generated catch block  
  31.                         e.printStackTrace();  
  32.                     }  
  33.                 }  
  34.                 if(inputStream!=null){  
  35.                     try {  
  36.                         inputStream.close();//关闭输入流  
  37.                     } catch (IOException e) {  
  38.                         // TODO Auto-generated catch block  
  39.                         e.printStackTrace();  
  40.                     }  
  41.                 }  
  42.             }  
  43.               
  44.               
  45.               
  46.         }  
  47.           
  48.           
  49.         public static InputStream getInputStream(){  
  50.             InputStream inputStream = null;  
  51.             HttpURLConnection httpURLConnection = null;  
  52.             try {  
  53.                 URL url = new URL(url_path);//创建的URL  
  54.                 if (url!=null) {  
  55.                     httpURLConnection = (HttpURLConnection) url.openConnection();//打开链接  
  56.                     httpURLConnection.setConnectTimeout(3000);//设置网络链接超时时间,3秒,链接失败后重新链接  
  57.                     httpURLConnection.setDoInput(true);//打开输入流  
  58.                     httpURLConnection.setRequestMethod("GET");//表示本次Http请求是GET方式  
  59.                     int responseCode = httpURLConnection.getResponseCode();//获取返回码  
  60.                     if (responseCode == 200) {//成功为200  
  61.                         //从服务器获得一个输入流  
  62.                         inputStream = httpURLConnection.getInputStream();  
  63.                     }  
  64.                 }  
  65.                   
  66.                   
  67.             } catch (MalformedURLException e) {  
  68.                 // TODO Auto-generated catch block  
  69.                 e.printStackTrace();  
  70.             } catch (IOException e) {  
  71.                 // TODO Auto-generated catch block  
  72.                 e.printStackTrace();  
  73.             }  
  74.             return inputStream;  
  75.               
  76.         }  
  77.         //主函数  
  78.         public static void main(String[] args) {  
  79.             //从服务器获得图片,保存到本地  
  80.             saveImageToDisk();  
  81.         }  
  82.           
  83.     }  


注:http的get方式提交请求的流程:

创建URL;

1)定义InputStream流对象,定义HttpUrlConnection对象;

2)用URL的openConnection函数打开链接返回HttpUrlConnection对象;

3)HttpUrlConnection对象的setConnectTimeOut函数设置链接超时时间;

4)HttpUrlConnection对象的setDoInput函数打开输入流;

5)HttpUrlConnection对象的setRequestMethod函数设置链接方式,即GET或者POST方式;

6)HttpUrlConnection对象的getResponseCode函数返回链接结果,结果为int类型;

结果有这么几种常见的可能:200,请求成功;404资源未找到;500,服务器内部错误;

详见: http状态码/http返回码详解



二.服务器端

http://blog.csdn.net/u012702039/article/details/42224721

以上链接的基础上,在WebContent文件夹下放置一个bg.jpg图片,运行起来,作为服务器端。

如图:


然后再运行客户端程序,即可把服务器端的bg.jpg图片保存为d盘的test.jpg文件。


转载于:https://www.cnblogs.com/Obelia/p/7256248.html

相关文章:

  • php string
  • 软工_个人博客作业1
  • iOS 没有安装对应客户端,不应显示对应的图标
  • New Year Transportation(水)
  • 解决EditText和ScrollView滑动冲突问题
  • (转)fock函数详解
  • linux TLB表
  • 基于范围的for循环(STL)
  • bzoj1221: [HNOI2001] 软件开发
  • Python小杂点
  • nginx在 window下 自动退出 php-cgi
  • MongoDB 常用命令
  • 使用异或解题 —— 序列中仅出现一次的两个数
  • 为什么我从来不无偿加班?你也不应该! 【转载】
  • MySQL主从同步配置(Ubuntu)
  • @angular/forms 源码解析之双向绑定
  • “大数据应用场景”之隔壁老王(连载四)
  • 【剑指offer】让抽象问题具体化
  • GraphQL学习过程应该是这样的
  • PHP 7 修改了什么呢 -- 2
  • React的组件模式
  • SQL 难点解决:记录的引用
  • 不上全站https的网站你们就等着被恶心死吧
  • 工作中总结前端开发流程--vue项目
  • 基于OpenResty的Lua Web框架lor0.0.2预览版发布
  • 简单数学运算程序(不定期更新)
  • 码农张的Bug人生 - 初来乍到
  • 每天一个设计模式之命令模式
  • 驱动程序原理
  • 视频flv转mp4最快的几种方法(就是不用格式工厂)
  • 我是如何设计 Upload 上传组件的
  • 携程小程序初体验
  • 一个普通的 5 年iOS开发者的自我总结,以及5年开发经历和感想!
  • HanLP分词命名实体提取详解
  • ​iOS安全加固方法及实现
  • #include
  • (4)通过调用hadoop的java api实现本地文件上传到hadoop文件系统上
  • (51单片机)第五章-A/D和D/A工作原理-A/D
  • (JS基础)String 类型
  • (动手学习深度学习)第13章 计算机视觉---微调
  • (附源码)计算机毕业设计ssm电影分享网站
  • (六)软件测试分工
  • (篇九)MySQL常用内置函数
  • (三)mysql_MYSQL(三)
  • (算法)求1到1亿间的质数或素数
  • (转)fock函数详解
  • .“空心村”成因分析及解决对策122344
  • .NET 5.0正式发布,有什么功能特性(翻译)
  • .NET Conf 2023 回顾 – 庆祝社区、创新和 .NET 8 的发布
  • .NET CORE 3.1 集成JWT鉴权和授权2
  • .NET Core WebAPI中封装Swagger配置
  • .net 前台table如何加一列下拉框_如何用Word编辑参考文献
  • .NET 同步与异步 之 原子操作和自旋锁(Interlocked、SpinLock)(九)
  • @JsonSerialize注解的使用
  • [ 攻防演练演示篇 ] 利用通达OA 文件上传漏洞上传webshell获取主机权限