summaryrefslogtreecommitdiffstats
path: root/Dozentenmodul/src/ftp/DownloadTask.java
diff options
context:
space:
mode:
Diffstat (limited to 'Dozentenmodul/src/ftp/DownloadTask.java')
-rw-r--r--Dozentenmodul/src/ftp/DownloadTask.java60
1 files changed, 30 insertions, 30 deletions
diff --git a/Dozentenmodul/src/ftp/DownloadTask.java b/Dozentenmodul/src/ftp/DownloadTask.java
index d29b7802..74b55e37 100644
--- a/Dozentenmodul/src/ftp/DownloadTask.java
+++ b/Dozentenmodul/src/ftp/DownloadTask.java
@@ -10,24 +10,22 @@ import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
/**
- * Execute file download in a background thread and update the progress.
+ * 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;
-
-
public DownloadTask(String host, int port, String username,
String password, String downloadPath, String saveDir) {
this.host = host;
@@ -36,63 +34,65 @@ public class DownloadTask extends SwingWorker<Void, Void> {
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;
int percentCompleted = 0;
- long start=System.nanoTime();
- final double NANOS_PER_SECOND = 1000000000.0;
- final double BYTES_PER_MIB = 1024*1024;
+ 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);
-
+ // 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) {
+
+ 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);
+ // 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);
-
+ 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);
+ JOptionPane.showMessageDialog(null,
+ "Error downloading file: " + ex.getMessage(), "Error",
+ JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
setProgress(0);
- cancel(true);
+ cancel(true);
} finally {
util.disconnect();
}
-
+
return null;
}
@@ -106,5 +106,5 @@ public class DownloadTask extends SwingWorker<Void, Void> {
"File has been downloaded successfully!", "Message",
JOptionPane.INFORMATION_MESSAGE);
}
- }
+ }
} \ No newline at end of file