Skip to content

Commit

Permalink
Import of project
Browse files Browse the repository at this point in the history
  • Loading branch information
EutiziStefano committed Nov 30, 2019
1 parent 90c7b11 commit c91f4d8
Show file tree
Hide file tree
Showing 10 changed files with 814 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin/
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>SFTP-SSL-Client</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,28 @@
# SFTP-SSL-Client
A Java SFTP/FTPS Client for command line file transfer upload/download

You need it.sauronsoftware.ftp4j to get client working

#Transfer mode
there is three types of file transfer:
- All files in a directory
- A fixed list of files
- A variable file name with date in it (dynamic)


#Proxy
You can set different proxy types:
HTTPTunnelConnector
FTPProxyConnector
SOCKS4Connector
SOCKS5Connector


#EXTRA
The script allow you to:
- set file permission (linux chmod)
- execute a custom command, the file is passed as an argument


## P.S.
Sorry but the English translation is in progress, parameters and classes are an half in Italian
71 changes: 71 additions & 0 deletions configuration.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# tipo di traferimento: download | upload
TIPO_TRAFERIMENTO=upload

# logleve: INFO | DEBUG
LOGLEVEL=DEBUG

#SSL activation (SSL|SSLv3|false)
SSL=SSLv3
# SSL type (IMPLICIT|EXPLICIT)
SSL_TYPE=EXPLICIT

# passive mode: true o false
PASSIVE=true

# tipo trasferimento: AUTO, BINARY, TEXTUAL
TRANSFER_TYPE=AUTO

# Eliminazione dei file/s sorgenti
DELETE_SOURCE_FILES=false

# se download posso impostare i permessi del file es: 644
is_set_PERMESSI=false
PERMESSI=644


# dati connessione
IP=rmftp1.postel.com
PORTA=2110
USER=2012294
PW=YMXXXXXX

#proxy true|false
proxy=false
# type: HTTPTunnelConnector | FTPProxyConnector | SOCKS4Connector | SOCKS5Connector
proxy_type=SOCKS5Connector
proxy_ip=192.168.78.69
proxy_port=1080
# Proxy is authenticated
proxy_auth=false
proxy_user=
proxy_pass=



# numero di file da trasferire, da inserire un percorso per ogni file
NUMERO_FILE=1

# trasferimento file:
# - dinamico (sostituisce %data% dei nomi file con la data odierna nel formato specificato)
# - statico (copia i files specificati come ORIGINE - DESTINAZIONE e NUMERO_FILE)
# - all_files (copia tutti i files nella dalla cartella di input nella cartella di output)
TRASFERIMENTO_FILE=all_files

# formato della data es: yyyy-MM-dd HH:mm:ss.S
FORMATO=yyyyMMdd

# comando da eseguire su ogni file, indicare se necessario con %file% il nome del file stesso
is_set_COMANDO=false
COMANDO=dos2unix %file% %file%

#CARTELLA REMOTA
REMOTE_FOLDER=/lotti/
#CARTELLA LOCALE
LOCAL_FOLDER=/home/stefano/conservazione/

# specifica dei file con percorso completo, origine e destinazione inserire (se dinamico, dove va inserita la data mettere %data% nel punto esatto)
ORIGINE_FILE_1=prova.zip
DESTINAZIONE_FILE_1=prova.zip



34 changes: 34 additions & 0 deletions configuration.properties_example2
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# tipo di traferimento: download | upload
TIPO_TRAFERIMENTO=download

# dati connessione
IP=10.207.230.60
USER=ptc3012
PW=ptc3012

# numero di file da trasferire, da inserire un percorso per ogni file
NUMERO_FILE=4

# file dinamico con data
DINAMICO=true
# formato della data es: yyyy-MM-dd HH:mm:ss.S
FORMATO=yyyyMMdd

#CARTELLA REMOTA
REMOTE_FOLDER=/transfer/bolrep/
#CARTELLA LOCALE
LOCAL_FOLDER=/web/postalizzazioni/bollettini_telematici/

# specifica dei file con percorso completo, origine e destinazione inserire (se dinamico, dove va inserita la data mettere %data% nel punto esatto)
ORIGINE_FILE_1=BollettinoReport55111.%data%.txt
DESTINAZIONE_FILE_1=BollettinoReport55111.%data%.txt

ORIGINE_FILE_2=BollettinoReport55111.%data%.xml
DESTINAZIONE_FILE_2=BollettinoReport55111.%data%.xml

ORIGINE_FILE_3=BollettinoReport77555.%data%.txt
DESTINAZIONE_FILE_3=BollettinoReport77555.%data%.txt

ORIGINE_FILE_4=BollettinoReport77555.%data%.xml
DESTINAZIONE_FILE_4=BollettinoReport77555.%data%.xml

18 changes: 18 additions & 0 deletions src/config/carica.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package config;

public class carica {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub






}

}
65 changes: 65 additions & 0 deletions src/main/put.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main;

import it.sauronsoftware.ftp4j.FTPAbortedException;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferException;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPFile;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
import it.sauronsoftware.ftp4j.FTPListParseException;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class put{
/*
public static void main(String[] args) throws IllegalStateException, IOException, FTPIllegalReplyException, FTPException, FTPDataTransferException, FTPAbortedException, FTPListParseException{
TrustManager[] trustManager = new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} };
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustManager, new SecureRandom());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
FTPClient client = new FTPClient();
client.setSSLSocketFactory(sslSocketFactory);
//client.setSecurity(FTPClient.SECURITY_FTPS); // implicito
client.setSecurity(FTPClient.SECURITY_FTPES); // esplicito
client.connect("rmftp1.postel.com", 2110);
client.login("2012294", "YM372619");
FTPFile[] list = client.list();
for (FTPFile ftpFile : list) {
System.out.println(ftpFile.getName());
}
client.disconnect(true);
}
*/


}
121 changes: 121 additions & 0 deletions src/main/testFTP.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package main;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;

public class testFTP {

/**
* @param args
*/
public static void main(String[] args) {

FTPClient client = null;

try {
Properties properties = new Properties();
properties.load(new FileInputStream("configuration.properties"));

String TIPO_TRAFERIMENTO = properties.getProperty("TIPO_TRAFERIMENTO");
String IP = properties.getProperty("IP");
String USER = properties.getProperty("USER");
String PW = properties.getProperty("PW");
String NUMERO_FILE = properties.getProperty("NUMERO_FILE");
String DINAMICO = properties.getProperty("DINAMICO");
String FORMATO = properties.getProperty("FORMATO");
String REMOTE_FOLDER = properties.getProperty("REMOTE_FOLDER");
String LOCAL_FOLDER = properties.getProperty("LOCAL_FOLDER");
String PERMESSI = properties.getProperty("PERMESSI");
String COMANDO = properties.getProperty("COMANDO");

// CONNESSIONE
client = new FTPClient();
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
client.configure(conf);
client.connect(IP);
client.login(USER, PW);

// COSTRUISCO DUE ARRAY CON I FILE SORGENTE E DESTINAZIONE
int numero_files = Integer.parseInt(NUMERO_FILE);
String[] sorgenti = new String[numero_files];
String[] destinazioni = new String[numero_files];

if (DINAMICO.equals("true")) {
Date date = Calendar.getInstance().getTime();
DateFormat formatter = new SimpleDateFormat(FORMATO);
String today_formattato = formatter.format(date);

System.out.println("Data costruita: " + today_formattato);

for (int i = 1; i <= numero_files; i++) {
String sorgente = properties.getProperty("ORIGINE_FILE_" + i);
String destinazione = properties.getProperty("DESTINAZIONE_FILE_" + i);

sorgenti[i - 1] = sorgente.replaceAll("%data%", today_formattato);
destinazioni[i - 1] = destinazione.replaceAll("%data%", today_formattato);
}
} else {
for (int i = 1; i <= numero_files; i++) {
String sorgente = properties.getProperty("ORIGINE_FILE_" + i);
String destinazione = properties.getProperty("DESTINAZIONE_FILE_" + i);

sorgenti[i - 1] = sorgente;
destinazioni[i - 1] = destinazione;
}
}

for (int i = 0; i < sorgenti.length; i++) {

String s = sorgenti[i];
String d = destinazioni[i];

client.changeWorkingDirectory(REMOTE_FOLDER);
client.setFileType(FTPClient.BINARY_FILE_TYPE);
System.out.println(TIPO_TRAFERIMENTO + " del file remoto: " + REMOTE_FOLDER + s + " locale: " + LOCAL_FOLDER + d);

if (TIPO_TRAFERIMENTO.equals("download")) {
String f = LOCAL_FOLDER + d;
FileOutputStream fos = new FileOutputStream(f);
client.retrieveFile(s, fos);
Runtime.getRuntime().exec("chmod " + PERMESSI + " " + f);
String comando_dopo_replace = COMANDO.replaceAll("%file%", f);
Runtime.getRuntime().exec(comando_dopo_replace);
} else if (TIPO_TRAFERIMENTO.equals("upload")) {
String lfile = LOCAL_FOLDER + s;
File fi = new File(lfile);
FileInputStream fin = new FileInputStream(fi);
client.storeFile(s, fin);
}
}
} catch (IOException e) {
System.out.println("###########################");
System.out.println("Properties File Not Found");
System.out.println("###########################");
e.printStackTrace();
} catch (IllegalStateException e) {
System.out.println("###########################");
System.out.println("FTP Connection Error");
System.out.println("###########################");
e.printStackTrace();
e.printStackTrace();
} finally {
if (client != null) {
try {
client.disconnect();
} catch (Exception e) {
System.err.println("ERROR : Error in disconnecting the Remote Machine");
}
}
}
}
}
Loading

0 comments on commit c91f4d8

Please sign in to comment.