java读取网络上的资源保存本地
来源:互联网 发布:用友软件庆阳总代理 编辑:程序博客网 时间:2024/05/17 03:22
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class Getpic{
public Getpic(){
}
public static boolean saveUrlAs(String fileUrl, String savePath)/*fileUrl网络资源地址*/
{
try
{
URL url = new URL(fileUrl);/*将网络资源地址传给,即赋值给url*/
/*此为联系获得网络资源的固定格式用法,以便后面的in变量获得url截取网络资源的输入流*/
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
DataInputStream in = new DataInputStream(connection.getInputStream());
/*此处也可用BufferedInputStream与BufferedOutputStream*/
DataOutputStream out = new DataOutputStream(new FileOutputStream(savePath));
/*将参数savePath,即将截取的图片的存储在本地地址赋值给out输出流所指定的地址*/
byte[] buffer = new byte[4096];
int count = 0;
while ((count = in.read(buffer)) > 0)/*将输入流以字节的形式读取并写入buffer中*/
{
out.write(buffer, 0, count);
}
out.close();/*后面三行为关闭输入输出流以及网络资源的固定格式*/
in.close();
connection.disconnect();
return true;/*网络资源截取并存储本地成功返回true*/
}
catch (Exception e)
{
System.out.println(e + fileUrl + savePath);
return false;
}
}
public static void main(String[] args)
{
Getpic pic = new Getpic();/*创建实例*/
String photoUrl =
http://hiphotos.baidu.com/yanshennan/pic/item/03a505c8bcbaf6557f3e6f8a.jpg";
/*photoUrl.substring(photoUrl.lastIndexOf("/")的方法将返回最后一个符号为
* ‘/’后photoUrl变量中的所有字符,包裹此自身符号*/
String fileName = photoUrl.substring(photoUrl.lastIndexOf("/"));
String filePath = "E:";
/*调用函数,并且进行传参*/
boolean flag = pic.saveUrlAs(photoUrl, filePath + fileName);
System.out.println("Run ok!\n Get URL file " + flag);
System.out.println(filePath);
System.out.println(fileName);
}
}
运行结果:
Run ok!
Get URL file true
E:
/03a505c8bcbaf6557f3e6f8a.jpg
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class Getpic{
public Getpic(){
}
public static boolean saveUrlAs(String fileUrl, String savePath)/*fileUrl网络资源地址*/
{
try
{
URL url = new URL(fileUrl);/*将网络资源地址传给,即赋值给url*/
/*此为联系获得网络资源的固定格式用法,以便后面的in变量获得url截取网络资源的输入流*/
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
DataInputStream in = new DataInputStream(connection.getInputStream());
/*此处也可用BufferedInputStream与BufferedOutputStream*/
DataOutputStream out = new DataOutputStream(new FileOutputStream(savePath));
/*将参数savePath,即将截取的图片的存储在本地地址赋值给out输出流所指定的地址*/
byte[] buffer = new byte[4096];
int count = 0;
while ((count = in.read(buffer)) > 0)/*将输入流以字节的形式读取并写入buffer中*/
{
out.write(buffer, 0, count);
}
out.close();/*后面三行为关闭输入输出流以及网络资源的固定格式*/
in.close();
connection.disconnect();
return true;/*网络资源截取并存储本地成功返回true*/
}
catch (Exception e)
{
System.out.println(e + fileUrl + savePath);
return false;
}
}
public static void main(String[] args)
{
Getpic pic = new Getpic();/*创建实例*/
String photoUrl =
http://hiphotos.baidu.com/yanshennan/pic/item/03a505c8bcbaf6557f3e6f8a.jpg";
/*photoUrl.substring(photoUrl.lastIndexOf("/")的方法将返回最后一个符号为
* ‘/’后photoUrl变量中的所有字符,包裹此自身符号*/
String fileName = photoUrl.substring(photoUrl.lastIndexOf("/"));
String filePath = "E:";
/*调用函数,并且进行传参*/
boolean flag = pic.saveUrlAs(photoUrl, filePath + fileName);
System.out.println("Run ok!\n Get URL file " + flag);
System.out.println(filePath);
System.out.println(fileName);
}
}
运行结果:
Run ok!
Get URL file true
E:
/03a505c8bcbaf6557f3e6f8a.jpg
- java读取网络上的资源保存本地
- Java从网络上读取图片并保存至本地
- java读取网络上的图片并保存到本地,图片文件名不变
- Java使用URL读取网络图片资源 并保存在本地
- 【Java】Java从网络读取图片并保存至本地
- Java从网络读取图片并保存至本地
- Java从网络读取图片并保存至本地 .
- Java从网络读取图片并保存至本地
- Java从网络读取图片并保存至本地
- Java从网络读取图片并保存至本地
- 使用Java读取网络图片并保存至本地
- Java从网络读取图片并保存至本地
- Java从网络读取图片并保存至本地
- Java从网络读取图片并保存至本地
- Java从网络(http)读取图片并保存至本地
- java 下载网络上的图片并保存到本地目录
- 【Java】下载网络上的图片并保存到本地目录
- 保存网络上的图片到本地计算机
- http://tech.it168.com/zt/sql2005/ sql2005专题网站
- nonatomic atomic 属性声明的作用(以 附带 retain关键字举例)iOS开发
- ubuntu server 使用手记
- Android短彩信收发流程(Framework)
- C#与加密
- java读取网络上的资源保存本地
- DotNetBar教程
- 改改改,又不知道改了多少遍!!!!!!
- 11g新特性:database replay
- 四川2名干部任前公示 王菲拟提名为广元市市长-四川-广元-任前公示
- 使用FULL OUTER JOIN拼接多表数据
- 计算定期还款额度
- MIPS busybox
- 生活贷款的问题。