HttpURLConnection根据URL下载图片
2015-07-14 08:08
831 查看
来看下最简单的根据URL下载图片,此方法在实际开发中,不建议使用,因为有些图片是下载不了的,比如百度上的一些图片,返回http的响应码是405
[java] view
plaincopy
package cn.ztz.test;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
public class HttpDownLoad {
public static void download(String url, String dir,String fileName) {
HttpURLConnection httpURLConnection = null;
OutputStream out = null;
InputStream in = null;
try {
URL sendUrl = new URL(url);
httpURLConnection = (HttpURLConnection) sendUrl.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("contentType", "utf-8");
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setConnectTimeout(3000);
httpURLConnection.setReadTimeout(3000);
httpURLConnection.setRequestProperty(
"User-agent",InetAddress.getLocalHost().getHostAddress() + ":"
+ System.getProperty("catalina.home"));
out = httpURLConnection.getOutputStream();
// 清空缓冲区数据
out.flush();
// 获取HTTP状态码
int httpStatusCode = httpURLConnection.getResponseCode();
if(httpStatusCode!=200){
throw new RuntimeException("异常");
}
in = httpURLConnection.getInputStream();
// 获取文件长度
int len = httpURLConnection.getContentLength();
// 路径+文件名
String pathAndName = dir + File.separator + fileName;
// 保存文件
saveFileByByte(in, pathAndName, len);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
}
if (in != null) {
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (httpURLConnection != null) {
httpURLConnection.disconnect();
httpURLConnection = null;
}
}
}
//写文件
private static void saveFileByByte(InputStream in, String path, int len)
throws Exception {
byte[] byteDatas = new byte[len];
BufferedOutputStream bw = null;
try {
// 创建文件对象
File f = new File(path);
// 创建文件路径
if (!f.getParentFile().exists())
f.getParentFile().mkdirs();
// 写入文件
bw = new BufferedOutputStream(new FileOutputStream(path));
int bytesRead = 0;
while ((bytesRead = in.read(byteDatas, 0, byteDatas.length)) != -1) {
bw.write(byteDatas, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
try {
if (bw != null)
bw.close();
} catch (Exception e) {
throw e;
}
}
}
}
[java] view
plaincopy
package cn.ztz.test;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
public class HttpDownLoad {
public static void download(String url, String dir,String fileName) {
HttpURLConnection httpURLConnection = null;
OutputStream out = null;
InputStream in = null;
try {
URL sendUrl = new URL(url);
httpURLConnection = (HttpURLConnection) sendUrl.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("contentType", "utf-8");
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setConnectTimeout(3000);
httpURLConnection.setReadTimeout(3000);
httpURLConnection.setRequestProperty(
"User-agent",InetAddress.getLocalHost().getHostAddress() + ":"
+ System.getProperty("catalina.home"));
out = httpURLConnection.getOutputStream();
// 清空缓冲区数据
out.flush();
// 获取HTTP状态码
int httpStatusCode = httpURLConnection.getResponseCode();
if(httpStatusCode!=200){
throw new RuntimeException("异常");
}
in = httpURLConnection.getInputStream();
// 获取文件长度
int len = httpURLConnection.getContentLength();
// 路径+文件名
String pathAndName = dir + File.separator + fileName;
// 保存文件
saveFileByByte(in, pathAndName, len);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
}
if (in != null) {
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (httpURLConnection != null) {
httpURLConnection.disconnect();
httpURLConnection = null;
}
}
}
//写文件
private static void saveFileByByte(InputStream in, String path, int len)
throws Exception {
byte[] byteDatas = new byte[len];
BufferedOutputStream bw = null;
try {
// 创建文件对象
File f = new File(path);
// 创建文件路径
if (!f.getParentFile().exists())
f.getParentFile().mkdirs();
// 写入文件
bw = new BufferedOutputStream(new FileOutputStream(path));
int bytesRead = 0;
while ((bytesRead = in.read(byteDatas, 0, byteDatas.length)) != -1) {
bw.write(byteDatas, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
try {
if (bw != null)
bw.close();
} catch (Exception e) {
throw e;
}
}
}
}
相关文章推荐
- HTTP请求方式
- HTTP 错误 500.21 - Internal Server Error
- XUtils HttpUtils 优化线程 防止无限访问网络
- HTTP 笔记与总结(5)socket 编程:使用 HTTP 协议模拟登录并发帖
- js测试网络连通情况
- Android 利用Fiddler进行网络数据抓包
- HTTPS那些事 用java实现HTTPS工作原理
- httpFS访问
- Linx监控分享--网络状态监控+邮件提醒
- 网络配置之ifconfig及Ip命令详解
- NS3网络仿真(7): Wifi节点
- 高效排除网络故障 从用好Ping命令开始
- NS3网络仿真(6): 总线型网络
- linux系统网络服务命令(一)
- 基于SPI的LSP网络封包截获
- 知识点——网络
- socket网络编程基础小记
- 入侵某邮网络例子
- HTTP长链接
- PRML5-神经网络(1)