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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
package ftp;
import java.io.File;
import java.io.FileInputStream;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import org.apache.log4j.Logger;
import models.Image;
/**
* Executes the file upload in a background thread and updates progress to
* listeners that implement the java.beans.PropertyChangeListener interface.
*
* @author www.codejava.net
*
*/
public class UploadTask extends SwingWorker<Void, Void> {
/**
* Logger instance for this class.
*/
private final static Logger LOGGER = Logger.getLogger(UploadTask.class);
// 8MB buffer
private static final int BUFFER_SIZE = 1024 * 1024;
private static final double UPDATE_INTERVAL_SECONDS = 0.6;
private static final double UPDATE_INTERVAL_MS = UPDATE_INTERVAL_SECONDS * 1000;
private static final double BYTES_PER_MIB = 1024 * 1024;
private String host;
private int port;
private String username;
private String password;
private String destDir;
private File uploadFile;
private int percentCompleted;
public UploadTask(String host, int port, String username, String password,
String destDir, File uploadFile) {
this.host = host;
this.port = port;
this.username = username;
this.password = password;
this.destDir = destDir;
this.uploadFile = uploadFile;
}
/**
* Executed in background thread
*/
@Override
protected Void doInBackground() throws Exception {
FTPUtility util = new FTPUtility(host, port, username, password);
try {
util.connect();
util.uploadFile(uploadFile, destDir);
FileInputStream inputStream = new FileInputStream(uploadFile);
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = -1;
int i = 0;
long totalBytesRead = 0;
percentCompleted = 0;
long fileSize = uploadFile.length();
Image.image.setFilesize(fileSize);
firePropertyChange("filesize", 0, fileSize);
long lastUpdate = 0;
long currentBytes = 0;
long lastBytes = 0;
while ((bytesRead = inputStream.read(buffer)) != -1 && !isCancelled()) {
util.writeFileBytes(buffer, 0, bytesRead);
currentBytes += bytesRead;
totalBytesRead += bytesRead;
long now = System.currentTimeMillis();
if (lastUpdate + UPDATE_INTERVAL_MS < now) {
percentCompleted = (int) ((totalBytesRead * 100) / fileSize);
setProgress(percentCompleted);
lastBytes = (lastBytes * 2 + currentBytes) / 3;
final double speed = lastBytes / UPDATE_INTERVAL_SECONDS;
firePropertyChange("speed", 0, speed / BYTES_PER_MIB);
firePropertyChange("bytesread", 0, totalBytesRead);
lastUpdate = now;
currentBytes = 0;
}
}
percentCompleted = (int) ((totalBytesRead * 100) / fileSize);
setProgress(percentCompleted);
inputStream.close();
util.finish();
} catch (FTPException ex) {
JOptionPane.showMessageDialog(null,
"Error uploading 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) {
LOGGER.info("Datei erfolgreich hochgeladen.");
JOptionPane.showMessageDialog(null, "Datei erfolgreich hochgeladen.",
"Message", JOptionPane.INFORMATION_MESSAGE);
} else if (!isCancelled() && percentCompleted != 100) {
LOGGER.error("Datei wurde unvollständig hochgeladen.");
JOptionPane.showMessageDialog(null,
"Datei wurde unvollständig hochgeladen. Bitte wiederholen.",
"Message", JOptionPane.INFORMATION_MESSAGE);
}
}
}
|