📄 test.java
字号:
import java.net.*;
import java.io.*;
public class Test //这里定义一个类,类名一定要和我们的文件名一样
{
Test(String s)
{
try
{
for(int i=5;i<9;i++)
{ String temp="";
if(i<10)
temp=s+"0"+i+".jpg";
else
temp=s+i+".jpg";
URL u=new URL(temp);
new down(u,i);
}
}catch(Exception e){}
}
public static void main(String args[]) //主函数是java程序的入口
{ // String s="http://comic.narutos.net/op/375/jojo_0";
new A();
}
}
class down extends Thread
{
URL u;
InputStream is;
FileOutputStream out;
String temp;
byte b[]=new byte[1024];
int j;
down(URL u,int j)
{
try
{
this.u=u;
this.j=j;
temp=u.getFile();//得到这个URL所指向的文件的完整路径
int i=temp.lastIndexOf("/");//得到这个路径中最后一个路径分割符"\"的位置
temp=temp.substring((i+1),temp.length());//将这个位置后面的子串取出,因为它就是完整的文件名
is=u.openStream();//打开程序与这个URL资源的输入流
out=new FileOutputStream(temp);//在本地也去创建一个一模一样的文件
this.start();
}catch(Exception e){}
}
public void run()
{
try
{
int i=is.read(b);//开始读入数据
System.out.println("开始下载"+j);
while(i!=-1)//如果数据不为null 就反复的写入到本地文件中,并且再次读入
{
out.write(b,0,i);
out.flush();
i=is.read(b);
}
is.close();
out.close();//关闭所有的流
System.out.println(j+"下载"+temp+"完毕,请验收!");
}catch(Exception e){}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -