Bugfix
This commit is contained in:
parent
fe03df8db2
commit
e0b9e63ccc
1 changed files with 23 additions and 57 deletions
|
@ -5,74 +5,40 @@
|
||||||
*/
|
*/
|
||||||
package ModpackDownloader;
|
package ModpackDownloader;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
import java.io.BufferedOutputStream;
|
import java.io.BufferedOutputStream;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.HttpURLConnection;
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.net.URLConnection;
|
||||||
|
|
||||||
import javax.swing.JFrame;
|
/**
|
||||||
import javax.swing.JProgressBar;
|
*
|
||||||
import javax.swing.SwingUtilities;
|
* @author simonr
|
||||||
import javax.swing.WindowConstants;
|
*/
|
||||||
|
|
||||||
public class ModpackDownloader1 {
|
public class ModpackDownloader1 {
|
||||||
|
|
||||||
static String URL;
|
static String URL;
|
||||||
static String output;
|
static String output;
|
||||||
private static JProgressBar jProgressBar;
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) throws MalformedURLException, IOException {
|
||||||
|
|
||||||
jProgressBar = new JProgressBar();
|
final URL url = new URL(URL);
|
||||||
jProgressBar.setMaximum(100000);
|
final URLConnection conn = url.openConnection();
|
||||||
JFrame frame = new JFrame();
|
try (InputStream is = new BufferedInputStream(conn.getInputStream())) {
|
||||||
frame.setContentPane(jProgressBar);
|
final OutputStream os;
|
||||||
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
|
os = new BufferedOutputStream(new FileOutputStream(output));
|
||||||
frame.setSize(300, 70);
|
byte[] chunk = new byte[1024];
|
||||||
frame.setVisible(true);
|
int chunkSize;
|
||||||
|
while ((chunkSize = is.read(chunk)) != -1) {
|
||||||
run();
|
os.write(chunk, 0, chunkSize);
|
||||||
frame.dispose();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void run() {
|
|
||||||
try {
|
|
||||||
|
|
||||||
URL url = new URL(URL);
|
|
||||||
HttpURLConnection httpConnection = (HttpURLConnection) (url.openConnection());
|
|
||||||
long completeFileSize = httpConnection.getContentLength();
|
|
||||||
|
|
||||||
java.io.BufferedInputStream in = new java.io.BufferedInputStream(httpConnection.getInputStream());
|
|
||||||
java.io.FileOutputStream fos = new java.io.FileOutputStream(output);
|
|
||||||
java.io.BufferedOutputStream bout = new BufferedOutputStream(
|
|
||||||
fos, 1024);
|
|
||||||
byte[] data = new byte[1024];
|
|
||||||
long downloadedFileSize = 0;
|
|
||||||
int x = 0;
|
|
||||||
while ((x = in.read(data, 0, 1024)) >= 0) {
|
|
||||||
downloadedFileSize += x;
|
|
||||||
|
|
||||||
// calculate progress
|
|
||||||
final int currentProgress = (int) ((((double) downloadedFileSize) / ((double) completeFileSize)) * 100000d);
|
|
||||||
|
|
||||||
// update progress bar
|
|
||||||
SwingUtilities.invokeLater(new Runnable() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
jProgressBar.setValue(currentProgress);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
bout.write(data, 0, x);
|
|
||||||
}
|
}
|
||||||
bout.close();
|
os.flush(); // Necessary for Java < 6
|
||||||
in.close();
|
os.close();
|
||||||
} catch (FileNotFoundException e) {
|
|
||||||
} catch (IOException e) {
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue