package cn.jsfund.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
public class FtpHelper_bak {
/**
* @param args
*/
public static void main(String[] args) {
FtpHelper_bak help = new FtpHelper_bak();
//help.downloadFile();
help.uploadFile();
}
// 列举文件
public void listFile()
{
String server = "10.10.10.90"; // 输入的FTP服务器的IP地址
String user = "root"; // 登录FTP服务器的用户名
String password = "jsweb123"; // 登录FTP服务器的用户名的口令
String path = "/"; // FTP服务器上的路径
try {
FtpClient ftpClient = new FtpClient(); // 创建FtpClient对象
ftpClient.openServer(server); // 连接FTP服务器
ftpClient.login(user, password); // 登录FTP服务器
if (path.length() != 0)
ftpClient.cd(path);
TelnetInputStream is = ftpClient.list();
int c;
while ((c = is.read()) != -1) {
System.out.print((char) c);
}
is.close();
ftpClient.closeServer();// 退出FTP服务器
} catch (IOException ex) {
ex.printStackTrace();
}
}
// 文件下载
void downloadFile() {
String server = "10.10.10.90"; // 输入的FTP服务器的IP地址
String user = "root"; // 登录FTP服务器的用户名
String password = "jsweb123"; // 登录FTP服务器的用户名的口令
String path = "/"; // FTP服务器上的路径
String filename_in = "a.txt";
String filename_out = "d:\\1.txt";
try {
FtpClient ftpClient = new FtpClient();
ftpClient.openServer(server);
ftpClient.login(user, password);
if (path.length() != 0)
ftpClient.cd(path);
ftpClient.binary();
TelnetInputStream is = ftpClient.get(filename_in);
File file_out = new File(filename_out);
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 (IOException ex) {
ex.printStackTrace();
}
}
// 文件上传
void uploadFile() {
String server = "10.10.10.90"; // 输入的FTP服务器的IP地址
String user = "root"; // 登录FTP服务器的用户名
String password = "jsweb123"; // 登录FTP服务器的用户名的口令
String path = "/home"; // FTP服务器上的路径
String filename_in = "d:\\1.txt";
String filename_out = "1.txt";
try {
FtpClient ftpClient = new FtpClient();
ftpClient.openServer(server);
ftpClient.login(user, password);
if (path.length() != 0)
ftpClient.cd(path);
ftpClient.binary();
TelnetOutputStream os = ftpClient.put(filename_out);
File file_in = new File(filename_in);
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 (IOException ex) {
ex.printStackTrace();
}
}
}
- 1
- 2
- 3
前往页