HttpUtils--Get
来源:互联网 发布:pdg转pdf mac 编辑:程序博客网 时间:2024/05/16 15:18
package com.my.http;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Savepoint;
public class HttpUtils {
private static String URL_PATH="http://192.168.0.211:8080/httpServer/abc.jpg";
static HttpURLConnection httpUrlConnection=null;
public static InputStream getInputStream(){
InputStream inputStream=null;
try {
URL url=new URL(URL_PATH);
try {
httpUrlConnection=(HttpURLConnection)url.openConnection();
httpUrlConnection.setConnectTimeout(3000);
httpUrlConnection.setRequestMethod("GET");
httpUrlConnection.setDoInput(true);
int httpResponseCode=httpUrlConnection.getResponseCode();
if(httpResponseCode==200){
inputStream=httpUrlConnection.getInputStream();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return inputStream;
}
public static void saveImageToDisk() throws IOException
{
InputStream inputStream=getInputStream();
FileOutputStream fileOutputStream=null;
byte data[]=new byte[1024];
int lenth=0;
try {
fileOutputStream=new FileOutputStream("D:/abc.png");
while((lenth=inputStream.read(data))!=-1){
fileOutputStream.write(data, 0, lenth);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(inputStream!=null){
inputStream.close();
}
if(fileOutputStream!=null){
fileOutputStream.close();
}
}
}
public static void main(String[] args) throws IOException {
saveImageToDisk();
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Savepoint;
public class HttpUtils {
private static String URL_PATH="http://192.168.0.211:8080/httpServer/abc.jpg";
static HttpURLConnection httpUrlConnection=null;
public static InputStream getInputStream(){
InputStream inputStream=null;
try {
URL url=new URL(URL_PATH);
try {
httpUrlConnection=(HttpURLConnection)url.openConnection();
httpUrlConnection.setConnectTimeout(3000);
httpUrlConnection.setRequestMethod("GET");
httpUrlConnection.setDoInput(true);
int httpResponseCode=httpUrlConnection.getResponseCode();
if(httpResponseCode==200){
inputStream=httpUrlConnection.getInputStream();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return inputStream;
}
public static void saveImageToDisk() throws IOException
{
InputStream inputStream=getInputStream();
FileOutputStream fileOutputStream=null;
byte data[]=new byte[1024];
int lenth=0;
try {
fileOutputStream=new FileOutputStream("D:/abc.png");
while((lenth=inputStream.read(data))!=-1){
fileOutputStream.write(data, 0, lenth);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(inputStream!=null){
inputStream.close();
}
if(fileOutputStream!=null){
fileOutputStream.close();
}
}
}
public static void main(String[] args) throws IOException {
saveImageToDisk();
}
}
0 0
- HttpUtils--Get
- 【Http】httputils实现get请求
- Java:HttpUtils :发送get/post请求
- HttpUtils
- HttpUtils
- HttpUtils
- HttpUtils
- HttpUtils
- HttpUtils
- HttpUtils
- HttpUtils
- HttpUtils
- HttpUtils
- Httputils
- httpUtils
- HttpUtils
- HttpUtils
- HttpUtils
- js 如何验证上传文件的类型?
- android实用代码片段
- Serlvet/JSP中forward、include、sendRedirect的区别
- avstream mini driver的inf示例(usb)
- 物联网,你的信息安全吗?——第1期中国物联网信息安全主题沙龙在京顺利召开。
- HttpUtils--Get
- 使用MyEclipse中调用javadoc的问题
- Spring发送邮件的简单例子
- 求数值(乘法)
- LEADTOOLS使用教程:创建OCR引擎实例
- iOS多语言环境支持设计
- SEO是一种战前工作,不论从事什么工作,在做之前都要了解它的特征
- ShareSDK For COCOS2D-X 专用快速集成组件发布
- MT4 hst格式文件的读取