2013-07-03 29 views
7

Zastanawiałem się, czy istnieje prosty sposób, aby wysłać mały plik na serwer ftp. Sprawdziłem bibliotekę Net Apache Commons, ale wydaje mi się to dość skomplikowane. Czy istnieje prostszy sposób na przesłanie małego pliku do ftp?Przesyłanie do FTP przy użyciu Javy

Skończyło się na używaniu Biblioteki Net Apache Commons, nie było zbyt trudne.

Odpowiedz

2

Użyj Apache Commons lib ma tego narzędzia org.apache.commons.net.ftp.FTPClient:

http://commons.apache.org/

import org.apache.commons.net.ftp.FTPClient; 
FTPClient client = new FTPClient(); 
String sFTP = "ftp.miservidor.com"; 
String sUser = "usuario"; 
String sPassword = "password"; 

try { 
    client.connect(sFTP); 
    boolean login = client.login(sUser,sPassword); 
} catch (IOException ioe) {} 

Pełny przykład: http://www.google.es/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&ved=0CD8QFjAB&url=http%3A%2F%2Fcommons.apache.org%2Fproper%2Fcommons-net%2Fexamples%2Fftp%2FFTPClientExample.java&ei=JGLUUdsrotHsBtj9gNgI&usg=AFQjCNHymPhHZOKh2S4yuLMTYHTVTnQ02g&sig2=8kJSz_Gl-vsPECe8sIxLGA&bvm=bv.48705608,d.ZGU&cad=rja

+2

A jak bym przesłać plik po zalogowaniu ? – user2526311

18

z tego linku: Upload files to FTP server using URLConnection class. Nie jest wymagana żadna biblioteka zewnętrzna.

String ftpUrl = "ftp://%s:%[email protected]%s/%s;type=i"; 
String host = "www.myserver.com"; 
String user = "tom"; 
String pass = "secret"; 
String filePath = "E:/Work/Project.zip"; 
String uploadPath = "/MyProjects/archive/Project.zip"; 

ftpUrl = String.format(ftpUrl, user, pass, host, uploadPath); 
System.out.println("Upload URL: " + ftpUrl); 

try { 
    URL url = new URL(ftpUrl); 
    URLConnection conn = url.openConnection(); 
    OutputStream outputStream = conn.getOutputStream(); 
    FileInputStream inputStream = new FileInputStream(filePath); 

    byte[] buffer = new byte[BUFFER_SIZE]; 
    int bytesRead = -1; 
    while ((bytesRead = inputStream.read(buffer)) != -1) { 
     outputStream.write(buffer, 0, bytesRead); 
    } 

    inputStream.close(); 
    outputStream.close(); 

    System.out.println("File uploaded"); 
} catch (IOException ex) { 
    ex.printStackTrace(); 
} 
+3

To jest bardzo dobre - tylko mała korekta - użytkownik i hasło muszą być zakodowane za pomocą funkcji URLEncoder.encode –

1

myślę, znalazłem całkiem ładny próbkę za pomocą org.apache.commons.net.ftp.FTPClienthere

import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

import org.apache.commons.net.ftp.FTP; 
import org.apache.commons.net.ftp.FTPClient; 

/** 
* A program that demonstrates how to upload files from local computer 
* to a remote FTP server using Apache Commons Net API. 
* @author www.codejava.net 
*/ 
public class FTPUploadFileDemo { 

    public static void main(String[] args) { 
     String server = "www.myserver.com"; 
     int port = 21; 
     String user = "user"; 
     String pass = "pass"; 

     FTPClient ftpClient = new FTPClient(); 
     try { 

      ftpClient.connect(server, port); 
      ftpClient.login(user, pass); 
      ftpClient.enterLocalPassiveMode(); 

      ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 

      // APPROACH #1: uploads first file using an InputStream 
      File firstLocalFile = new File("D:/Test/Projects.zip"); 

      String firstRemoteFile = "Projects.zip"; 
      InputStream inputStream = new FileInputStream(firstLocalFile); 

      System.out.println("Start uploading first file"); 
      boolean done = ftpClient.storeFile(firstRemoteFile, inputStream); 
      inputStream.close(); 
      if (done) { 
       System.out.println("The first file is uploaded successfully."); 
      } 

      // APPROACH #2: uploads second file using an OutputStream 
      File secondLocalFile = new File("E:/Test/Report.doc"); 
      String secondRemoteFile = "test/Report.doc"; 
      inputStream = new FileInputStream(secondLocalFile); 

      System.out.println("Start uploading second file"); 
      OutputStream outputStream = ftpClient.storeFileStream(secondRemoteFile); 
      byte[] bytesIn = new byte[4096]; 
      int read = 0; 

      while ((read = inputStream.read(bytesIn)) != -1) { 
       outputStream.write(bytesIn, 0, read); 
      } 
      inputStream.close(); 
      outputStream.close(); 

      boolean completed = ftpClient.completePendingCommand(); 
      if (completed) { 
       System.out.println("The second file is uploaded successfully."); 
      } 

     } catch (IOException ex) { 
      System.out.println("Error: " + ex.getMessage()); 
      ex.printStackTrace(); 
     } finally { 
      try { 
       if (ftpClient.isConnected()) { 
        ftpClient.logout(); 
        ftpClient.disconnect(); 
       } 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 
    } 

} 
Powiązane problemy