Hi I have a problem with my ftp, my code is not working. i may made mistake somehow, if some one can helping me pease. The code is like this. import java.io.*; import java.net.*; import java.util.*;
public class Ass_2 { public static void main(String[] args){ String currentPath = new String(); currentPath = "ftp://ftp.scit.wlv.ac.uk/pub"; try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String str,user,pass ; System.out.println("\n\nTYPE HELP for view the list of commands and there Formats!!"); System.out.println("enter command 'bye' to exit the application"); System.out.print("Enter Username: "); user = in.readLine(); System.out.print("Enter Password: "); pass = in.readLine(); String tempPath = new String(); String toFile = new String(); tempPath = currentPath ; readPage(currentPath); while (!(str = in.readLine()).equals("bye")){ int tokencount; StringTokenizer token = new StringTokenizer(str); tokencount = token.countTokens(); String block[] = new String[tokencount]; for (int i = 0; i < tokencount; i++) { block[i] = token.nextToken(); System.out.println(block[i]); } if (block[0].equalsIgnoreCase("dir")){ System.out.println("DIR"); System.out.println(currentPath); readPage(currentPath); System.out.println("\n"); }else if(block[0].equalsIgnoreCase("cwd")){ System.out.println("Change Directory"); currentPath = changeDirectory(currentPath, block[1]); System.out.println("\n"); }else if(block[0].equalsIgnoreCase("get")){ System.out.println("Wait Copying File : "+block[1]); copyFile(currentPath,block[1]); }else if(block[0].equalsIgnoreCase("cd..")){ currentPath = goBack(currentPath); }else if(block[0].equalsIgnoreCase("help")){ System.out.println("\nYour in the Help Section \n"); }else { System.out.println("Wrong Command or Format, Type Help!!!"); System.out.println("\n"); } } } catch(Exception e){ System.out.println(e); } }
// Function to read content form the specfied path static void readPage(String path){ String tempPath = new String(); try { URL pageref = new URL(path); InputStream in = pageref.openStream(); BufferedReader inline = new BufferedReader(new InputStreamReader(in));//(instr); String line; int linect = 0; while((line = inline.readLine())!=null){ linect++; System.out.println(line); } System.out.println("lines read = "+linect); } catch(Exception e){ System.out.println(e); } } static String changeDirectory(String path,String dir){ path = path+"/"+dir;//System.out.println(">>>>>>>>>>> " +path+" >>> \n"); readPage(path);
return path; } // Function to go one setp back from the current directory static String goBack(String currentPath){ return currentPath; } /// Function for basic copy TXT file static void copyFile(String path,String file){ try { path = path +"/"+ file; //System.out.println("Copy Path : " + path); String fileName = new String(); fileName = file+".txt"; System.out.println("Out put file name : " + fileName); OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(fileName)); URL pageref = new URL(path); InputStream in = pageref.openStream(); BufferedReader inline = new BufferedReader(new InputStreamReader(in));//(instr); String line; int linect = 0; while((line = inline.readLine())!=null){ System.out.println(line); linect++; //System.out.println(line); out.write(line+"\r\n"); } out.close(); System.out.println("File - " +file+ " - copyed Successfully !!"); } catch(Exception e){ System.out.println(e); } } }
View All Comments
| View Tutorial