From 15a8580db44e4db169e5b55b3774d49860a31658 Mon Sep 17 00:00:00 2001 From: Piegames <14054505+piegamesde@users.noreply.github.com> Date: Mon, 12 Nov 2018 10:27:28 +0100 Subject: [PATCH] Redone download file --- .classpath | 5 - pom.xml | 21 +++- .../MinecraftLandGenerator/DownloadFile.java | 102 ++++-------------- 3 files changed, 39 insertions(+), 89 deletions(-) diff --git a/.classpath b/.classpath index 46f1016..82298c2 100644 --- a/.classpath +++ b/.classpath @@ -12,11 +12,6 @@ - - - - - diff --git a/pom.xml b/pom.xml index d61a9fd..6fbd55d 100644 --- a/pom.xml +++ b/pom.xml @@ -1,5 +1,6 @@ - 4.0.0 @@ -7,11 +8,29 @@ groupId MinecraftLandGenerator 1.0-SNAPSHOT + + + + + maven-compiler-plugin + 3.7.0 + + 1.8 + 1.8 + + + + 1.8 1.8 + + commons-io + commons-io + 2.6 + commons-logging commons-logging diff --git a/src/main/java/morlok8k/MinecraftLandGenerator/DownloadFile.java b/src/main/java/morlok8k/MinecraftLandGenerator/DownloadFile.java index 27e26e5..cb04bea 100644 --- a/src/main/java/morlok8k/MinecraftLandGenerator/DownloadFile.java +++ b/src/main/java/morlok8k/MinecraftLandGenerator/DownloadFile.java @@ -19,17 +19,14 @@ package morlok8k.MinecraftLandGenerator; -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; +import java.io.File; import java.io.IOException; -import java.net.MalformedURLException; import java.net.URL; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.FilenameUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.w3c.bert_bos.UTF8URL.Unescape; /** * @author morlok8k @@ -37,87 +34,26 @@ import org.w3c.bert_bos.UTF8URL.Unescape; public class DownloadFile { private static Log log = LogFactory.getLog(Main.class); + /** - * Downloads a File using a URL in a String.
- * (If the file is a dynamic URL (Not like "http://example.com/file.txt") and it can't get the filename, it saves it as "System.currentTimeMillis();")
- *
- * Thanks to bs123 at
- * http://www.daniweb.com/software-development/java/threads/84370 - * - * @param URL URL in a String - * @param Output Displays output if true - * @return Boolean: true if download was successful, false if download wasn't - * @author Morlok8k + * Downloads a File using a URL in a String. + * + * @param url + * The URL of the file to download + * @param Output + * Deprecated, does nothing + * @return true if download was successful, false if not + * @author piegames */ - public static boolean downloadFile(final String URL, final boolean Output) { - - String fileName = URL.substring(URL.lastIndexOf("/") + 1, URL.length()); - - if (fileName.startsWith("\"")) { - if (fileName.substring(fileName.length() - 1, fileName.length()) == "\"") { - fileName = fileName.substring(1, fileName.length() - 1); - } - } - - final int size = 1024 * 4; // 1024 * n should be tested to get the optimum size (for download speed.) - - if (fileName.equals("")) { - fileName = String.valueOf(System.currentTimeMillis()); - } - - fileName = Unescape.unescape(fileName); - - if (Output) { - log.info("Downloading: " + URL); - log.info("Saving as: " + fileName); - } - - final long differenceTime = System.currentTimeMillis(); - final Long[] timeTracking = new Long[]{differenceTime, differenceTime}; - timeTracking[0] = System.currentTimeMillis(); - - if (Output) { - log.info(var.MLG + "*"); - } - + public static boolean downloadFile(final String url, final boolean Output) { try { - BufferedInputStream in = new BufferedInputStream(new URL(URL).openStream()); - FileOutputStream fos = new FileOutputStream(fileName); - final BufferedOutputStream bout = new BufferedOutputStream(fos, size); - final byte[] data = new byte[size]; - int x = 0; - int count = 0; - while ((x = in.read(data, 0, size)) >= 0) { - bout.write(data, 0, x); - count = count + x; - if (Output) { - log.info("*"); - } - } - bout.close(); - in.close(); - if (Output) { - log.info(var.newLine); - log.info(count + " byte(s) copied"); - } - - timeTracking[1] = System.currentTimeMillis(); - long differenceTime1 = (timeTracking[1] - timeTracking[0]); - if (Output) { - log.info("Elapsed Time: " + String.format("%02d min:%02d sec", (differenceTime1 / 1000) / 60, (differenceTime1 / 1000) % 60)); - } - } catch (final FileNotFoundException e) { - e.printStackTrace(); + URL download = new URL(url); + File dest = new File(FilenameUtils.getName(download.getPath())); + log.info("Downloading " + url + " to " + dest); + FileUtils.copyURLToFile(download, dest); + } catch (IOException e) { + log.warn("Could not download " + url, e); return false; - } catch (final MalformedURLException e) { - e.printStackTrace(); - return false; - } catch (final IOException e) { - e.printStackTrace(); - return false; - } - if (Output) { - log.info("Done"); } return true; }