Przesyłanie pliku przez FTP

0

Hej! Mam problem z wysłanie pliku na serwer FTP za pomocą aplikacji napisanej w Javie...
Rzuca mi wyjątek :
java.net.SocketTimeoutException: connect timed out

Kod klasy obsługującej operacje na FTP (znalazłem to w sieci):

import java.io.*;
import java.net.*;


public class SimpleFTPClient
{

  /** The URL connection object */
  private URLConnection m_client;

  /** The FTP host/server to be connected */
  private String host;

  /** The FTP user */
  private String user;

  /** The FTP user’s password */
  private String password;

  /** The remote file that needs to be uploaded or downloaded */
  private String remoteFile;

  /** The previous error message triggered after a method is called */
  private String erMesg;

  /** The previous success message after any method is called */
  private String succMesg;

  public SimpleFTPClient(){}

  /** Setter method for the FTP host/server */
  public void setHost (String host)
  {
    this.host = host;
  }

  /** Setter method for the FTP user */
  public void setUser (String user)
  {
    this.user = user;
  }

  /** Setter method for the FTP user’s password */
  public void setPassword (String p)
  {
    this.password = p;
  }

  /** Setter method for the remote file, this must include the sub-directory path relative
   to the user’s home directory, e.g you’e going to download a file that is within a sub directory
   called "sdir", and the file is named "d.txt", so you shall include the path as "sdir/d.txt"
  */
  public void setRemoteFile (String d)
  {
    this.remoteFile = d;
  }

  /** The method that returns the last message of success of any method call */
  public synchronized String getLastSuccessMessage()
  {
    if (succMesg==null ) return ""; return succMesg;
  }

  /** The method that returns the last message of error resulted from any exception of any method call */
  public synchronized String getLastErrorMessage()
  {
    if (erMesg==null ) return ""; return erMesg;
  }

  /** The method that handles file uploading, this method takes the absolute file path
   of a local file to be uploaded to the remote FTP server, and the remote file will then
   be transfered to the FTP server and saved as the relative path name specified in method setRemoteFile
   @param localfilename – the local absolute file name of the file in local hard drive that needs to
   FTP over
  */
  public synchronized boolean uploadFile (String localfilename)
  {
    try{

      InputStream is = new FileInputStream(localfilename);
      BufferedInputStream bis = new BufferedInputStream(is);
      OutputStream os =m_client.getOutputStream();
      BufferedOutputStream bos = new BufferedOutputStream(os);
      byte[] buffer = new byte[1024];
      int readCount;

      while( (readCount = bis.read(buffer)) > 0)
      {
            bos.write(buffer, 0, readCount);
      }
      bos.close();

      this.succMesg = "Uploaded!";

      return true;
    }
    catch(Exception ex)
    {
      StringWriter sw0= new StringWriter ();
      PrintWriter p0= new PrintWriter ( sw0, true );
      ex.printStackTrace ( p0 );
      erMesg = sw0.getBuffer().toString ();

      return false;
    }
  }

  /** The method to download a file and save it onto the local drive of the client in the specified absolut path
   @param localfilename – the local absolute file name that the file needs to be saved as */
  public synchronized boolean downloadFile (String localfilename)
  {
    try{
      InputStream is = m_client.getInputStream();
      BufferedInputStream bis = new BufferedInputStream(is);

      OutputStream os = new FileOutputStream(localfilename);
      BufferedOutputStream bos = new BufferedOutputStream(os);

      byte[] buffer = new byte[1024];
      int readCount;

      while( (readCount = bis.read(buffer)) > 0)
      {
        bos.write(buffer, 0, readCount);
      }
      bos.close();
      is.close (); // close the FTP inputstream
      this.succMesg = "Downloaded!";

      return true;
    }catch(Exception ex)
    {
      StringWriter sw0= new StringWriter ();
      PrintWriter p0= new PrintWriter ( sw0, true );
      ex.printStackTrace ( p0 );
      erMesg = sw0.getBuffer().toString ();

      return false;
    }
  }

  /** The method that connects to the remote FTP server */
  public synchronized boolean connect()
  {
    try{

    URL url = new URL("ftp://"+user+":"+password+"@"+host+"/"+remoteFile+";type=i");
    //  URL url = new URL("ftp://"+user+":"+password+"@"+host); 

    m_client = url.openConnection();
        m_client.setConnectTimeout(5000);

    return true;

    }
    catch(Exception ex)
    {
      StringWriter sw0= new StringWriter ();
      PrintWriter p0= new PrintWriter ( sw0, true );
      ex.printStackTrace ( p0 );
      erMesg = sw0.getBuffer().toString ();
      return false;
    }
  }
  

}
 

no i gdzieś w kodzie wykorzystuje tą klasę :)

 
SimpleFTPClient f= new SimpleFTPClient ();
  f.setHost("ftp.friko.pl");
  f.setUser("jakistamadresprzykladowy.za.pl");
  f.setPassword("haslomaslo123");
  boolean connected=f.connect(); 
  
        f.setRemoteFile("test.txt");
if ( connected){
  if (f.uploadFile("c:" + File.separator + "test.txt"))
    // display the message of success if uploaded
  System.out.println(f.getLastSuccessMessage ());
  else
    System.out.println(f.getLastErrorMessage ());
  }
  else
  // Display any connection exception, if any
    System.out.println(f.getLastErrorMessage ()); 

Dodam, że przez przeglądarkę normalnie się dostaje...

0

A mógłbys mi to zdebugować dać zrzut stosu?? Będzie przynajmniej widać stos wywołań i generalnie w którym momencie (która linia kodu) wywala timeouta. Chociaż pewnie przy próbie zapisu do strumienia przy uploadzie. Daj ten stos.

0

Jeszcze małe info, Java to wersja 1.7 update 2, Netbeans 7.01.

 
java.net.SocketTimeoutException: connect timed out
	at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:337)
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:198)
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
	at java.net.Socket.connect(Socket.java:579)
	at sun.net.ftp.impl.FtpClient.doConnect(FtpClient.java:938)
	at sun.net.ftp.impl.FtpClient.tryConnect(FtpClient.java:903)
	at sun.net.ftp.impl.FtpClient.connect(FtpClient.java:998)
	at sun.net.ftp.impl.FtpClient.connect(FtpClient.java:984)
	at sun.net.www.protocol.ftp.FtpURLConnection.connect(FtpURLConnection.java:294)
	at sun.net.www.protocol.ftp.FtpURLConnection.getOutputStream(FtpURLConnection.java:507)

Jak debuguje wygląda na to, że błąd jest przy metodzie uploadFile....klasy SimpleFTPClient :(

0

Tzn. wstawiłem kod tej klasy SimpleFTP...
A może ktoś zna inną metodę przesyłania przez FTP... Chyba nie powinno być to problemem do zaimplementowania w javie? Niby jest to język tworzony m.in z myślą o sieci, to nie powinno być problemów...

0

Przed chwilą próbowałem, ale niestety nie działa :( Rzuca taki wyjątek :

 
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
	at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:337)
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:198)
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180)
	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
	at java.net.Socket.connect(Socket.java:579)
	at org.apache.commons.net.SocketClient.connect(SocketClient.java:168)
	at org.apache.commons.net.SocketClient.connect(SocketClient.java:189)
	at org.apache.commons.net.SocketClient.connect(SocketClient.java:278)

Dokładniej u mnie w kodzie na tej linijce :
ftpClient.connect("ftp.friko.pl");
:(
Z przeglądarki normalnie wchodzę na ten serwer FTP.
Spróbuje wieczorem uruchomić ten program na innym komputerze w innej sieci zupełnie...
Może coś jest zablokowane? Ale Java korzysta domyślnie z ustawień domyślnej przeglądarki jeśli chodzi o proxy..

1 użytkowników online, w tym zalogowanych: 0, gości: 1