|
@@ -1,49 +1,71 @@ |
1 |
|
- |
|
2 |
1 |
|
package com.esotericsoftware.scar; |
3 |
2 |
|
|
4 |
|
- |
import static com.esotericsoftware.minlog.Log.*; |
|
3 |
+ |
import java.io.*; |
|
4 |
+ |
import java.net.*; |
5 |
5 |
|
|
6 |
|
- |
import java.io.BufferedInputStream; |
7 |
|
- |
import java.io.File; |
8 |
|
- |
import java.io.FileInputStream; |
9 |
|
- |
import java.io.IOException; |
10 |
|
- |
import java.net.InetAddress; |
|
6 |
+ |
import org.apache.commons.net.ftp.*; |
11 |
7 |
|
|
12 |
|
- |
import org.apache.commons.net.ftp.FTPClient; |
|
8 |
+ |
import com.esotericsoftware.wildcard.*; |
13 |
9 |
|
|
14 |
|
- |
import com.esotericsoftware.wildcard.Paths; |
|
10 |
+ |
import static com.esotericsoftware.minlog.Log.*; |
15 |
11 |
|
|
16 |
|
- |
public class FTP { |
17 |
|
- |
static public boolean upload (String server, String user, String password, String dir, Paths paths, boolean passive) |
18 |
|
- |
throws IOException { |
19 |
|
- |
FTPClient ftp = new FTPClient(); |
20 |
|
- |
InetAddress address = InetAddress.getByName(server); |
21 |
|
- |
if (DEBUG) debug("Connecting to FTP server: " + address); |
22 |
|
- |
ftp.connect(address); |
23 |
|
- |
if (passive) ftp.enterLocalPassiveMode(); |
24 |
|
- |
if (!ftp.login(user, password)) { |
25 |
|
- |
if (ERROR) error("FTP login failed for user: " + user); |
26 |
|
- |
return false; |
27 |
|
- |
} |
28 |
|
- |
if (!ftp.changeWorkingDirectory(dir)) { |
29 |
|
- |
if (ERROR) error("FTP directory change failed: " + dir); |
30 |
|
- |
return false; |
31 |
|
- |
} |
32 |
|
- |
ftp.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE); |
33 |
|
- |
for (String path : paths) { |
34 |
|
- |
if (INFO) info("FTP upload: " + path); |
35 |
|
- |
BufferedInputStream input = new BufferedInputStream(new FileInputStream(path)); |
36 |
|
- |
try { |
37 |
|
- |
ftp.storeFile(new File(path).getName(), input); |
38 |
|
- |
} finally { |
39 |
|
- |
try { |
40 |
|
- |
input.close(); |
41 |
|
- |
} catch (Exception ignored) { |
42 |
|
- |
} |
43 |
|
- |
} |
44 |
|
- |
} |
45 |
|
- |
ftp.logout(); |
46 |
|
- |
ftp.disconnect(); |
47 |
|
- |
return true; |
48 |
|
- |
} |
|
12 |
+ |
public class FTP |
|
13 |
+ |
{ |
|
14 |
+ |
static public boolean upload( String server, String user, String password, String dir, Paths paths, boolean passive ) |
|
15 |
+ |
throws IOException |
|
16 |
+ |
{ |
|
17 |
+ |
FTPClient ftp = new FTPClient(); |
|
18 |
+ |
InetAddress address = InetAddress.getByName( server ); |
|
19 |
+ |
if ( DEBUG ) |
|
20 |
+ |
{ |
|
21 |
+ |
debug( "Connecting to FTP server: " + address ); |
|
22 |
+ |
} |
|
23 |
+ |
ftp.connect( address ); |
|
24 |
+ |
if ( passive ) |
|
25 |
+ |
{ |
|
26 |
+ |
ftp.enterLocalPassiveMode(); |
|
27 |
+ |
} |
|
28 |
+ |
if ( !ftp.login( user, password ) ) |
|
29 |
+ |
{ |
|
30 |
+ |
if ( ERROR ) |
|
31 |
+ |
{ |
|
32 |
+ |
error( "FTP login failed for user: " + user ); |
|
33 |
+ |
} |
|
34 |
+ |
return false; |
|
35 |
+ |
} |
|
36 |
+ |
if ( !ftp.changeWorkingDirectory( dir ) ) |
|
37 |
+ |
{ |
|
38 |
+ |
if ( ERROR ) |
|
39 |
+ |
{ |
|
40 |
+ |
error( "FTP directory change failed: " + dir ); |
|
41 |
+ |
} |
|
42 |
+ |
return false; |
|
43 |
+ |
} |
|
44 |
+ |
ftp.setFileType( org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE ); |
|
45 |
+ |
for ( String path : paths ) |
|
46 |
+ |
{ |
|
47 |
+ |
if ( INFO ) |
|
48 |
+ |
{ |
|
49 |
+ |
info( "FTP upload: " + path ); |
|
50 |
+ |
} |
|
51 |
+ |
BufferedInputStream input = new BufferedInputStream( new FileInputStream( path ) ); |
|
52 |
+ |
try |
|
53 |
+ |
{ |
|
54 |
+ |
ftp.storeFile( new File( path ).getName(), input ); |
|
55 |
+ |
} |
|
56 |
+ |
finally |
|
57 |
+ |
{ |
|
58 |
+ |
try |
|
59 |
+ |
{ |
|
60 |
+ |
input.close(); |
|
61 |
+ |
} |
|
62 |
+ |
catch ( Exception ignored ) |
|
63 |
+ |
{ |
|
64 |
+ |
} |
|
65 |
+ |
} |
|
66 |
+ |
} |
|
67 |
+ |
ftp.logout(); |
|
68 |
+ |
ftp.disconnect(); |
|
69 |
+ |
return true; |
|
70 |
+ |
} |
49 |
71 |
|
} |