根据URL获取文件,以采集网络图片为例
2017-08-24 11:35
525 查看
此方法可以采集图片,音频,视频等资源
public String downLoadFromUrl(String urlStr, String fileName, String savePath) throws IOException {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置超时间为3秒
conn.setConnectTimeout(5 * 1000);
// 防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
// 得到输入流
InputStream inputStream = conn.getInputStream();
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
String type = HttpURLConnection.guessContentTypeFromStream(inputStream);
int flagIndex = type.indexOf("/");
String imageType = type.substring(flagIndex + 1);
if (!imageType.equals("gif")) {
imageType = "jpg";
}
// 获取自己数组
byte[] getData = readInputStream(inputStream);
// 文件保存位置
File saveDir = new File(savePath);
if (!saveDir.exists()) {
saveDir.mkdir();
}
File file = new File(saveDir + File.separator + fileName + "." + imageType);
FileOutputStream fos = new FileOutputStream(file);
fos.write(getData);
if (fos != null) {
fos.flush();
fos.close();
}
if (inputStream != null) {
inputStream.close();
}
log.error("info:" + savePath + "/" + fileName + "." + imageType + " download success");
return imageType;
}
public byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}
public String downLoadFromUrl(String urlStr, String fileName, String savePath) throws IOException {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置超时间为3秒
conn.setConnectTimeout(5 * 1000);
// 防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
// 得到输入流
InputStream inputStream = conn.getInputStream();
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
String type = HttpURLConnection.guessContentTypeFromStream(inputStream);
int flagIndex = type.indexOf("/");
String imageType = type.substring(flagIndex + 1);
if (!imageType.equals("gif")) {
imageType = "jpg";
}
// 获取自己数组
byte[] getData = readInputStream(inputStream);
// 文件保存位置
File saveDir = new File(savePath);
if (!saveDir.exists()) {
saveDir.mkdir();
}
File file = new File(saveDir + File.separator + fileName + "." + imageType);
FileOutputStream fos = new FileOutputStream(file);
fos.write(getData);
if (fos != null) {
fos.flush();
fos.close();
}
if (inputStream != null) {
inputStream.close();
}
log.error("info:" + savePath + "/" + fileName + "." + imageType + " download success");
return imageType;
}
public byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}
相关文章推荐
- Android中如何根据图片url路径来获取网络图片
- Android中如何根据图片url路径来获取网络图片
- Android中如何根据图片url路径来获取网络图片
- 根据网络url图片路径获取图片字节数组工具类
- 根据图片的网络URL获取Bitmap
- Android 通过url获取网络图片并显示在ImageView中
- Wininetdll函数库:根据url获取缓存文件
- Android 如何根据网络地址获取网络图片方法
- 服务器端根据数据库图片的url获取保存的图片并进行缩略后返回给客户端-压缩之Thumbnails
- iOS 根据图片URL从本地相册获取图片
- Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)
- .net客户端根据url获取字符串及图片,并保存本地
- 根据图片在IE中的URL得到图片文件在本地磁盘中的Path
- python 使用 urllib.urlretrieve()下载网络图片,在本地打开提示文件损坏无法打开
- 根据identifier从StoryBoard中获取对象,UIButton的图片文件位置
- Wininetdll函数库:根据url获取缓存文件
- URL转Drawable之 Android中获取网络图片的三种方法
- android 网络编程--URL获取数据/图片
- 服务器端根据数据库图片的url获取保存的图片并进行缩略后返回给客户端-压缩之Thumbnails
- Android根据文件路径获取图片的名字