ftp文件下载与删除


代码只是简单的功能实现,并没有复杂逻辑,开发中如果需要其他复杂业务的话,需要大家改下代码了。

一、删除

	@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(); 
            } 
        } 
    } 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值