Http协议Get方式获取图片
一、

二、

我试了试,Post方式也行啊,干嘛要叫强调Get方式,费解~~
答曰:get是向服务器请求数据,post是提交数据。
三、
package com.hpu.test; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL; public class HttpUtils {
// 自定义的web服务器的资源
private static String URL_PATH = "http://localhost:8080/TestGet/1.jpg"; public HttpUtils() {
// TODO Auto-generated constructor stub
} public static void saveImageToDisk() throws IOException {
InputStream inputStream = getInputStream();
byte[] data = new byte[1024];
int len = 0;
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream("D:\\p.jpg");
while ((len = inputStream.read(data)) != -1) {
fileOutputStream.write(data, 0, len);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} } /**
* 获得服务器端数据,以InputStream形式返回
*
* @return
* @throws IOException
*/
public static InputStream getInputStream() throws IOException {
InputStream inputStream = null;
HttpURLConnection httpURLConnection = null;
try {
URL url = new URL(URL_PATH);
if (url != null) {
httpURLConnection = (HttpURLConnection) url.openConnection();
// 设置连接网络的超时时间
httpURLConnection.setConnectTimeout(3000);
httpURLConnection.setDoInput(true);
// 设置本次http请求使用get方式请求
httpURLConnection.setRequestMethod("GET");
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == 200) {
// 从服务器获得一个输入流
inputStream = httpURLConnection.getInputStream();
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return inputStream;
} public static void main(String[] args) throws IOException {
// 从服务器获得图片保存到本地
saveImageToDisk();
System.out.println("传输步骤完毕");
}
}
四、IO学习链接
http://www.cnblogs.com/hxsyl/p/3302852.html
Http协议Get方式获取图片的更多相关文章
- java分别通过httpclient和HttpURLConnection获取图片验证码内容
前面的文章,介绍了如何通过selenium+Tesseract-OCR来识别图片验证码,如果用接口来访问的话,再用selenium就闲的笨重,下面就介绍一下分别通过httpclient和HttpURL ...
- Java通过图片url地址获取图片base64位字符串的两种方式
工作中遇到通过图片的url获取图片base64位的需求.一开始是用网上的方法,通过工具类Toolkit,虽然实现的代码比较简短,不过偶尔会遇到图片转成base64位不正确的情况,至今不知道为啥. 之后 ...
- 使用selenium的方式获取网页中图片的链接和网页的链接,来判断是否是死链(二)
上一篇使用Java正则表达式来判断和获取图片的链接以及跳转的网址,这篇使用selenium的自带的API(getAttribute)来获取网页中指定的内容 实现内容:获取下面所有图片的链接地址以及跳转 ...
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
相册 iphone的相册包含摄像头胶卷+用户计算机同步的部分照片.用户可以通过UIImagePickerController类提供的交互对话框来从相册中选择图像.但是,注意:相册中的图片机器路径无法直 ...
- C#获取图片的后缀名
最近在学习过程中遇到一个问题,就是如何获取图片的格式,对于一张知道全路径的照片,如果其路径包含后缀名的话,要取得后缀名,只需要一行代码即可: var ext = System.IO.Path.GetE ...
- 安卓学习----使用okHttp(get方式)---下载图片
一首先下载Jar包 https://github.com/square/okhttp 如果使用android studio只需要加入依赖 compile 'com.squareup.okhttp3:o ...
- js和jquery如何获取图片真实的宽度和高度
按照插入的图片的尺寸来判断图片是横图还是竖图.然后判断过后给予不同的展示方式,下面为大家介绍下js和jquery如何获取图片真实的宽度和高度 1.什么时候需要获取图片真实的宽度和高度 在做pc网页 ...
- 限制Xamarin获取图片的大小
限制Xamarin获取图片的大小在App开发中,经常会使用网络图片.因为这样不仅可以减少App的大小,还可以动态更新图片.但是手机使用网络环境千差万别.当网络环境不是理想的情况下,加载网络图片就是一个 ...
- js和jquery获取图片真实的宽度和高度
1.什么时候需要获取图片真实的宽度和高度 在做pc网页的时候,有时候会考虑按照插入的图片的尺寸来判断图片是横图还是竖图.然后判断过后给予不同的展示方式! 另外一种就是在手机页面上,在新闻页插入的图片往 ...
随机推荐
- Kafka 0.8: 多日志文件夹机制
kafka 0.7.2 中对log.dir的定义如下: log.dir none Specifies the root directory in which all log data is kept. ...
- Android开发之线程池使用总结
线程池算是Android开发中非常常用的一个东西了,只要涉及到线程的地方,大多数情况下都会涉及到线程池.Android开发中线程池的使用和Java中线程池的使用基本一致.那么今天我想来总结一下Andr ...
- 安装完Kali linux 之后 需要配置的一些 常用软件 记录
apt 阿里云开源镜像站 deb http://mirrors.aliyun.com/kali sana main non-free contribdeb http://mirrors.aliyun. ...
- 读取properties文件
假设项目名称为myproject public class UtilConfig { private static final Properties prop; static { prop = new ...
- Magento强大的配置系统
Magento的配置系统就像是Magento的心脏,支撑着Magento的运行.这套配置系统掌管着几乎所有"module/model/class/template/etc".它把整 ...
- Base64 encode/decode large file
转载:http://www.cnblogs.com/jzywh/archive/2008/04/20/base64_encode_large_file.html The class System.Co ...
- meteor中分页库alethes:pages用法汇总
1.添加分页库: meteor add alethes:pages 2.新建分页: Pages = new Meteor.Pagination("collection-name") ...
- nodejs-日常练习记录-使用express搭建static服务器.
cd C:\wxg\test\node_demo\myapp nvmw use 0.12.1 node static.js var express = require('express'); var ...
- Core模块其他常用知识点[OpenCV 笔记14]
Matx 轻量级的Mat,必须在使用前规定好大小,比如一个2x3的float型的Matx,可以声明为Matx23f Vec Vec是Matx的一个派生类,是一个一维的Matx,跟vector很相似.在 ...
- centos 安装php-fpm , nginx二级域名配置 ,但为什么必须要 域名提供商 哪里解析新的二级域名一下 才能用呢?
yum -y install php-fpm php-mysql(当然还有其它扩展) /etc/init.d/php-fpm restart (重启php-fpm) /etc/php.ini (php ...