六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 51|回复: 0

每天进步一点一点 ftp上传下载

[复制链接]

升级  30%

3

主题

3

主题

3

主题

童生

Rank: 1

积分
15
 楼主| 发表于 2013-1-15 03:01:02 | 显示全部楼层 |阅读模式
http://www.iteye.com/topic/206185package com.northking.dataManager.util;   import sun.net.ftp.*;   import sun.net.*;   import java.io.*;     /**   * 使用sun.net.ftp工具包进行ftp上传下载   * @author maochangming   * @date 2008-6-20  13:09:29   * @description:   */  public class FtpTool {       String ip;       int port;       String user;       String pwd;       String remotePath;       String localPath;       FtpClient ftpClient;         /**       * 连接ftp服务器       * @param ip       * @param port       * @param user       * @param pwd       * @return       * @throws Exception       */      public boolean connectServer(String ip, int port, String user, String pwd)           throws Exception {           boolean isSuccess = false;           try {               ftpClient = new FtpClient();               ftpClient.openServer(ip, port);               ftpClient.login(user, pwd);               isSuccess = true;           } catch (Exception ex) {               throw new Exception("Connect ftp server error:" + ex.getMessage());           }           return isSuccess;       }         /**       * 下载文件       * @param remotePath       * @param localPath       * @param filename       * @throws Exception       */      public void downloadFile(String remotePath,String localPath, String filename) throws Exception {           try {               if (connectServer(getIp(), getPort(), getUser(), getPwd())) {                   if (remotePath.length() != 0)                       ftpClient.cd(remotePath);                   ftpClient.binary();                   TelnetInputStream is = ftpClient.get(filename);                   File file_out = new File(localPath + File.separator + filename);                   FileOutputStream os = new FileOutputStream(file_out);                   byte[] bytes = new byte[1024];                   int c;                   while ((c = is.read(bytes)) != -1) {                       os.write(bytes, 0, c);                   }                   is.close();                   os.close();                   ftpClient.closeServer();               }           } catch (Exception ex) {               throw new Exception("ftp download file error:" + ex.getMessage());           }       }         /**       * 上传文件       * @param remotePath       * @param localPath       * @param filename       * @throws Exception       */      public void uploadFile(String remotePath,String localPath, String filename) throws Exception {           try {               if (connectServer(getIp(), getPort(), getUser(), getPwd())) {                   if (remotePath.length() != 0)                       ftpClient.cd(remotePath);                   ftpClient.binary();                   TelnetOutputStream os = ftpClient.put(filename);                   File file_in = new File(localPath + File.separator + filename);                   FileInputStream is = new FileInputStream(file_in);                   byte[] bytes = new byte[1024];                   int c;                   while ((c = is.read(bytes)) != -1) {                       os.write(bytes, 0, c);                   }                   is.close();                   os.close();                   ftpClient.closeServer();               }           } catch (Exception ex) {               throw new Exception("ftp upload file error:" + ex.getMessage());           }       }         /**       * @return       */      public String getIp() {           return ip;       }         /**       * @return       */      public int getPort() {           return port;       }         /**       * @return       */      public String getPwd() {           return pwd;       }         /**       * @return       */      public String getUser() {           return user;       }         /**       * @param string       */      public void setIp(String string) {           ip = string;       }         /**       * @param i       */      public void setPort(int i) {           port = i;       }         /**       * @param string       */      public void setPwd(String string) {           pwd = string;       }         /**       * @param string       */      public void setUser(String string) {           user = string;       }         /**       * @return       */      public FtpClient getFtpClient() {           return ftpClient;       }         /**       * @param client       */      public void setFtpClient(FtpClient client) {           ftpClient = client;       }         /**       * @return       */      public String getRemotePath() {           return remotePath;       }         /**       * @param string       */      public void setRemotePath(String string) {           remotePath = string;       }         /**       * @return       */      public String getLocalPath() {           return localPath;       }         /**       * @param string       */      public void setLocalPath(String string) {           localPath = string;       }     }   
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表