URL、URLConnection和URLPermission
URL对象代表统一资源定位器,它是对指向互联网“资源”的指针。资源可以是简单的文件或目录,也可以是对更为复杂对象的引用,例如对数据库或搜索引擎的查询。在通常情况下,URL可以由协议名、主机、端口和资源组成。
URL类提供了多个构造器用于创建URL对象,一旦获得了URL对象之后,就可以调用如下方法来访问URL对应的资源。
方法 | |
---|---|
String getFile() | 获取该URL的资源名 |
String getHost() | 获取该URL的主机名 |
String getPath() | 获取该URL的路径部分 |
int getPort() | 获取该URL的端口号 |
String getProtocol() | 获取该URL的协议名称 |
String getQuery() | 获取该URL的查询字符串部分 |
URLConnection openConnection() | 返回一个URLConnection对象,它代表了与URL所引用的远程对象的连接 |
InputStream openStream() | 打开于此URL的连接,并返回一个用于读取该URL资源的InputStream |
测试URL类方法的代码:
public class URLTest {
public static void main(String[] args) throws Exception{
URL url = new URL("http://img2.ph.126.net/Mpr7kRqQ51wTt7KHYmJI6A==/6619508599956323849.jpg");
System.out.println("URL资源名:" + url.getFile());
System.out.println("URl主机名:" + url.getHost());
System.out.println("URL路径:" + url.getPath());
System.out.println("URL端口号:" + url.getPort());
System.out.println("URL协议:" + url.getProtocol());
System.out.println("URL查询字符串部分:" + url.getQuery());
}
}
测试结果:
URL资源名:/Mpr7kRqQ51wTt7KHYmJI6A==/6619508599956323849.jpg
URl主机名:img2.ph.126.net
URL路径:/Mpr7kRqQ51wTt7KHYmJI6A==/6619508599956323849.jpg
URL端口号:-1
URL协议:http
URL查询字符串部分:null
使用openStream()方法可以读取该URL资源的InputStream,通过该方法可以非常方便地读取远程资源——甚至实现多线程下载。
实现步骤:
1.创建URL对象。
2.获取指定URL对象所指向资源的大小(通过getContentLength()方法获得)。
3.在本地磁盘上创建一个与网络资源具有相同大小的空文件。
4.计算每个线程应该下载网络资源的哪个部分(从哪个字节开始,到哪个字节结束)。
5.依次创建、启动多个线程来下载网络资源的指定部分。
下载工具类代码的实现:
public class DownUtil {
//定义下载资源路径
private String path;
//指定所下载的文件保存位置
private String targetFile;
//定义需要使用多少个线程下载资源
private int threadNum;
//定义下载的线程对象
private DownThread[] threads;
//定义下载的文件的总大小
private int fileSize;
public DownUtil(String path, String targetFile, int threadNum){
this.path = path;
this.threadNum = threadNum;
//初始化threads数组
threads = new DownThread[threadNum];
this.targetFile = targetFile;
}
public void download() throws Exception{
URL url = new URL(path);
//URLConnection表示应用程序和URL之间的通信连接
//HttpURLConnection表示与URL之间的http连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//连接超时设置
conn.setConnectTimeout(5 * 1000);
//请求获取Request-URI所标识的资源
conn.setRequestMethod("GET");
//客户端的配置,需求
//setRequestProperty()可以不设置
conn.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, "
+ "application/x-shockwave-flash, application/xaml+xml,"
+ "application/vnd.ms-xpsdocument, application/x-ms-xbap,"
+ "application/x-ms-application, application/vnd.ms-excel,"
+ "application/vnd.ms-powerpoint, application/msword, */*");
//支持语言
conn.setRequestProperty("Accept-Language", "zh-CN");
//返回的字符
conn.setRequestProperty("Charset", "UTF-8");
//连接方式
conn.setRequestProperty("Connection", "Keep-Alive");
//得到文件大小
fileSize = conn.getContentLength();
conn.disconnect();
//每个线程所需下载的大小
int currentPartSize = fileSize / threadNum + 1;
//保存文件
RandomAccessFile file = new RandomAccessFile(targetFile, "rw");
//设置本地文件的大小
file.setLength(fileSize);
file.close();
for(int i = 0; i < threadNum; i++){
//计算每个线程下载的开始位置
int startPos = i * currentPartSize;
//每个线程使用一个RandomAccessFile进行下载
RandomAccessFile currentPart = new RandomAccessFile(targetFile, "rw");
//定位该线程的下载位置
currentPart.seek(startPos);
//创建下载线程
threads[i] = new DownThread(startPos, currentPartSize, currentPart);
//启动下载线程
threads[i].start();
}
}
//获取下载的完成百分比
public double getCompleteRate(){
//统计多个线程已经下载的总大小
int sumSize = 0;
for(int i = 0; i < threadNum; i++){
sumSize += threads[i].length;
}
//返回已经完成的百分比
return sumSize * 1.0 / fileSize;
}
private class DownThread extends Thread{
//当前线程的下载位置
private int startPos;
//定义当前线程负责下载的文件大小
private int currentPartSize;
//当前线程需要下载的文件块
private RandomAccessFile currentPart;
//定义该线程已下载的字节数
private int length;
public DownThread(int startPos, int currentPartSize, RandomAccessFile currentPart) {
this.startPos = startPos;
this.currentPartSize = currentPartSize;
this.currentPart = currentPart;
}
public void run(){
try{
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//连接超时设置
conn.setConnectTimeout(5 * 1000);
//请求获取Request-URI所标识的资源
conn.setRequestMethod("GET");
//客户端的配置,需求
conn.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, "
+ "application/x-shockwave-flash, application/xaml+xml,"
+ "application/vnd.ms-xpsdocument, application/x-ms-xbap,"
+ "application/x-ms-application, application/vnd.ms-excel,"
+ "application/vnd.ms-powerpoint, application/msword, */*");
//支持语言
conn.setRequestProperty("Accept-Language", "zh-CN");
//返回的字符
conn.setRequestProperty("Charset", "UTF-8");
InputStream inStream = conn.getInputStream();
//跳过startPos个字节,表明该线程只下载自己负责的那部分文件
inStream.skip(this.startPos);
byte[] buffer = new byte[1024];
int hasRead = 0;
//读取网络数据,并写入本地文件
while (length < currentPartSize &&
(hasRead = inStream.read(buffer)) != -1){
currentPart.write(buffer, 0, hasRead);
//累计该线程下载的总大小
length += hasRead;
}
currentPart.close();
inStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
主程序代码:
public class MultiThreadDown {
public static void main(String[] args) throws Exception{
//初始化DownUtil对象
final DownUtil downUtil = new DownUtil("http://s1.dwstatic.com/group1/"
+ "M00/65/09/5e4f2b1472094ccaf7b7f2fda38b01d9.gif",
"1.png", 4);
downUtil.download();
new Thread(() -> {
while(downUtil.getCompleteRate() < 1){
//每隔0.1秒查询一次任务得完成速度
//GUI程序中可根据该进度来绘制进度条
System.out.println("已完成:" + downUtil.getCompleteRate());
try{
Thread.sleep(1000);
}catch (Exception e){}
}
}).start();;
}
}
运行程序可以下载到一张图片。