import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
/*
* @author *
*/
class FTPUtil {
/**
* 连接到FTP
*
* @param IP
* 地址
* @param userName
* 用户名
* @param passWord
* 密码
* @return FtpClient
* @throws Exception
*/
public FtpClient Connection(String IP, String userName, String passWord)
throws Exception {
FtpClient fc = new FtpClient();
fc.openServer(IP);
fc.login(userName, passWord);
fc.binary();
return fc;
}
/**
* 断开连接
*
* @param fc
* FTP连接对象
* @throws IOException
*/
public void Close(FtpClient fc) throws IOException {
fc.closeServer();
}
/**
* 获取当前工作作目录
*
* @param fc
* FTP连接对象
* @throws IOException
*/
public String getPwd(FtpClient fc) throws IOException {
return fc.pwd();
}
/**
* 修改工作目录
*
* @param fc
* @param path
* 子目录
* @throws Exception
*/
public void ftpCD(FtpClient fc, String path) throws Exception {
fc.cd(path);
}
/**
* 下载文件
*
* @param fc
* FTP连接对象
* @param filename
* 下载的文件名称
* @return InputStream
* @throws Exception
*/
public InputStream downLoad(FtpClient fc, String filename) throws Exception {
fc.binary();
return fc.get(filename);
}
/**
* 上传文件
*
* @param fc
* FTP连接对象
* @param filename
* 上传的文件名称
* @throws Exception
*/
public void upLoad(FtpClient fc, String filename, String Url) throws Exception {
TelnetOutputStream os = fc.put(filename);
File file = new File(Url);
FileInputStream is = new FileInputStream(file);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
is.close();
os.close();
}
/**
* 删除指定文件
*
* @param fc
* @param filename
* @throws Exception
*/
public void Delete(FtpClient fc, String filename) throws Exception {
fc.cd(getPwd(fc));
fc.readServerResponse();
}
}