随笔-200  评论-148  文章-15  trackbacks-0

以下所用到的包可以在http://download.csdn.net/sort/tag/edtftpj.zip下载。

import java.io.IOException;
import java.util.Properties;
import com.enterprisedt.net.ftp.FTPClient;
import com.enterprisedt.net.ftp.FTPConnectMode;
import com.enterprisedt.net.ftp.FTPException;
import com.enterprisedt.net.ftp.FTPMessageCollector;
import com.enterprisedt.net.ftp.FTPTransferType;

public class TestFtp {

 /**
  * @param args
  */
 public static void main(String[] args) {
  
   String FTPSERVER = "192.168.11.11";
    
      /**ftp server 端口,ftp默认的端口都是21*/
      int FTPPORT = 21;
    
      /**ftp 用户名 */
      String FTPUSER ="userName";
    
      /**ftp 用户密码 */
      String FTPPSWD ="******";
     
      /**文件存放的路径*/
      String filePath="D:/projects/TestProject/";
    
      /**文件名*/
      String fileName ="temp.txt";

    try { 
    FTPClient ftp= new FTPClient();
    ftp.setRemoteHost(FTPSERVER);
    ftp.setRemotePort(FTPPORT);
    ftp.setControlEncoding("GB2312");
    FTPMessageCollector listener = new FTPMessageCollector();
             ftp.setMessageListener(listener);  
             ftp.connect();
             ftp.login(FTPUSER,FTPPSWD);
             /**设置连接模式 */
             ftp.setConnectMode(FTPConnectMode.ACTIVE);
             /**设置传送模式  为二进制模式*/
             ftp.setType(FTPTransferType.BINARY);
             /**put(源,目的)这里的路径可以用相对路径或绝对路径*/
             ftp.put(filePath+fileName,fileName);

   /**可以用以下语句求出当前路径地址*/

   Properties p = System.getProperties();
             System.out.println("path: "+p.getProperty("user.dir"));
                ftp.quit();
    } catch (NumberFormatException e) {
     e.printStackTrace();
    } catch (IOException e) { 
     e.printStackTrace();
    } catch (FTPException e) {
     e.printStackTrace();
    }

 }

}

以上代码在JDK1.42中测试通过。

注意:这三句代码

 FTPClient ftp= new FTPClient();
    ftp.setRemoteHost(FTPSERVER);
    ftp.setRemotePort(FTPPORT);

FTPClient ftp= new FTPClient(FTPSERVER,FTPPORT);意思一样

但最好不要用后一句,是不被推荐使用的。我使用上面一句就报了

The FTP client has already been connected to the server. 

The requested action must be performed before a connection is established.

异常。

下载做法差不多了。自己摸索下吧~~
posted on 2008-05-05 11:04 无声 阅读(6564) 评论(0)  编辑  收藏 所属分类: 职场生活

只有注册用户登录后才能发表评论。


网站导航: