Fixed FTPUpload behaviour by using Apache common-net. [artf441879]

This commit is contained in:
Ziver Koc 2014-11-10 13:37:13 +01:00
parent f00bd440a7
commit 62937e7e81
13 changed files with 278 additions and 11 deletions

View file

@ -5,6 +5,9 @@ import com.ericsson.uecontrol.core.util.ThroughputCalculator;
import com.ericsson.uecontrol.core.util.UrlUtil;
import com.ericsson.uecontrol.gui.util.Configurator.Configurable;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.log4j.Logger;
import java.io.BufferedOutputStream;
@ -35,12 +38,57 @@ public class UeBehaviourFtpUpload extends UeBehaviour {
@Override
protected void execute() throws IOException {
FTPClient ftp = new FTPClient();
try {
// Connect
ftp.setConnectTimeout(3000);
ftp.setDataTimeout(200);
if (host.contains(":")) {
String tmpHost = host.substring(0, host.indexOf(':'));
int tmpPort = Integer.parseInt(host.substring(host.indexOf(':')));
ftp.connect(tmpHost, tmpPort);
} else ftp.connect(host);
ftp.login(username, password);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
// Upload data
OutputStream outStream = ftp.storeFileStream(filePath);
if (outStream != null) {
OutputStream out = new BufferedOutputStream(outStream);
int total = 0;
byte[] data = new byte[BUFFER_SIZE];
while (total < size && !stopExecution()) {
int writeLength = (total + data.length < size ? data.length : size - total);
out.write(data, 0, writeLength);
total += writeLength;
super.setProgress((float) total / size);
}
out.close();
if(!ftp.completePendingCommand()) {
throw new IOException("FTP reply: "+ftp.getReplyString());
}
} else {
throw new IOException("FTP reply: "+ftp.getReplyString());
}
// Disconnect
ftp.logout();
} finally {
if(ftp.isConnected()) {
ftp.disconnect();
}
}
}
/* @Override
protected void execute() throws IOException {
URL url = new URL(getFileUrl());
log.debug("Uploading file: " + url);
byte[] data = new byte[BUFFER_SIZE];
URLConnection connection = UrlUtil.getURLConnection(url);
URLConnection connection = url.openConnection();
OutputStream out = new BufferedOutputStream(connection.getOutputStream());
int total = 0;
@ -52,8 +100,8 @@ public class UeBehaviourFtpUpload extends UeBehaviour {
super.setProgress((float)total/size);
}
out.close();
}
}
*/
protected String getFileUrl() {
String url = "ftp://";
if(username != null && !username.isEmpty()) {