summaryrefslogtreecommitdiffstats
path: root/Dozentenmodul/src/main/java/ftp/DownloadTask.java
blob: 83ad2c15aef82f8353c9b0ee46a4463bb93986ec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package ftp;

import gui.image.FTPDownloader_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;
	
	@SuppressWarnings("unused")
	private FTPDownloader_GUI gui;
	
	public DownloadTask(String host, int port, String username,
			String password, String downloadPath, String saveDir,
			FTPDownloader_GUI gui) {
		this.host = host;
		this.port = port;
		this.username = username;
		this.password = password;
		this.downloadPath = downloadPath;
		this.saveDir = saveDir;
		this.gui = gui;
		
	}

	/**
	 * 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 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()) {
			JOptionPane.showMessageDialog(null,
					"File has been downloaded successfully!", "Message",
					JOptionPane.INFORMATION_MESSAGE);
		}
	}	
}