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;
}
}
分享到:
相关推荐
从标签“源码”和“工具”中可以得知,该项目不仅是一个实用的FTP上传工具,而且提供了完整的源代码供用户下载。这意味着开发者不仅可以直接使用这个工具,还可以深入研究其工作原理,根据自己的需求进行定制和优化...
这种工具运用了FTP(File Transfer Protocol)协议,它是一种网络上常用的文件传输协议,能够允许用户远程管理服务器上的文件,包括上传、下载、删除等操作。FTP挂链工具正是利用这一点,为黑链操作者提供了一个便捷...
接着,通过FTP工具或者网页服务器的文件管理功能,将这些文件上传到自己的服务器空间。完成文件上传之后,用户可以根据模板提供的安装说明或文档进行基本的网站配置和内容填充,最终完成整个网站的搭建。 这款带有...
这样,他们可以方便地搭建一个既安全又易于管理的FTP服务器,用于上传、下载和共享教学资料,极大地提高了教学和学习的效率。 在教育环境中,尤其是对于那些需要大量文件交换的学科组,ServuFTP服务器成为一个不可...
Java提供了丰富的API来处理网络编程,如套接字编程和URL编程,使得Java应用程序可以轻松处理HTTP请求、FTP上传下载等网络操作。Java的网络编程能力不仅限于底层的网络通信,还支持构建完整的网络应用程序和网络服务...
教师办公中心则旨在为教师提供稳定可靠的网络支持,包括但不限于课程资料的下载、上传以及教学相关活动的网络需求。多媒体网络教室则是将计算机网络和多媒体教学相结合,通过网络传输高质量的音频和视频内容,丰富...