Redone download file
This commit is contained in:
parent
2c12f951cd
commit
15a8580db4
@ -12,11 +12,6 @@
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry exported="true" kind="lib" path="lib/JNBT_1.3.jar"/>
|
||||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||
<attributes>
|
||||
<attribute name="test" value="true"/>
|
||||
|
21
pom.xml
21
pom.xml
@ -1,5 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
<project
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
@ -7,11 +8,29 @@
|
||||
<groupId>groupId</groupId>
|
||||
<artifactId>MinecraftLandGenerator</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-logging</groupId>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
|
@ -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.<br>
|
||||
* (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 <i>"System.currentTimeMillis();"</i>) <br>
|
||||
* <br>
|
||||
* Thanks to bs123 at <br>
|
||||
* <a href="http://www.daniweb.com/software-development/java/threads/84370"> http://www.daniweb.com/software-development/java/threads/84370</a>
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user