Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*!
- * @readerief Client FTP
- * @author Romain Leborgne
- * @author Nicolas Pieuchot
- * @date 2011
- * @version 1.0
- */
- import java.io.*;
- import java.net.Socket;
- import java.util.StringTokenizer;
- public class Client
- {
- public static Integer portInt;
- public static String addr = "stockage.univ-readerest.fr";
- public static String adresseIp=new String();
- /*!
- * @fn public static Boolean PWD(PrintWriter writer, BufferedReader reader)
- * @exception IOException
- * @readerief Fonction qui genere la commande PWD du protocole FTP
- * @param writer : PrintWriter, reader : BufferedReader.
- * @return un booleen : true si la commande ftp a fonctionne false sinon.
- * Permet d'afficher le chemin courant.
- */
- public static Boolean PWD(PrintWriter writer, BufferedReader reader) throws IOException
- {
- String str = new String();
- writer.println("PWD");
- str = reader.readLine();
- if(str.substring(0,3).equals("257")){
- System.out.println("PWD : "+str.substring(3));
- return true;
- }
- return false;
- }
- /*!
- * @fn CWD(PrintWriter writer, BufferedReader reader, String path)
- * @exception IOException
- * @readerief Fonction qui genere la commande CWD du protocole FTP
- * @param writer : PrintWriter, reader : BufferedReader, path : String.
- * @return un booleen : true si la commande ftp a fonctionne false sinon.
- * Permet de changer de repertoire courant.
- */
- public static Boolean CWD(PrintWriter writer, BufferedReader reader, String path) throws IOException
- {
- String str = new String();
- writer.println("CWD "+path);
- str = reader.readLine();
- //System.out.println(str);
- if(str.substring(0,3).equals("250")){
- System.out.println(str.substring(3));
- return true;
- }
- return false;
- }
- /*!
- * @fn public static Boolean PASV(PrintWriter writer, BufferedReader reader)
- * @exception IOException
- * @readerief Fonction qui genere la commande PASV du protocole FTP
- * @param writer : PrintWriter, reader : BufferedReader.
- * @return un booleen : true si la commande ftp a fonctionne false sinon.
- * Permet de passer en mode passif.
- */
- public static Boolean PASV(PrintWriter writer, BufferedReader reader) throws IOException
- {
- String str = new String();
- int j=0;
- String tabString[]=new String[6];
- writer.println("PASV"+str);
- str = reader.readLine();
- String port = new String();
- StringTokenizer ip = new StringTokenizer(str, "(), ABCDEFGHIJKLMOPQRSTUVWXYZabcdefghijklmnopkrstuvwxyz");
- ip.nextToken();
- if(str.substring(0,3).equals("227")){
- while(ip.hasMoreTokens())
- {
- tabString[j]=ip.nextToken();
- if(j<4) adresseIp=adresseIp+tabString[j]+".";
- else port += tabString[j];
- j++;
- }
- portInt = Integer.parseInt(tabString[j-2])*256 + Integer.parseInt(tabString[j-1]);
- System.out.println("Adresse IP : "+adresseIp + " Port : "+portInt);
- return true;
- }
- return false;
- }
- /*!
- * @fn public static Boolean STOR(PrintWriter writer, BufferedReader reader, String file)
- * @exception IOException
- * @readerief Fonction qui genere la commande STOR du protocole FTP
- * @param writer : PrintWriter, reader : BufferedReader,is : InputStream file : String
- * @return un booleen : true si la commande ftp a fonctionne false sinon.
- * Permet d'uploader un fichier sur le FTP.
- */
- public static Boolean STOR(PrintWriter writer, BufferedReader reader, String file_name) throws IOException
- {
- Socket sock = new Socket(adresseIp,21);
- FileInputStream file = new FileInputStream(file_name);
- /*buffer d'écriture */
- PrintWriter w = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sock.getOutputStream())),true);
- /*buffer de lecture */
- BufferedReader r = new BufferedReader(new InputStreamReader(file));
- String str = new String();
- String str_line = new String();
- writer.println("STOR "+file_name);
- str = reader.readLine();
- System.out.println(str);
- if(str.substring(0,3).equals("150")){
- System.out.println(str.substring(3));
- str_line = r.readLine();
- while (str_line != null) {
- w.println(str_line);
- }
- return true;
- }
- return false;
- }
- /*!
- * @fn public static Boolean QUIT(PrintWriter writer, BufferedReader reader)
- * @exception IOException
- * @readerief Fonction qui genere la commande QUIT du protocole FTP
- * @param writer : PrintWriter, reader : BufferedReader.
- * @return un booleen : true si la commande ftp a fonctionne false sinon.
- * Permet de quitter le FTP.
- */
- public static Boolean QUIT(PrintWriter writer, BufferedReader reader) throws IOException
- {
- String str = new String();
- writer.println("QUIT");
- str = reader.readLine();
- if(str.substring(0,3).equals("221")){
- System.out.println(str.substring(3));
- return true;
- }
- return false;
- }
- /*!
- * @fn static Boolean connexion(PrintWriter writer, BufferedReader reader)
- * @exception IOException
- * @readerief Fonction de connexion au serveur FTP
- * @param writer : PrintWriter, reader : BufferedReader.
- * @return un booleen : true si la commande ftp a fonctionne false sinon.
- * Permet de se connecter au FTP.
- */
- public static Boolean connexion(PrintWriter writer, BufferedReader reader) throws IOException
- {
- BufferedReader entree = new BufferedReader(new InputStreamReader(System.in));
- String str = new String();
- //String user = "e20703933";
- //String pass = "pemeenam1";
- String response = new String();
- response = reader.readLine();
- System.out.println(response.substring(0, 3));
- if(response.substring(0,3).equals("220"))
- {
- System.out.print("Username : ");
- str = entree.readLine();
- writer.println("USER "+str);
- response = reader.readLine();
- if(response.substring(0,3).equals("331"))
- {
- System.out.print("Password : ");
- str = entree.readLine();
- writer.println("PASS "+str);
- response = reader.readLine();
- if(response.substring(0,3).equals("230"))
- {
- System.out.println("Connection successfull !");
- return true;
- }
- System.out.println("Wrong password");
- return false;
- }
- return false;
- }
- System.out.println(response);
- return false;
- }
- public static void main(String [] args){
- System.getProperties().put("http.proxyHost", "proxyubo.univ-readerest.fr");
- System.getProperties().put("http.proxyPort", "3128");
- try{
- Socket sock = new Socket(addr,21);
- PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sock.getOutputStream())),true);
- BufferedReader reader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
- if(connexion(writer,reader)){
- if(PWD(writer,reader))
- {
- if(CWD(writer,reader,"WinXp"))
- {
- if(PWD(writer,reader))
- {
- if(PASV(writer,reader)){
- System.out.println("mode passif : ON");
- STOR(writer,reader,"D:\\produit.c");
- QUIT(writer,reader);
- }
- }
- }
- }
- }
- writer.println("END") ;
- reader.close();
- writer.close();
- sock.close();
- }catch (IOException ioe){}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement