java ftp文件上传、下载..
2013-08-23 10:35
302 查看
java ftp文件上传、下载..
public class FileFTP {String localfilename;
String remotefilename;
FtpClient ftpClient;
// server:服务器名字
// user:用户名
// password:密码
// path:服务器上的路径
public void connectServer(String ip, int port,String user, String password,String path)
{
try
{
ftpClient = new FtpClient();
ftpClient.openServer(ip,port);
ftpClient.login(user, password);
System.out.println("login success!");
if (path.length() != 0)
ftpClient.cd(path);
ftpClient.binary();
}
catch (IOException ex)
{
System.out.println("not login");
System.out.println(ex);
}
}
public void closeConnect()
{
try
{
ftpClient.closeServer();
System.out.println("disconnect success");
}
catch (IOException ex)
{
System.out.println("not disconnect");
System.out.println(ex);
}
}
public void upload()
{
this.localfilename = "F:\\2009-04-01.txt";
this.remotefilename = "2009-04-01.txt";
try
{
TelnetOutputStream os = ftpClient.put(this.remotefilename);
java.io.File file_in = new java.io.File(this.localfilename);
FileInputStream is = new FileInputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1)
{
os.write(bytes, 0, c);
}
System.out.println("upload success");
is.close();
os.close();
}
catch (IOException ex)
{
System.out.println("not upload");
System.out.println(ex);
}
}
public void upload(String path,String name)
{
try
{
TelnetOutputStream os = ftpClient.put(name);
FileInputStream is = new FileInputStream(new File(path));
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1)
{
os.write(bytes, 0, c);
}
System.out.println("upload success");
is.close();
os.close();
}
catch (IOException ex)
{
System.out.println("not upload");
System.out.println(ex);
}
}
public void download()
{
try
{
TelnetInputStream is = ftpClient.get(this.remotefilename);
java.io.File file_in = new java.io.File(this.localfilename);
FileOutputStream os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1)
{
// System.out.println((char)is.read());
// System.out.println(file_in);
os.write(bytes, 0, c);
}
System.out.println("download success");
os.close();
is.close();
}
catch (IOException ex)
{
System.out.println("not download");
System.out.println(ex);
}
}
public void download(String remotePath,String remoteFile,String localFile)
{
try
{
if (remotePath.length() != 0) ftpClient.cd(remotePath);
TelnetInputStream is = ftpClient.get(remoteFile);
java.io.File file_in = new java.io.File(localFile);
FileOutputStream os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1)
{
// System.out.println((char)is.read());
// System.out.println(file_in);
os.write(bytes, 0, c);
}
System.out.println("download success");
os.close();
is.close();
}
catch (IOException ex)
{
System.out.println("not download");
System.out.println(ex);
}
}
public void download(String remoteFile,String localFile)
{
try
{
TelnetInputStream is = ftpClient.get(remoteFile);
java.io.File file_in = new java.io.File(localFile);
FileOutputStream os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1)
{
// System.out.println((char)is.read());
// System.out.println(file_in);
os.write(bytes, 0, c);
}
System.out.println("download success");
os.close();
is.close();
}
catch (IOException ex)
{
System.out.println("not download");
System.out.println(ex);
}
}
}
相关文章推荐
- JAVA FTP文件上传下载删除
- java实现ftp文件的上传与下载
- Java ftp实现文件的上传和下载ftp,sftp sun.net.ftp.FtpProtocolException:Welcome message: SSH-2.0-OpenSSH_5.1
- Java连接FTP服务器并且实现对其文件的上传和下载
- java基于Apache FTP点断续传的文件上传和下载
- JAVA中使用FTPClient实现文件上传下载
- java FTP上传下载文件
- java通过apache ftp实现ftp上传文件,下载文件
- java 两个通过java代码操作FTP的类,上传下载删除,获取文件
- JAVA连接FTP服务器,并上传/下载文件代码
- 基于apache的commons-net-3.3.jar的 ftp java代码上传下载文件
- oracle 调用Java 实现ftp文件的上传和下载 问题
- java实现ftp文件的上传与下载
- ftp 实现文件的上传下载以及列出文件列表Java代码
- JAVA连接FTP服务器,并上传/下载文件代码
- java实现ftp文件的上传与下载
- Java实现FTP文件上传与下载
- JAVA实现FTP服务器文件的上传,下载,删除功能
- 用Java写个ftp传输类实现文件的上传和下载,用ikvmc转成dll
- Java中实现FTP文件上传下载 涉及中文路径以及中文文件