java - Can't transfer a file with a binary transfer with a FTP client -


have ftp client transfer files byte array. in console says transferred doesn't transfer , these errors:

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:110) @ ftp.test.main(test.java:12) 

i want files tranferred filepath: c:/users/username/desktop/ftp

this program:

package ftp;  import java.io.bufferedreader; import java.io.bufferedwriter; import java.io.datainputstream; import java.io.dataoutputstream; import java.io.file; import java.io.ioexception; import java.io.inputstreamreader; import java.io.outputstreamwriter; import java.net.socket; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.net.url; import java.net.urlconnection; import java.util.scanner;  public class ftp { public synchronized void connect(string host, int port, string user,         string pass) throws ioexception {     socket socket = new socket(host, port);     //  if (socket != null) {     //     throw new ioexception("simpleftp connected. disconnect first.");     //   }     //  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);     // response = reader.readline();     /*  if (!response.startswith("331 ")) {           throw new ioexception(               "simpleftp received unknown response after sending user: "                   + response);         }         else {*/     writer.write("pass hello" +"\r\n");     writer.flush();     response= reader.readline();     system.out.println("3"+response);     //}       //response = reader.readline();     /*if (!response.startswith("230 ")) {           throw new ioexception(               "simpleftp unable log in supplied password: "                   + response);         }         else {*/     //system.out.println("4"+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;     system.out.println(port1);     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);       string text;     while((text =reader2.readline())!=null){         system.out.println(text);          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();         }      }   }         } 

output:

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,219,100) 6200 type set 7150 opening data channel directory listing of "/" -rw-r--r-- 1 ftp ftp              0 apr 14 20:18 abc.txt 8226 transferred "/" 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:112) @ ftp.test.main(test.java:12) 

ok. there number of issues fix here. first, every time need data on data channel (using arbitrary high port you're calculating passive response), need issue pasv command , open new socket using new port.

try this, code, starting list command:

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 test.txt\r\n");     writer.flush();     system.out.println(response);      byte[] mybytearray = new byte[1024];     // try-with-resources     try (socket filesocket = new socket("localhost", passiveport);          inputstream = filesocket.getinputstream();          fileoutputstream fos = new fileoutputstream("c:\\temp\\" + file)) {          int count;          while ((count = is.read(mybytearray)) >= 0) {              fos.write(mybytearray, 0, count);          }     } } 

getport then, of course, looks this:

private int getport(bufferedreader reader, bufferedwriter writer) throws ioexception {     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; } 

i'll let write parsefilename; that's matter of pulling filename raw listing returned server.

this code should trick you, , worked me against local apache ftp server. since you're using filezilla, may need read in line somewhere if they're returning more data server -- otherwise should work you.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -