java - Getting "501 Syntax error" when issuing RETR command to FTP server -
i've made ftp client have retr
, list
commands, have got problem. after implemented retr
command keep getting error:
1:220-filezilla server version 0.9.50 beta 1:220-written tim kosse (tim.kosse@filezilla-project.org) 1:220 please visit https://filezilla-project.org/ 2331 password required geek 3230 logged on 5227 entering passive mode (127,0,0,1,217,137) 6200 type set 7150 opening data channel directory listing of "/" 5227 entering passive mode (127,0,0,1,217,139) 150 opening data channel directory listing of "/" 8501 syntax error exception in thread "main" java.net.connectexception: connection refused: connect @ java.net.dualstackplainsocketimpl.connect0(native method) @ java.net.dualstackplainsocketimpl.socketconnect(unknown source) @ java.net.abstractplainsocketimpl.doconnect(unknown source) @ java.net.abstractplainsocketimpl.connecttoaddress(unknown source) @ java.net.abstractplainsocketimpl.connect(unknown source) @ java.net.plainsocketimpl.connect(unknown source) @ java.net.sockssocketimpl.connect(unknown source) @ java.net.socket.connect(unknown source) @ java.net.socket.connect(unknown source) @ java.net.socket.<init>(unknown source) @ java.net.socket.<init>(unknown source) @ ftp.ftp.connect(ftp.java:135) @ ftp.test.main(test.java:12)
furthermore after implemented retr
command client stopped giving list
command; doesn't tell files there in folder.
public class ftp{ public synchronized void connect(string host, int port, string user, string pass) throws ioexception { socket socket = new socket(host, port); bufferedreader reader = new bufferedreader(new inputstreamreader(socket.getinputstream())); bufferedwriter writer = new bufferedwriter( new outputstreamwriter(socket.getoutputstream())); string response = reader.readline(); if (!response.startswith("220-")) { throw new ioexception( "ftp modtog et ukendt svar, da den blev forbundet til ftp server: " + response); } else {system.out.println("1:"+response); response = reader.readline(); system.out.println("1:"+response); response = reader.readline(); system.out.println("1:"+response); } writer.write("user geek"+"\r\n"); writer.flush(); response= reader.readline(); system.out.println("2"+response); writer.write("pass hello" +"\r\n"); writer.flush(); response= reader.readline(); system.out.println("3"+response); writer.write("pasv"+"\r\n"); writer.flush(); response= reader.readline(); system.out.println("5"+response ); string [] r=response.trim().split(","); string reg=r[4]; string add= r[5].replace(")", ""); int m = integer.parseint(reg); int = integer.parseint(add); int port1 = m*256+a; socket datasocket = new socket("127.0.0.1", port1); writer.write("type a\r\n"); writer.flush(); response= reader.readline(); system.out.println("6"+response); bufferedreader reader2 = new bufferedreader(new inputstreamreader(datasocket.getinputstream())); writer.write("list\r\n"); writer.flush(); response = reader.readline(); system.out.println("7" + response); list<string> files = new arraylist<>(); string text; while ((text = reader2.readline()) != null) { string filename = parsefilename(text); files.add(filename); } reader2.close(); datasocket.close(); reader.readline(); (string file : files) { int passiveport = getport(reader, writer); writer.write("retr\r\n"); writer.flush(); system.out.println(response); byte[] mybytearray = new byte[1024]; try (socket filesocket = new socket("localhost", passiveport); inputstream = filesocket.getinputstream(); fileoutputstream fos = new fileoutputstream("c:/users/username/desktop/ftp/" + file)) { int count; while ((count = is.read(mybytearray)) >= 0) { fos.write(mybytearray, 0, count); } } } writer.write("retr\r\n"); writer.flush(); response= reader.readline(); system.out.println("8"+response); socket filsocket = new socket ("localhost", port1); inputstream = null; fileoutputstream fos = null; byte[] mybytearray = new byte[1024]; try { = filsocket.getinputstream(); fos = new fileoutputstream("myfile.txt"); int count; while ((count = is.read(mybytearray)) >= 0) { fos.write(mybytearray, 0, count); } } { fos.close(); is.close(); filsocket.close(); } } private string parsefilename(string text) { return "abc.txt"; } private int getport(bufferedreader reader, bufferedwriter writer) throws ioexception { //this line 135 string response; writer.write("pasv" + "\r\n"); writer.flush(); response = reader.readline(); system.out.println("5" + response); string[] r = response.trim().split(","); string reg = r[4]; string add = r[5].replace(")", ""); int m = integer.parseint(reg); int = integer.parseint(add); return m * 256 + a; } }
thanks in advance :)
the retr
command has mandatory argument specifies name of file download. how server know, file want download otherwise?
writer.write("retr " + file + "\r\n");
see rfc 959:
retr <sp> <pathname> <crlf>
anyway, why implement ftp protocol on own? use existing implementation, ftpclient
apache commons.
Comments
Post a Comment