📄 down.java
字号:
/**
* Download the file specified by an URL.
*/
//package fetchInformation;
import java.io.*;
import java.net.*;
/**
* @author caojinghua
*
*/
public class Down {
public static void LoadFiles(String spec, File save)
{//获取目标页面
try{
URL url=new URL(spec);
BufferedReader in=new BufferedReader(new InputStreamReader(url.openStream()));
//output to a file
BufferedWriter out=new BufferedWriter(new FileWriter(save));
String line=null;
while((line=in.readLine())!=null)
{
out.write(line);
}
if(in!=null)
in.close();
if(out!=null)
out.close();
}catch(MalformedURLException e)
{
System.out.println(e);
}catch(IOException ioe)
{
System.out.println(ioe);
}
}
/**
* @param args
*/
public static void main(String[] args)throws IOException {
int pagenum=26700;
int flag=0;
File file = new File( "config.ini" );
if( !(file.exists()))
{ //假如没有配置文件,就创建一个
DataOutputStream newFw = new DataOutputStream( new FileOutputStream( "config.ini" ));
newFw.writeInt(26815);
newFw.close();
}
//读入上次下载之后的位置,以便继续下载新的mp3
DataInputStream fr = new DataInputStream( new FileInputStream("config.ini"));
pagenum = fr.readInt();
fr.close();
System.out.println( "Download from :" +pagenum );
//file=new File(""+pagenum+".htm");
//while ((file.exists()))
//{
// pagenum++;
// file=new File(""+pagenum+".htm");
// }
pagenum++; //此时pagenum的值应该是最新的页面号码
//pagenum=26686;
while(1==1)
{
String savepath= "" + pagenum +".htm";
//URL补全
String url="http://www.51voa.com/VOA_Standard_English/VOA_Standard_English_" + pagenum + ".html";
try{
File savefile=new File(savepath);
LoadFiles(url, savefile);
}catch(NullPointerException e){
System.out.println("End");
System.exit(0);
}
try{
File file1 = new File( savepath );
BufferedReader reader = new BufferedReader(new FileReader(file1));
String str;
str=reader.readLine();
int index1=0,index2=0;
index1 = str.indexOf("<EMBED src=\"") + 12;
index2 = str.indexOf("\" width=", index1);
if( index1==-1 || index2==-1 )
throw new IOException("Not a mp3 URL");
String newStr;
newStr="http://www.51voa.com" + str.substring(index1, index2);
//System.out.println(newStr);
System.out.println( "This is the URL:" + newStr );
//这里设定flashget的路径
String exePath="D:\\Program Files\\FlashGet Network\\Flashget\\flashget.exe";
String mp3Path="E:\\Downloads";
try{
Runtime.getRuntime().exec(exePath+" "+newStr);
}catch(IOException e){
//System.out.println(e);
flag++;
}
}// end try
catch(IOException ee)
{
System.out.println("End while exec"+pagenum);
System.out.println(ee);
flag++;
//System.exit(0);
}// end catch
pagenum++;
if(flag>10)
{ //这里是设定的用于修复页面号码不连续问题的,假如有10个连续页面不存在,说明后面确实是没有更新的内容了
//将页面号码写回配置文件,以便下次继续下载新的内容而不是重复下载已经下载过的内容
BufferedWriter fw = new BufferedWriter( new FileWriter("config.ini"));
fw.write( "" + (pagenum - 11) );
fw.close();
System.out.println( "The last download is " + (pagenum -6) );
System.exit(0);
}
}// end while
//System.out.println("");
//BufferedWriter fw = new BufferedWriter( new FileWriter("config.ini"));
//FileWriter fw = new FileWriter( "config.ini" );
//fw.write( "" + (pagenum - 5) );
//fw.close();
}// end method main
}// end class Down
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -