If you want to download mp3/song file from website its take time and it will popup adds, to avoid that you can use below program to download songs by one shot.
Create Songs Folder Location to store in Local drive "c:/tmp/songs/";
Place urls properties file in C:/tmp/songs/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
/**
* The standard class for downloading songs from given web url
*
*/
public class DownloadSongsFromWeb {
/*
* Main Method
*/
public static void main(String[] args) {
String tgtLocation = "c:/tmp/songs/";
String songsUrls = "C:/tmp/songs/urls.properties";
try {
File file = new File(songsUrls);
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println("Processing : " + line);
if (line.contains("&cpage")) {
processSPage(line, tgtLocation);
} else {
getFileInfo(line, tgtLocation);
}
// System.out.println(line + ":Download Completed");
}
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* It process the entire web url and find mp3 file or child url to get mp3 file
*/
private static void processSPage(String line, String tgtLocation) {
try {
URL url = new URL(line);
URLConnection uc = url.openConnection();
uc.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
uc.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
String inputLine="";
String songsURL[];
String songsSplitLine = "";
String newURL = "";
while ((inputLine = in.readLine()) != null) {
songsURL = inputLine.split("page=ILaiyaraja Hits&");
for (int i = 0; i < songsURL.length; i++) {
songsSplitLine = songsURL[i];
if (!songsSplitLine.isEmpty() && songsSplitLine.contains("spage=")) {
newURL = songsSplitLine.substring(0, songsSplitLine.indexOf("\""));
newURL = line.substring(0, line.indexOf("cpage")) + newURL;
System.out.println(newURL);
getFileInfo(newURL, tgtLocation);
}
}
}
in.close();
} catch (MalformedURLException me) {
System.out.println(me);
} catch (IOException ioe) {
System.out.println(ioe);
}
}
/*
* This method get the list of mp3 urls from child page and pass url to @downloadMP3 method
*/
private static void getFileInfo(String urlPage, String tgtLocation) {
try {
URL url = new URL(urlPage);
URLConnection uc = url.openConnection();
uc.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
uc.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
String inputLine, songUrl = "";
int count = 0;
String songsURL[];
String songsSplitLine = "";
while ((inputLine = in.readLine()) != null) {
songsURL = inputLine.split("href=");
for (int i = 0; i < songsURL.length; i++) {
songsSplitLine = songsURL[i];
if (songsSplitLine.toUpperCase().contains(".MP3") && songsSplitLine.contains("http://")) {
songUrl = songsSplitLine.substring(songsSplitLine.indexOf("http://"),
songsSplitLine.indexOf(".mp3") + 4);
downloadMP3(songUrl, tgtLocation);
}
}
}
in.close();
} catch (MalformedURLException me) {
System.out.println(me);
} catch (IOException ioe) {
System.out.println(ioe);
}
}
/*
* This method download mp3 file from give url
*/
private static void downloadMP3(String songUrl, String tgtLocation) {
try {
String[] folderNames = songUrl.split("/");
String folderName = folderNames[folderNames.length - 2];
folderName = URLDecoder.decode(folderName, "UTF-8");
String fileName = folderNames[folderNames.length - 1].replace("TamilWire.com", "");
fileName = URLDecoder.decode(fileName, "UTF-8").replace(" ", "").replace("-.", ".");
createFolder(tgtLocation + folderName);
String tgtFile = tgtLocation + folderName + "/" + fileName;
if (!new File(tgtFile).exists()) {
songUrl = songUrl.replace(" ", "%20");
URLConnection conn = new URL(songUrl).openConnection();
conn.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
conn.connect();
InputStream is = conn.getInputStream();
OutputStream outstream = new FileOutputStream(new File(tgtFile));
byte[] buffer = new byte[4096];
int len;
while ((len = is.read(buffer)) > 0) {
outstream.write(buffer, 0, len);
}
outstream.close();
System.out.println(tgtFile + " Downloaded");
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
/*
* It songs folder will be created based on url songs categorization
*/
private static void createFolder(String folderPath) {
File file = new File(folderPath);
if (!file.exists()) {
if (file.mkdir()) {
System.out.println("Directory is created!");
}
}
}
urls.properties
You may provide multiple songs urls in properties file.
http://tamiltunes.live/vijayakanth-hits-59-tamil-songs.html
I have tested with jdk1.8 in windows 10.
That's it!!
Copyright - There is no copyright on the code. You can copy, change and distribute it freely. Just mentioning this site should be fair
(C) November 2017, manivelcode