Skip to content

Commit 5c6b33f

Browse files
author
NoteFox
committed
tested FTP Service with Client class,
it does work
1 parent 60a84d2 commit 5c6b33f

6 files changed

Lines changed: 330 additions & 32 deletions

File tree

pom.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
<groupId>org.springframework.boot</groupId>
2424
<artifactId>spring-boot-starter-data-rest</artifactId>
2525
</dependency>
26+
2627
<dependency>
2728
<groupId>org.springframework.boot</groupId>
2829
<artifactId>spring-boot-starter-web</artifactId>
2930
</dependency>
31+
3032
<dependency>
3133
<groupId>org.springframework.boot</groupId>
3234
<artifactId>spring-boot-starter-web-services</artifactId>
@@ -38,7 +40,6 @@
3840
<version>1.1.1</version>
3941
</dependency>
4042

41-
4243
<dependency>
4344
<groupId>org.springframework.boot</groupId>
4445
<artifactId>spring-boot-starter-test</artifactId>
@@ -50,11 +51,18 @@
5051
</exclusion>
5152
</exclusions>
5253
</dependency>
54+
5355
<dependency>
5456
<groupId>DemoProject</groupId>
5557
<artifactId>demo</artifactId>
5658
<version>0.0.1-SNAPSHOT</version>
5759
</dependency>
60+
61+
<dependency>
62+
<groupId>commons-net</groupId>
63+
<artifactId>commons-net</artifactId>
64+
<version>3.6</version>
65+
</dependency>
5866
</dependencies>
5967

6068
<build>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package FTPClient;
2+
3+
import org.apache.commons.net.ftp.FTPFile;
4+
5+
import java.io.IOException;
6+
7+
public interface FTPInterface {
8+
9+
/**
10+
* if called, connects to a server
11+
*
12+
* @param server server ip/hostname
13+
* @param port FTPServer port
14+
* @param user user to log in with
15+
* @param pass passwort used to log in
16+
* @return
17+
* 0 = successful connection,
18+
* 1 = FTP server refused connection,
19+
* 2 = Could not login to FTP Server (probably wrong password),
20+
* 3 = Socket exception thrown, Server not found,
21+
* 4 = IO Exception
22+
*/
23+
int connect(String server, int port, String user, String pass);
24+
25+
/**
26+
* checking if connected
27+
* @return
28+
*/
29+
boolean isConnected();
30+
31+
/**
32+
* gives back the File list, given in current working dir
33+
* @return FTPFiles in current dir
34+
* @throws IOException couldn't read from current dir
35+
*/
36+
FTPFile[] getFileList() throws IOException;
37+
38+
/**
39+
* call to download a File from current dir
40+
* @param remoteFileName file to download from Server
41+
* @param localFile file to write to
42+
* @throws IOException couldn't download from current dir
43+
*/
44+
void downloadFile(String remoteFileName, String localFile) throws IOException;
45+
46+
/**
47+
* closes connection
48+
* pls always use at the end !!!!
49+
*/
50+
void closeConnection();
51+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package FTPClient;
2+
3+
import org.apache.commons.net.ftp.FTPClient;
4+
import org.apache.commons.net.ftp.FTPFile;
5+
import org.apache.commons.net.ftp.FTPReply;
6+
7+
import java.io.*;
8+
import java.net.SocketException;
9+
import java.text.SimpleDateFormat;
10+
import java.util.Date;
11+
12+
public class FtpClientImpl extends Thread implements FTPInterface{
13+
14+
public FtpClientImpl(OutputStream os) {
15+
logStream = new PrintStream(os);
16+
}
17+
18+
public FtpClientImpl() {
19+
LOGGING_OVER_FILE = false;
20+
}
21+
22+
private FTPClient client;
23+
24+
private PrintStream logStream;
25+
private String LOG_TAG = "FTP Client";
26+
private boolean LOGGING_OVER_FILE = true;
27+
28+
@Override
29+
public int connect(String server, int port, String user, String pass) {
30+
if( client == null ) {
31+
log("Getting passive FTP client");
32+
client = new FTPClient();
33+
34+
try {
35+
36+
client.connect(server, port);
37+
// After connection attempt, you should check the reply code to verify
38+
// success.
39+
int reply = client.getReplyCode();
40+
41+
if(!FTPReply.isPositiveCompletion(reply)) {
42+
client.disconnect();
43+
log(" ERROR : FTP server refused connection.");
44+
return 1;
45+
}
46+
47+
//after connecting to the server set the local passive mode
48+
client.enterLocalPassiveMode();
49+
50+
//send username and password to login to the server
51+
if( !client.login(user, pass) ) {
52+
log(" ERROR - Could not login to FTP Server");
53+
return 2;
54+
}
55+
} catch (SocketException e) {
56+
String message = "Socket exception thrown, Server not found";
57+
log("ERROR :" + message+"\n" + e);
58+
return 3;
59+
} catch (IOException e) {
60+
String message = "IO Exception";
61+
log("ERROR :" + message+"\n" + e);
62+
return 4;
63+
}
64+
}
65+
return 0;
66+
}
67+
68+
@Override
69+
public boolean isConnected() {
70+
return client.isConnected();
71+
}
72+
73+
@Override
74+
public FTPFile[] getFileList() throws IOException {
75+
if(client == null) {
76+
System.out.println("INFO : First initialize the FTPClient by calling 'initFTPPassiveClient()'");
77+
return null;
78+
}
79+
80+
log("DEBUG : Getting file listing for current director");
81+
FTPFile[] files = client.listFiles("");
82+
return files;
83+
}
84+
85+
@Override
86+
public void downloadFile(String remoteFileName, String downloadPath) throws IOException {
87+
// Download File using retrieveFile(String, OutputStream)
88+
OutputStream fileOutputStream = new BufferedOutputStream(new FileOutputStream(new File(downloadPath)));
89+
boolean success = client.retrieveFile(remoteFileName, fileOutputStream);
90+
fileOutputStream.close();
91+
92+
if (success)
93+
log("INFO : File " + remoteFileName + " has been downloaded successfully.");
94+
else
95+
log("ERROR : couldn't download " + remoteFileName + "from server!");
96+
97+
}
98+
99+
@Override
100+
public void closeConnection() {
101+
if( client == null ) {
102+
log("Nothing to close, the FTPClient wasn't initialized");
103+
return;
104+
}
105+
106+
//be polite and logout & close the connection before the application finishes
107+
try {
108+
client.logout();
109+
client.disconnect();
110+
} catch (IOException e) {
111+
String message = "Could not logout";
112+
log(message+"\n");
113+
}
114+
}
115+
116+
private void log(String msg) {
117+
118+
if (!LOGGING_OVER_FILE) {
119+
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
120+
Date date = new Date(System.currentTimeMillis());
121+
System.out.println(LOG_TAG + " : " + formatter.format(date) + " | " + msg);
122+
return;
123+
}
124+
125+
if (logStream == null) {
126+
throw new NullPointerException("ERROR: PrintStream for webServiceLog not initialized");
127+
}
128+
129+
130+
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
131+
Date date = new Date(System.currentTimeMillis());
132+
logStream.println(LOG_TAG + " : " + formatter.format(date) + " | " + msg);
133+
}
134+
135+
136+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package FTPClient;
2+
3+
import org.apache.commons.net.ftp.FTPFile;
4+
5+
import java.io.IOException;
6+
7+
public class TestMain {
8+
static FtpClientImpl client = new FtpClientImpl();
9+
10+
public static void main(String[] args) {
11+
client.connect("localhost", 5000, "ohdmOffViewer", "H!3r0glyph Sat3llite Era$er");
12+
FTPFile[] files = new FTPFile[0];
13+
try {
14+
files = client.getFileList();
15+
} catch (IOException e) {
16+
e.printStackTrace();
17+
}
18+
19+
System.out.println("File list : \n");
20+
for (FTPFile f :
21+
files) {
22+
System.out.println(f.getName());
23+
}
24+
25+
String downloadableFile = files[0].getName();
26+
try {
27+
client.downloadFile(downloadableFile, downloadableFile);
28+
} catch (IOException e) {
29+
e.printStackTrace();
30+
}
31+
32+
client.closeConnection();
33+
}
34+
}

0 commit comments

Comments
 (0)