Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.08 KB | None | 0 0
  1. /*!
  2. * @readerief Client FTP
  3. * @author Romain Leborgne
  4. * @author Nicolas Pieuchot
  5. * @date 2011
  6. * @version 1.0
  7. */
  8.  
  9. import java.io.*;
  10. import java.net.Socket;
  11. import java.util.StringTokenizer;
  12.  
  13. public class Client
  14. {
  15. public static Integer portInt;
  16. public static String addr = "stockage.univ-readerest.fr";
  17. public static String adresseIp=new String();
  18.  
  19. /*!
  20. * @fn public static Boolean PWD(PrintWriter writer, BufferedReader reader)
  21. * @exception IOException
  22. * @readerief Fonction qui genere la commande PWD du protocole FTP
  23. * @param writer : PrintWriter, reader : BufferedReader.
  24. * @return un booleen : true si la commande ftp a fonctionne false sinon.
  25. * Permet d'afficher le chemin courant.
  26. */
  27. public static Boolean PWD(PrintWriter writer, BufferedReader reader) throws IOException
  28. {
  29. String str = new String();
  30. writer.println("PWD");
  31. str = reader.readLine();
  32. if(str.substring(0,3).equals("257")){
  33. System.out.println("PWD : "+str.substring(3));
  34. return true;
  35. }
  36. return false;
  37. }
  38.  
  39. /*!
  40. * @fn CWD(PrintWriter writer, BufferedReader reader, String path)
  41. * @exception IOException
  42. * @readerief Fonction qui genere la commande CWD du protocole FTP
  43. * @param writer : PrintWriter, reader : BufferedReader, path : String.
  44. * @return un booleen : true si la commande ftp a fonctionne false sinon.
  45. * Permet de changer de repertoire courant.
  46. */
  47. public static Boolean CWD(PrintWriter writer, BufferedReader reader, String path) throws IOException
  48. {
  49. String str = new String();
  50. writer.println("CWD "+path);
  51. str = reader.readLine();
  52. //System.out.println(str);
  53. if(str.substring(0,3).equals("250")){
  54. System.out.println(str.substring(3));
  55. return true;
  56. }
  57. return false;
  58. }
  59.  
  60. /*!
  61. * @fn public static Boolean PASV(PrintWriter writer, BufferedReader reader)
  62. * @exception IOException
  63. * @readerief Fonction qui genere la commande PASV du protocole FTP
  64. * @param writer : PrintWriter, reader : BufferedReader.
  65. * @return un booleen : true si la commande ftp a fonctionne false sinon.
  66. * Permet de passer en mode passif.
  67. */
  68. public static Boolean PASV(PrintWriter writer, BufferedReader reader) throws IOException
  69. {
  70. String str = new String();
  71. int j=0;
  72. String tabString[]=new String[6];
  73. writer.println("PASV"+str);
  74. str = reader.readLine();
  75.  
  76. String port = new String();
  77. StringTokenizer ip = new StringTokenizer(str, "(), ABCDEFGHIJKLMOPQRSTUVWXYZabcdefghijklmnopkrstuvwxyz");
  78. ip.nextToken();
  79. if(str.substring(0,3).equals("227")){
  80. while(ip.hasMoreTokens())
  81. {
  82. tabString[j]=ip.nextToken();
  83. if(j<4) adresseIp=adresseIp+tabString[j]+".";
  84. else port += tabString[j];
  85. j++;
  86. }
  87. portInt = Integer.parseInt(tabString[j-2])*256 + Integer.parseInt(tabString[j-1]);
  88. System.out.println("Adresse IP : "+adresseIp + " Port : "+portInt);
  89. return true;
  90. }
  91. return false;
  92. }
  93.  
  94. /*!
  95. * @fn public static Boolean STOR(PrintWriter writer, BufferedReader reader, String file)
  96. * @exception IOException
  97. * @readerief Fonction qui genere la commande STOR du protocole FTP
  98. * @param writer : PrintWriter, reader : BufferedReader,is : InputStream file : String
  99. * @return un booleen : true si la commande ftp a fonctionne false sinon.
  100. * Permet d'uploader un fichier sur le FTP.
  101. */
  102. public static Boolean STOR(PrintWriter writer, BufferedReader reader, String file_name) throws IOException
  103. {
  104. Socket sock = new Socket(adresseIp,21);
  105. FileInputStream file = new FileInputStream(file_name);
  106. /*buffer d'écriture */
  107. PrintWriter w = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sock.getOutputStream())),true);
  108. /*buffer de lecture */
  109. BufferedReader r = new BufferedReader(new InputStreamReader(file));
  110.  
  111.  
  112. String str = new String();
  113. String str_line = new String();
  114. writer.println("STOR "+file_name);
  115. str = reader.readLine();
  116. System.out.println(str);
  117. if(str.substring(0,3).equals("150")){
  118. System.out.println(str.substring(3));
  119. str_line = r.readLine();
  120. while (str_line != null) {
  121. w.println(str_line);
  122. }
  123.  
  124. return true;
  125. }
  126. return false;
  127. }
  128.  
  129. /*!
  130. * @fn public static Boolean QUIT(PrintWriter writer, BufferedReader reader)
  131. * @exception IOException
  132. * @readerief Fonction qui genere la commande QUIT du protocole FTP
  133. * @param writer : PrintWriter, reader : BufferedReader.
  134. * @return un booleen : true si la commande ftp a fonctionne false sinon.
  135. * Permet de quitter le FTP.
  136. */
  137. public static Boolean QUIT(PrintWriter writer, BufferedReader reader) throws IOException
  138. {
  139. String str = new String();
  140. writer.println("QUIT");
  141. str = reader.readLine();
  142. if(str.substring(0,3).equals("221")){
  143. System.out.println(str.substring(3));
  144. return true;
  145. }
  146. return false;
  147. }
  148.  
  149. /*!
  150. * @fn static Boolean connexion(PrintWriter writer, BufferedReader reader)
  151. * @exception IOException
  152. * @readerief Fonction de connexion au serveur FTP
  153. * @param writer : PrintWriter, reader : BufferedReader.
  154. * @return un booleen : true si la commande ftp a fonctionne false sinon.
  155. * Permet de se connecter au FTP.
  156. */
  157. public static Boolean connexion(PrintWriter writer, BufferedReader reader) throws IOException
  158. {
  159. BufferedReader entree = new BufferedReader(new InputStreamReader(System.in));
  160. String str = new String();
  161. //String user = "e20703933";
  162. //String pass = "pemeenam1";
  163. String response = new String();
  164. response = reader.readLine();
  165. System.out.println(response.substring(0, 3));
  166. if(response.substring(0,3).equals("220"))
  167. {
  168. System.out.print("Username : ");
  169. str = entree.readLine();
  170. writer.println("USER "+str);
  171. response = reader.readLine();
  172. if(response.substring(0,3).equals("331"))
  173. {
  174. System.out.print("Password : ");
  175. str = entree.readLine();
  176.  
  177. writer.println("PASS "+str);
  178. response = reader.readLine();
  179. if(response.substring(0,3).equals("230"))
  180. {
  181. System.out.println("Connection successfull !");
  182. return true;
  183. }
  184. System.out.println("Wrong password");
  185. return false;
  186.  
  187. }
  188. return false;
  189. }
  190. System.out.println(response);
  191. return false;
  192. }
  193.  
  194. public static void main(String [] args){
  195. System.getProperties().put("http.proxyHost", "proxyubo.univ-readerest.fr");
  196. System.getProperties().put("http.proxyPort", "3128");
  197.  
  198. try{
  199. Socket sock = new Socket(addr,21);
  200.  
  201. PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sock.getOutputStream())),true);
  202. BufferedReader reader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
  203.  
  204. if(connexion(writer,reader)){
  205. if(PWD(writer,reader))
  206. {
  207. if(CWD(writer,reader,"WinXp"))
  208. {
  209. if(PWD(writer,reader))
  210. {
  211. if(PASV(writer,reader)){
  212. System.out.println("mode passif : ON");
  213. STOR(writer,reader,"D:\\produit.c");
  214. QUIT(writer,reader);
  215. }
  216. }
  217. }
  218. }
  219. }
  220.  
  221.  
  222. writer.println("END") ;
  223. reader.close();
  224. writer.close();
  225. sock.close();
  226.  
  227. }catch (IOException ioe){}
  228. }
  229.  
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement