做了一個很簡單的音樂下載項目,從網絡上相關網頁抓取歌曲和歌詞的
超鏈接地址,然後通過這個超鏈接將歌曲和歌詞下載到本地。這樣一來就
可以進行批量下載了。批量下載還會遇到一個問題,抓取的網站可能會將
你的IP地址封掉,這就需要動態切換IP,動態切換IP,可以使用撥號上網
的機器,沒隔一段時間重新撥號,通常情況下就會換一個IP進行下載了。
很簡單的實現,主要包括兩個關鍵的地方,一是獲取歌曲和歌詞的超
鏈接。這個代碼其實比較簡單的:
URL url = new URL(urlAddr); // 轉化成URL格式
URLConnection URLconnection = url.openConnection(); // 創建一個連接
HttpURLConnection httpConnection = (HttpURLConnection) URLconnection;
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {// 連接正常
System.out.println("Connection ok.");
InputStream urlStream = httpConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlStream));
String sCurrentLine = ""; // 按行讀取網頁
String sTotalString = ""; // 存儲整個網頁
while ((sCurrentLine = bufferedReader.readLine()) != null)
sTotalString = sTotalString + sCurrentLine + "\r\n";
}
}
從獲取到的字符流中提取歌曲和歌詞的超鏈接,將這些超鏈接信息通過一個鏈表
或者其它什麼數據結構保存起來,做爲參數傳遞給下載相關的代碼:
public void saveToFile(String destUrl,String filename) throws Exception{
File file = new File(filename);
if(file.exists()){
logger.info(file.getName()+"文件已經存在,無需下載!");
return;
}
int slashPos = filename.lastIndexOf("/");
String dir = null;
if(slashPos >= 0){
dir = filename.substring(0,slashPos);
File f = new File(dir);
if(f.isDirectory()){
;
}else{
if(!f.mkdirs()){
throw new IOException("Make Dir Fail:" + dir);
}
}
}
FileOutputStream fos = null;
BufferedInputStream bis = null;
HttpURLConnection httpUrl = null;
URL url = null;
byte[] buf = new byte[BUFFER_SIZE];
int size = 0;
boolean isNeedRetry = true;
while(isNeedRetry){
try{
isNeedRetry = false;
url = new URL(destUrl);
httpUrl = (HttpURLConnection)url.openConnection();
httpUrl.connect();
bis = new BufferedInputStream(httpUrl.getInputStream());
fos = new FileOutputStream(filename);
while((size = bis.read(buf)) != -1)
fos.write(buf,0,size);
logger.info(file.getName()+"下載完成!");
}catch(Exception e){
if(destUrl.contains(".lrc")){
}else{
if(!isNeedRetry)
logger.info("Current ip is:"+IPUtils.getInstance().getIPInfo());
isNeedRetry = true;
Thread.sleep(30000);
}
}finally{
fos.close();
bis.close();
httpUrl.disconnect();
}
}
此實現只是簡單的判斷一下文件是否已經下載過了,並沒有添加斷點續傳的功能。
如果以前的文件沒有下載完整,下次重新下載依舊不是完整的,這也是沒有提供斷
點續傳會導致的問題,但是由於使用的是單線程下載,所以每次會影響的文件只有
一個,如果使用了多線程的話,那麼就必須考慮增加斷點續傳的功能了。