一、删除
@Test
public void test1(){
try {
// FileInputStream in=new FileInputStream(new File("E:/新建文件夹 (2)/timg1.jpg"));
// Map<String, Object> map = uploadFile("192.168.0.105", 21, "lqt1", "lqt", "dsa", "tim.jpg", in);
// System.out.println(map.get("status")+"====>"+map.get("filepath"));
boolean flag = delFile("192.168.0.105", 21, "lqt1", "lqt", "dsa/tim.jpg");
System.out.println(flag);
// download("192.168.0.105", 21, "lqt1", "lqt", "sss.mp3");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Description: 删除FTP服务器上的文件
* @param url FTP服务器hostname
* @param port FTP服务器端口
* @param username FTP登录账号
* @param password FTP登录密码
* @param filePath FTP服务器保存目录
* @return 成功返回true,否则返回false
*/
public static boolean delFile(String url,int port,String username, String password, String filePath){
FTPClient client = new FTPClient();
boolean flag = false;
try {
client.connect(url, port);
client.login(username, password);
int reply = client.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)){
client.disconnect();
System.out.println("failed to connect");
return flag;
}
client.deleteFile(filePath);
client.logout();
flag = true;
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(client.isConnected()){
client.disconnect();
}
} catch (Exception e2) {
}
}
return flag;
}
二、下载
@Test
public void test1(){
try {
// FileInputStream in=new FileInputStream(new File("E:/新建文件夹 (2)/timg1.jpg"));
// Map<String, Object> map = uploadFile("192.168.0.105", 21, "lqt1", "lqt", "dsa", "tim.jpg", in);
// System.out.println(map.get("status")+"====>"+map.get("filepath"));
// boolean flag = delFile("192.168.0.105", 21, "lqt1", "lqt", "dsa/tim.jpg");
// System.out.println(flag);
download("192.168.0.105", 21, "lqt1", "lqt", "sss.mp3");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Description: 删除FTP服务器上的文件
* @param url FTP服务器hostname
* @param port FTP服务器端口
* @param username FTP登录账号
* @param password FTP登录密码
* @param filePath FTP服务器保存目录
*/
public static void download(String url,int port,String username, String password, String filePath) {
FTPClient client = new FTPClient();
FileOutputStream fos = null;
try {
client.connect(url, port);
client.login(username, password);
//想要下载的文件在ftp服务器的路径的路径
String downloadfilePath = "sss.mp3";
//下载后文件的存储路径
fos = new FileOutputStream("e:/sss.mp3");
client.setBufferSize(1024);
//设置文件类型(二进制)
client.setFileType(FTPClient.BINARY_FILE_TYPE);
client.retrieveFile(downloadfilePath, fos);
fos.flush();
fos.close();
System.out.println("下载成功");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}