Java实现FTP文件上传、下载和删除的基本操作

1、FTP

FTP是File Transfer Protocol(文件传输协议),顾名思义,就是专门用来传输文件的协议。FTP服务器(File Transfer Protocol Server)是在互联网上提供文件存储和访问服务的计算机,它们依照FTP协议提供服务。

2、实例

(1)目录结构


(2)pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>cn.hwd</groupId>
  <artifactId>ftp</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>ftp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
  	<dependency>
	    <groupId>commons-net</groupId>
	    <artifactId>commons-net</artifactId>
	    <version>1.4.1</version>
	</dependency>
  	<dependency>
  		<groupId>junit</groupId>
  		<artifactId>junit</artifactId>
  		<version>4.8.1</version>
  	</dependency>
  </dependencies>
</project>

(3)FtpUtil.java

package cn.hwd.ftp.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

public class FtpUtil {
	
	private String hostname = "192.168.17.128";
	private int port = 21;
	private String username = "huangwending";
	private String password = "123456";
	private FTPClient ftpClient = null;
	private InputStream inputStream = null;
	private OutputStream outputStream = null;

	public void uploadFile(String fileName, String originFilename) {
		ftpClient = new FTPClient();
		// 设置utf-8编码
        ftpClient.setControlEncoding("utf-8");
        try {
        	// 连接ftp服务器
			ftpClient.connect(hostname, port);
			// 登录ftp服务器
			ftpClient.login(username, password);
			inputStream = new FileInputStream(new File(originFilename));
			// 设置二进制传输模式
            ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
            ftpClient.storeFile(fileName, inputStream);
            inputStream.close();
            ftpClient.logout();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
            if (ftpClient.isConnected()) { 
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } 
            if (null != inputStream) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } 
            } 
        }
    }
	
	public void downloadFile(String fileName, String localPath) { 
		ftpClient = new FTPClient();
	    ftpClient.setControlEncoding("utf-8");
        try {
        	ftpClient.connect(hostname, port);
			ftpClient.login(username, password);
        	FTPFile[] ftpFiles = ftpClient.listFiles();
            for (FTPFile ftpFile : ftpFiles) {
            	if (fileName.equalsIgnoreCase(ftpFile.getName())) {
                	File file = new File(localPath + "/" + ftpFile.getName());
                    outputStream = new FileOutputStream(file);
                    ftpClient.retrieveFile(ftpFile.getName(), outputStream);
                    outputStream.close();
            	}
            }
            ftpClient.logout();
        } catch (Exception e) {
        	e.printStackTrace();
        } finally {
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != outputStream) {
                try {
                	outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
	}
	
	public void deleteFile(String fileName) {
		ftpClient = new FTPClient();
	    ftpClient.setControlEncoding("utf-8");
        try {
        	ftpClient.connect(hostname, port);
			ftpClient.login(username, password);
			ftpClient.dele(fileName);
            ftpClient.logout();
        } catch (Exception e) {
        	e.printStackTrace();
        } finally {
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
     }
	
}

(4)FtpMain.java

package cn.hwd.ftp.main;

import cn.hwd.ftp.util.FtpUtil;

public class FtpMain {

	public static void main(String[] args) {
		FtpUtil ftpUtil = new FtpUtil();
		ftpUtil.uploadFile("a.txt", "C://Users//huangwending//Desktop//a.txt");
//		ftpUtil.downloadFile("a.txt", "C://Users//huangwending//Desktop//");
//		ftpUtil.deleteFile("a.txt");
	}

}
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值