请老师看一下到底哪里有问题,没有输出结果,也没下载
package com.imooc.downloader;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Downloader {
private Integer threadNum=10;
/**
*下载单个文件保存到本地
* @param source 原图片地址
* @param targetDir 目标目录,下载后的图片保存位置
*/
public void download(String source,String targetDir){
InputStream is = null;
OutputStream os=null;
try{
//https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/pexels-photo-11572548.jpeg
//substring字符串截取 截取最后一个斜杠字符出现开始之后的字符
String fileName=source.substring(source.lastIndexOf("/")+1);
File targetFile=new File(targetDir+"/"+fileName);
//确保文档创建成功
if(!targetFile.exists()){
targetFile.createNewFile();
}
URL url = new URL(source);
URLConnection connection=url.openConnection();
is=connection.getInputStream();
os=new FileOutputStream(targetFile);
byte[] bs = new byte[1024];
int len=0;
while((len=is.read(bs))!=-1){
os.write(bs,0,len);
}
// 来源网址 换行,缩进 目标保存路径 向下取整 文件长度除以1024
System.out.println("[INFO]图片下载完成"+source+"\n\t ->"+targetFile.getPath()+"("+Math.floor(targetFile.length()/1024)+"kb)");
}catch(IOException e){
e.printStackTrace();
}finally{
try {
if (os != null) {
os.close();
}
if (is != null) {
is.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
/**
*从指定文件读取下载地址,批量下载网络资源
* @param targetDir 下载文件的储存目录
* @param downloadTxt downloade.txt完整路径
*/
public void multiDownloadFromFile(String targetDir,String downloadTxt){
//目录实例
File dir = new File(targetDir);
if(!dir.exists()){
dir.mkdirs();//目录创建方法,有s的是多级目录
System.out.println("[INFO]发现下载目录["+dir.getPath()+"]不存在,已自动创建");
//读取download.txt存入resoures集合
List<String> resources = new ArrayList<>();
BufferedReader reader=null;
ExecutorService threadPool=null;
try{
reader = new BufferedReader(new FileReader(downloadTxt));
String line=null;
while((line=reader.readLine())!=null){
resources.add(line);
//System.out.println(line);
}
threadPool= Executors.newFixedThreadPool(this.threadNum);
//直接在for循环中使用this.实现类调用的是匿名实现类runnable,因此需要用that转换
Downloader that=this;
for(String res:resources){
threadPool.execute(new Runnable() {
@Override
public void run() {
that.download(res,targetDir);
}
});
}
}catch(IOException e){
e.printStackTrace();
}finally {
if(threadPool!=null){
threadPool.shutdown();
}
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
//开始多线程下载的方法
public void start(String propDir){
//实例文件指定路径
File propFile=new File(propDir+"\\config.properties");
//实例properties类
Properties properties = new Properties();
//加载配置文件
Reader reader=null;
try {
reader=new FileReader(propFile);
//加载文件里的数据
properties.load(reader);
//读取数据,getproperty根据属性名获取对应的值
String threadNum=properties.getProperty("thread-num");
this.threadNum = Integer.parseInt(threadNum);
String targetDir = properties.getProperty("target-dir");
// System.out.println(threadNum);
// System.out.println(targetDir);
this.multiDownloadFromFile(targetDir,propDir+"\\download.txt");
} catch (IOException e) {
e.printStackTrace();
}finally {
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Downloader downloader=new Downloader();
downloader.start("D:\\JAVA\\imooc\\慕课Java练习\\downloader\\src");
//downloader.download("https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/pexels-photo-11572548.jpeg","d:/JavaIO");
}
}config-properties:
==
download.txt:
https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/pexels-photo-11572548.jpeg https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/pexels-photo-11593467.jpeg https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/pexels-photo-11631922.jpeg https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/pexels-photo-12203460.jpeg https://manongbiji.oss-cn-beijing.aliyuncs.com/imooc/pexels/pexels-photo-12240136.jpeg
6
收起
正在回答
1回答
同学你好,第一次执行同学代码是没有问题的,但当路径已经存在就无法进行下载。那是因if (!dir.exists()) 判断的闭合错误,建议同学移动花括号的位置。如下所示:然后重新测试代码试一下。

祝学习愉快!
2023版Java工程师
- 参与学习 人
- 提交作业 8790 份
- 解答问题 9886 个
综合就业常年第一,编程排行常年霸榜,北上广深月薪过万! 不需要基础,无需脱产即可学习,只要你有梦想,想高薪! 全新升级:技术栈升级(包含VUE3.0,ES6,Git)+项目升级(前后端联调与功能升级)
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星