diff options
| author | Jonathan Bauer | 2014-09-11 15:00:28 +0200 |
|---|---|---|
| committer | Jonathan Bauer | 2014-09-11 15:00:28 +0200 |
| commit | afd4abcd1902971384f14515fe97646947b66f94 (patch) | |
| tree | 4ad51b4af5a7a8384d9a1ce8dcc8fae0f177f563 /dozentenmodul/Dozentenmodul/src/main/java/ftp/DownloadTask.java | |
| parent | OS dependent path for config file (diff) | |
| download | tutor-module-afd4abcd1902971384f14515fe97646947b66f94.tar.gz tutor-module-afd4abcd1902971384f14515fe97646947b66f94.tar.xz tutor-module-afd4abcd1902971384f14515fe97646947b66f94.zip | |
lower case names for folder please
Diffstat (limited to 'dozentenmodul/Dozentenmodul/src/main/java/ftp/DownloadTask.java')
| -rw-r--r-- | dozentenmodul/Dozentenmodul/src/main/java/ftp/DownloadTask.java | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/dozentenmodul/Dozentenmodul/src/main/java/ftp/DownloadTask.java b/dozentenmodul/Dozentenmodul/src/main/java/ftp/DownloadTask.java new file mode 100644 index 00000000..5592adba --- /dev/null +++ b/dozentenmodul/Dozentenmodul/src/main/java/ftp/DownloadTask.java @@ -0,0 +1,117 @@ +package ftp; + +import gui.image.FTPEditDownloader_GUI; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; + +import javax.swing.JOptionPane; +import javax.swing.SwingWorker; + +/** + * Execute file download in a background thread and update the progress. + * + * @author www.codejava.net + * + */ +public class DownloadTask extends SwingWorker<Void, Void> { + + private static final int BUFFER_SIZE = 4096; + + private String host; + private int port; + private String username; + private String password; + private String downloadPath; + private String saveDir; + private int percentCompleted; + + public DownloadTask(String host, int port, String username, + String password, String downloadPath, String saveDir) { + this.host = host; + this.port = port; + this.username = username; + this.password = password; + this.downloadPath = downloadPath; + this.saveDir = saveDir; + + } + + /** + * Executed in background thread + */ + @Override + protected Void doInBackground() throws Exception { + FTPUtility util = new FTPUtility(host, port, username, password); + try { + util.connect(); + + byte[] buffer = new byte[BUFFER_SIZE]; + int bytesRead = -1; + long totalBytesRead = 0; + percentCompleted = 0; + long start = System.nanoTime(); + final double NANOS_PER_SECOND = 1000000000.0; + final double BYTES_PER_MIB = 1024 * 1024; + long fileSize = util.getFileSize(downloadPath); + // gui.setFileSize(fileSize); + + String fileName = new File(downloadPath).getName(); + + File downloadFile = new File(saveDir + File.separator + fileName); + FileOutputStream outputStream = new FileOutputStream(downloadFile); + + util.downloadFile(downloadPath); + InputStream inputStream = util.getInputStream(); + + while ((bytesRead = inputStream.read(buffer)) != -1 + && isCancelled() == false) { + outputStream.write(buffer, 0, bytesRead); + totalBytesRead += bytesRead; + // System.out.println(totalBytesRead); + double speed = NANOS_PER_SECOND / BYTES_PER_MIB + * totalBytesRead / (System.nanoTime() - start + 1); + percentCompleted = (int) (totalBytesRead * 100 / fileSize); + setProgress(percentCompleted); + firePropertyChange("speed", 0, speed); + firePropertyChange("filesize", 0, fileSize); + firePropertyChange("bytesread", 0, totalBytesRead); + + } + + outputStream.close(); + + util.finish(); + } catch (FTPException ex) { + JOptionPane.showMessageDialog(null, + "Error downloading file: " + ex.getMessage(), "Error", + JOptionPane.ERROR_MESSAGE); + ex.printStackTrace(); + setProgress(0); + cancel(true); + } finally { + util.disconnect(); + } + + return null; + } + + /** + * Executed in Swing's event dispatching thread + */ + @Override + protected void done() { + if (!isCancelled() && percentCompleted==100) { + System.out.println("Datei erfolgreich heruntergeladen"); + JOptionPane.showMessageDialog(null, + "Datei erfolgreich heruntergeladen.", "Message", + JOptionPane.INFORMATION_MESSAGE); + } else if(!isCancelled() && percentCompleted != 100){ + System.out.println("Datein wurde unvollständig heruntergeladen"); + JOptionPane.showMessageDialog(null, + "Datei wurde unvollständig heruntergeladen. Bitte wiederholen.", "Message", + JOptionPane.INFORMATION_MESSAGE); + } + } +}
\ No newline at end of file |
