`
ww5025419
  • 浏览: 27026 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

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

阅读更多
http://www.iteye.com/topic/206185
package 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;   
    }   
  
}  

 

分享到:
评论

相关推荐

    flex web ftp上传. 点对点方式 . 支持多文件上传 现打包发布

    从标签“源码”和“工具”中可以得知,该项目不仅是一个实用的FTP上传工具,而且提供了完整的源代码供用户下载。这意味着开发者不仅可以直接使用这个工具,还可以深入研究其工作原理,根据自己的需求进行定制和优化...

    黑链专用ftp挂链工具

    这种工具运用了FTP(File Transfer Protocol)协议,它是一种网络上常用的文件传输协议,能够允许用户远程管理服务器上的文件,包括上传、下载、删除等操作。FTP挂链工具正是利用这一点,为黑链操作者提供了一个便捷...

    (带手机版数据同步)高端大气网络建站公司网站源码 网络工作室html5网站模板_网站整站打包下载.zip

    接着,通过FTP工具或者网页服务器的文件管理功能,将这些文件上传到自己的服务器空间。完成文件上传之后,用户可以根据模板提供的安装说明或文档进行基本的网站配置和内容填充,最终完成整个网站的搭建。 这款带有...

    ServuFTP服务器教程PPT教案.pptx

    这样,他们可以方便地搭建一个既安全又易于管理的FTP服务器,用于上传、下载和共享教学资料,极大地提高了教学和学习的效率。 在教育环境中,尤其是对于那些需要大量文件交换的学科组,ServuFTP服务器成为一个不可...

    试谈计算机软件Java编程特点及其技术.pdf

    Java提供了丰富的API来处理网络编程,如套接字编程和URL编程,使得Java应用程序可以轻松处理HTTP请求、FTP上传下载等网络操作。Java的网络编程能力不仅限于底层的网络通信,还支持构建完整的网络应用程序和网络服务...

    计算机网络基础网络课程设计

    教师办公中心则旨在为教师提供稳定可靠的网络支持,包括但不限于课程资料的下载、上传以及教学相关活动的网络需求。多媒体网络教室则是将计算机网络和多媒体教学相结合,通过网络传输高质量的音频和视频内容,丰富...

Global site tag (gtag.js) - Google Analytics