一、开发环境及工具
开发环境及工具
IDEA
JDK8
UTF-8
二、包名概览
constant:存放常量类的包
core:存放了下载器核心类的包
util:存放工具类的包
Main:主类
三、项目结构
Main是主类,主函数在其中。
四、使用步骤
1.创建一个空项目
2.编写代码
Main.java:
package com.downloader;
import com.downloader.core.Downloader;
import java.util.Scanner;
public class Main {
/*主类,程序的入口*/
public static void main(String[] args) {
String url=null;//用来放下载连接的地址。
if(args!=null && args.length!=0){
//判断主函数的传入字符数组是否为空
url=args[0];//如果不为空,对url进行赋值arg[0]
}else {
while (true){
//主函数传入字符数组为空,进入while循环
System.out.println("请输入下载文件的地址。");
Scanner scanner=new Scanner(System.in);//在控制台中获取输入的信息。
url=scanner.next();//对url进行赋值,为控制台中输入的信息。
if(url!=null){
//如果url不为空,结束while循环。
break;
}
}
}
Downloader downloader = new Downloader();
downloader.download(url);
}
}
HttpUtils.java:
package com.downloader.util;
import java.io.IOException;
import java.net.*;
/*http相关工具类*/
public class HttpUtils {
/*获取HttpURLConnection连接对象*/
public static HttpURLConnection getHttpURLConnection(String url) throws IOException {
URL httpUrl = new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection)httpUrl.openConnection();
//向文件所在的服务器发送标识信息
urlConnection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1");
return urlConnection;
}
/*获取下载文件的名称*/
public static String getHttpFileName(String url){
int indexOf = url.lastIndexOf("/");
return url.substring(indexOf+1);
}
}
Downloader.java:
package com.downloader.core;
import com.downloader.constant.Constant;
import com.downloader.util.HttpUtils;
import com.sun.imageio.spi.FileImageOutputStreamSpi;
import java.io.*;
import java.net.HttpURLConnection;
/*下载器*/
public class Downloader {
public void download(String url){
//获取文件名
String httpFileName = HttpUtils.getHttpFileName(url);
//文件保存路径
httpFileName= Constant.PATH+httpFileName;
//获取连接对象
HttpURLConnection httpURLConnection =null;
try {
httpURLConnection = HttpUtils.getHttpURLConnection(url);
} catch (IOException e) {
e.printStackTrace();
}
try (
InputStream inputStream = httpURLConnection.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
FileOutputStream fileOutputStream = new FileOutputStream(httpFileName);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
){
int len=-1;
while ((len=bufferedInputStream.read())!=-1){
bufferedOutputStream.write(len);
}
} catch (FileNotFoundException e) {
System.out.println("下载的文件不存在");
}catch (Exception e){
System.out.println("下载失败");
}finally {
if (httpURLConnection!=null){
httpURLConnection.disconnect();//关闭连接
}
}
}
}
Constant.java:
package com.downloader.constant;
/*常量类*/
public class Constant {
public static final String PATH="D:\\Java_dm\\TestDownloaderPath\\";
}
3.运行程序
在控制台中输入要下载文件的地址,例如:https://downv6.qq.com/qqweb/QQ_1/android_apk/tim_3.5.1.3168_537150355_64.apk
下载一个APK的文件。等待程序运行结束,需要几分钟,耐心等待,之后会在D:\Java_dm\TestDownloaderPath的文件夹中找到tim_3.5.1.3168_537150355_64.apk文件。
留言
文章中已经提供全部代码,如果需要源码的可以在下方留言或者私信。