2011-06-29 43 views
23

Tôi đã tạo một hàm để tải xuống tệp từ máy chủ FTP mà tôi có quyền truy cập. Làm cách nào để tải tệp lên máy chủ FTP?Làm cách nào để tải tệp lên máy chủ FTP?

Dưới đây là phương pháp download_files i sử dụng:

public static void download_files(String un, String pw, String ip, String dir, String fn, String fp){ 

    URLConnection con; 
    BufferedInputStream in = null; 
    FileOutputStream out = null; 

    try{ 
     URL url = new URL("ftp://"+un+":"+pw+"@"+ip+"/"+dir+"/"+fn+";type=i"); 
     con = url.openConnection(); 
     in = new BufferedInputStream(con.getInputStream()); 
     out = new FileOutputStream(fp+fn); 

     int i = 0; 
     byte[] bytesIn = new byte[1024]; 

     while ((i = in.read(bytesIn)) >= 0) { 
      out.write(bytesIn, 0, i); 
     } 

    }catch(Exception e){ 
     System.out.print(e); 
     e.printStackTrace(); 
     System.out.println("Error while FTP'ing "+fn); 
    }finally{ 
     try{ 
      out.close(); 
      in.close(); 
     }catch(IOException e){ 
      e.printStackTrace(); 
      System.out.println("Error while closing FTP connection"); 
     } 
    } 
} 
+0

bản sao có thể có của [cách triển khai FTP bằng java?] (Http://stackoverflow.com/questions/4720539/how-to-implement-ftp-using-java) – CoolBeans

+0

Tôi có một nghi ngờ, là đoạn mã trên (Tải xuống tệp từ ftp) sẽ hoạt động trong android? –

Trả lời

43

Sử dụng lớp FTPClient từ thư viện Apache Commons Net.

Đây là một đoạn với một ví dụ:

FTPClient client = new FTPClient(); 
FileInputStream fis = null; 

try { 
    client.connect("ftp.domain.com"); 
    client.login("admin", "secret"); 

    // 
    // Create an InputStream of the file to be uploaded 
    // 
    String filename = "Touch.dat"; 
    fis = new FileInputStream(filename); 

    // 
    // Store file to server 
    // 
    client.storeFile(filename, fis); 
    client.logout(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} finally { 
    try { 
     if (fis != null) { 
      fis.close(); 
     } 
     client.disconnect(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

Snippet lấy từ http://www.kodejava.org/examples/356.html

0

Hãy xem apache-commons-net họ có một số công cụ FTP có thể giúp bạn ra ngoài!

3

Tôi đã sử dụng các gói EDT FTP, thư viện GPL miễn phí cho FTP trong Java: http://www.enterprisedt.com/products/edtftpj/overview.html

đây là mẫu mã, từ lớp Demo.java mà họ cung cấp:

ftp = new FTPClient(); 
ftp.setRemoteHost("hostname"); 

// connect 
ftp.connect(); 

// login 
ftp.login("user", "password"); 

// set up passive ASCII transfers 
ftp.setConnectMode(FTPConnectMode.PASV); 
ftp.setType(FTPTransferType.ASCII); 

// get directory and print it to console    
String[] files = ftp.dir(".", true); 
for (int i = 0; i < files.length; i++) 
    log.debug(files[i]); 

// copy file to server 
ftp.put("test.txt", "test.txt"); 

// copy file from server 
ftp.get("test.txt" + ".copy", "test.txt"); 

// delete file from server 
ftp.delete("test.txt"); 

// Shut down client     
ftp.quit(); 
Các vấn đề liên quan