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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/vinegar93/article/details/80253462

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");
	}

}
展开阅读全文

没有更多推荐了,返回首页