Java Utililty Methods URL Download

List of utility methods to do URL Download

Description

The list of methods to do URL Download are organized into topic(s).

Method

voiddownloadMobiFromEpubUrl(String href)
download Mobi From Epub Url
URL urlEpub = new URL(href);
String path = urlEpub.getFile();
path = path.substring(0, path.length() - 4).concat("mobi");
URL urlMobi = new URL("http", "bb.bulexpo-bg.com", path);
InputStream in = urlMobi.openStream();
String fileName = path.substring(path.lastIndexOf('/') + 1);
downloadBook(in, fileName);
StringdownloadPageSource(String stringURL)
download Page Source
URL url;
HttpURLConnection conn;
StringBuilder source = new StringBuilder();
try {
    url = new URL(stringURL);
    conn = (HttpURLConnection) url.openConnection();
} catch (IOException ex) {
    throw ex;
...
voiddownloadSource(PrintStream out, PrintStream err, String srcURL, String dirStr, boolean extract)
download Source
String fileName = (srcURL.lastIndexOf('/') > 0) ? srcURL.substring(srcURL.lastIndexOf('/') + 1) : srcURL;
try {
    out.println("Connecting...");
    File dir = new File(dirStr);
    if (!dir.exists()) {
        err.println("Destination directory does not exist.");
    File file = new File(dir, fileName);
...
StringdownloadString(String url)
download String
String s = null;
try {
    BufferedReader reader = new BufferedReader(new InputStreamReader(new URL(url).openStream()));
    String ss;
    while ((ss = reader.readLine()) != null)
        s += ss;
} catch (IOException ex) {
    ex.printStackTrace();
...
StringdownloadString(URL url)
download String
return readString(url.openStream());
StringdownloadString(URL url)
Downloads data from the given URL and returns it as a string.
return readString(url.openStream());
StringdownloadText(String url)
Download text from a string url.
return downloadText(new URL(url));
StringdownloadTextFromUrl(String url)
Downloads text from a given URL and returns it as a String
InputStream in;
try {
    in = new URL(url).openStream();
    Scanner scan = new Scanner(in);
    return scan.hasNext() ? scan.next() : null;
} catch (MalformedURLException e) {
    e.printStackTrace();
    return null;
...
longdownloadTo(String url, String filename)
download To
URL httpUrl = new URL(url);
File file = new File(filename);
file.deleteOnExit();
if (file.exists()) {
    if (file.isDirectory()) {
        throw new IOException("File '" + file + "' exists but is a directory");
    if (file.canWrite() == false) {
...
StringdownloadToDirectory(URL url, String directoryName)
Utility to download a remote file from a URL and place the contents into a directory.
BufferedInputStream inputStream = new BufferedInputStream(url.openStream());
String pathname = url.getPath();
String filename = pathname;
int index = pathname.lastIndexOf("/");
if (index != -1) {
    filename = pathname.substring(index + 1);
int bytes;
...