2011-11-16 62 views
13

Hãy tìm hiểu điều này. Mã chạy đúng mà không có ngoại lệ.FtpClient storeFile luôn trả về False

 FTPClient ftp = new FTPClient(); 
     ftp.connect(server); 
     if(!ftp.login(username, password)) 
     { 
      ftp.logout(); 
      return false; 
     } 
     int reply = ftp.getReplyCode(); 
     if (!FTPReply.isPositiveCompletion(reply)) 
     { 
      ftp.disconnect(); 
      return false; 
     } 
     InputStream in = new FileInputStream(localfile); 
     ftp.setFileType(ftp.BINARY_FILE_TYPE, ftp.BINARY_FILE_TYPE); 
     ftp.setFileTransferMode(ftp.BINARY_FILE_TYPE); 
     Store = ftp.storeFile(destinationfile, in); 
     in.close(); 
     ftp.logout(); 
     ftp.disconnect(); 
    } 
    catch (Exception ex) 
    { 
     ex.printStackTrace(); 
     return false; 
    } 
    return Store; 

Buttttttttt

lệnh return luôn trả về false và các tập tin không được tải lên trên máy chủ. Một số xin vui lòng giúp đỡ về điều này.

Để biết thông tin của bạn, 1) tôi đang ở trong mạng văn phòng. ---> chúng ta có cần thêm bất kỳ proxy nào không?


  File file = new File("C:\\Users\\sg0214273\\Desktop\\seagate\\seagate.txt"); 
      FileInputStream input = new FileInputStream(file); 
      client.setFileType(FTP.BINARY_FILE_TYPE); 
      if (!client.storeFile(file.getName(), input)) { 
      System.out.println("upload failed!"); 
      } 
      reply = client.getReplyCode(); 

      if(!FTPReply.isPositiveCompletion(reply)) { 
       System.out.println("upload failed!"); 
      } 



     Login success... 
     230 User ******** logged in. 
     upload failed!-----> is form boolean return value of storefile 
     upload failed!---------> is from replycode... 
     Logout from FTP server... 

hãy giúp đỡ

+0

ftp.getReplyCode() trả về gì? Sau ftp.storeFile? –

+0

@Gray no.Nhìn vào câu đầu tiên –

Trả lời

17

Các thông báo lỗi chính xác có thể được tìm thấy bằng cách gọi FtpClient#getReplyCode(). Từ trang đó (nhấn mạnh của tôi):

Ngay sau khi kết nối là thời gian thực duy nhất bạn cần kiểm tra mã trả lời (vì kết nối là loại void). Quy ước cho tất cả các phương thức lệnh FTP trong FTPClient là chúng có thể trả về giá trị boolean hoặc một số giá trị khác. Các phương thức boolean trả về đúng trên một câu trả lời hoàn thành thành công từ máy chủ FTP và sai trên một câu trả lời dẫn đến lỗi hoặc điều kiện lỗi. Các phương thức trả về một giá trị khác với giá trị boolean trả về giá trị có chứa dữ liệu cấp cao hơn được tạo bởi lệnh FTP hoặc null nếu trả lời dẫn đến lỗi hoặc lỗi. Nếu bạn muốn truy cập mã trả lời FTP chính xác gây ra thành công hay thất bại, bạn phải gọi getReplyCode sau khi thành công hay thất bại.

Để xem những gì một mã trở lại có nghĩa là, bạn có thể nhìn thấy Wikipedia: List of FTP server return codes.

+0

Cảm ơn bạn đã trả lời immedialty. Các vấn đề vẫn tồn tại. Tệp tệp = tệp mới ("C: \\ Người dùng \\ sg0214273 \\ Máy tính để bàn \\ seagate \\ seagate.txt"); \t FileInputStream input = new FileInputStream (tệp); \t client.setFileType (FTP.BINARY_FILE_TYPE); \t if (! Client.storeFile (file.getName(), đầu vào)) { \t System.out.println ("tải lên không thành công!"); \t} \t reply = client.getReplyCode(); \t \t if (FTPReply.isPositiveCompletion (reply)!) { \t \t \t System.out.println ("tải lên không thành công!"); \t \t} đầu ra là tải lên không thành công! tải lên không thành công! – RaviKiran

+1

Có, nhưng giá trị * của getReplyCode là gì. Đây là mã trả lời FTP: xem http://en.wikipedia.org/wiki/List_of_FTP_server_return_codes –

+0

giá trị của getReplyCode là 230 sau khi đăng nhập và 550 sau khi lưu trữ ... (immedialtly after) – RaviKiran

5

Chủ đề khá cũ nhưng có lẽ tôi sẽ giúp đỡ bất kỳ người nào khác. Tôi so sánh những gì FileZilla gửi đến máy chủ FTP và chương trình của tôi đã làm. Tôi cần phải dùng ftp.enterLocalPassiveMode() để làm cho nó hoạt động, ftp.pasv() không tốt :)

Và để gỡ lỗi là tốt hơn để sử dụng getReplyString() so với chỉ getReplyCode()

2

Modify bạn mã để chuyển sang chế độ thụ động trước khi bạn truyền tệp với storeFile() như sau:

... 
ftp.setFileTransferMode(ftp.BINARY_FILE_TYPE); 
ftp.enterLocalPassiveMode();//Switch to passive mode 
Store = ftp.storeFile(destinationfile, in); 
in.close(); 
... 

Hy vọng điều đó sẽ hữu ích.

+0

Tôi vừa có một vấn đề bằng cách sử dụng Apache commons FTP là khách hàng kết nối từ một máy tính Windows 7 dev đến một thử nghiệm Windows 7 ftp Server trên IIS 7.5. Các ftp.storefile trả về một ReplyCode của 501 (mà tuyên bố là lỗi cú pháp). Bản sửa lỗi là đặt chế độ thụ động cho cả máy chủ và ứng dụng khách bằng cách sử dụng ftp.enterRemotePassiveMode(); ftp.enterLocalPassiveMode(); – MartinS

1

xin thêm thư viện apache cho mã này này là lớp impoted

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

lớp còn lại nhập khẩu là từ java.io hoặc java.net

public boolean upload(String server,String username,String password,File localfile){ 
     boolean Store=false; 
     try{ 
     FTPClient ftp = new FTPClient(); 
       // ftp.connect(server); 
    /* you can use either code which is written above above or below code as ftp port 20 is used for the data transfer and port 21 is used for command and controlls */ 
      ftp.connect(InetAddress.getByName(server),21); 
    //here 'server' is your domain name of ftp server or url 
       if(!ftp.login(username, password)) 
       { 
        ftp.logout(); 
        return false; 
       } 
      ftp.sendNoOp();//used so server timeout exception will not rise 
       int reply = ftp.getReplyCode(); 
       if (!FTPReply.isPositiveCompletion(reply)) 
       { 
        ftp.disconnect(); 
        return false; 
       } 
       ftp.enterLocalPassiveMode(); /* just include this line here and your code will work fine */ 
       InputStream in = new FileInputStream(localfile); 
       // ftp.setFileType(ftp.BINARY_FILE_TYPE, ftp.BINARY_FILE_TYPE); 
       ftp.setFileType(FTP.BINARY_FILE_TYPE); 
       // ftp.setFileTransferMode(ftp.BINARY_FILE_TYPE); 
       Store = ftp.storeFile(destinationfile, in); 
       in.close(); 
      //ftp.disconnect(); 
    //here logout will close the connection for you 
       ftp.logout(); 

      } 
      catch (Exception ex) 
      { 
       ex.printStackTrace(); 
       return false; 
      } 
     return Store; 
    } 
+0

Cảm ơn Hải quân. ftp.enterLocalPassiveMode(); thực sự hữu ích với tôi. – yaryan997

0

Cố gắng sử dụng ftp.enterLocalPassiveMode(); trước ftp.storeFile (tệp đích, trong);

Các vấn đề liên quan