Springboot 封住ftp 功能模块

整体项目结构:

第一步:BuleSky 的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>com.zzg</groupId>
	<artifactId>BuleSky</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>

	<!--springboot 父类 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.0.RELEASE</version>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<!--springboot 依赖web -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!--springboot 依赖test -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>
		<!--springboot 依赖devtool -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
		<!--lombak 集成 -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.0</version>
		</dependency>
		<!--apache common 工具包 -->
		<dependency>
			<groupId>commons-net</groupId>
			<artifactId>commons-net</artifactId>
			<version>3.6</version>
		</dependency>
		 <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.8.1</version>
        </dependency>
	</dependencies>
	<modules>
		<module>common-ssh</module>
		<module>common-ftp</module>
		<module>common-fastdfs</module>
		<module>common-monitor-windows</module>
		<module>common-monitor_linux</module>
		<module>common-elasticsearch</module>
	</modules>
</project>

common-ftp 项目结构图:

common-ftp的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>
  <parent>
    <groupId>com.zzg</groupId>
    <artifactId>BuleSky</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>common-ftp</artifactId>
</project>

Application.java

package com.zzg.common;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SpringApplication.run(Application.class, args);
	}

}

FTPConfig.java

package com.zzg.common.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import lombok.Data;

@Configuration
@PropertySource(value = "classpath:ftp.properties")
@ConfigurationProperties(prefix = "ftp.remote")
@Data
public class FTPConfig {
	 private String address;
	 private int port;
	 private String username;
	 private String password;
}

FTPConstant.java

package com.zzg.common.constant;

public class FTPConstant {
	
	public static final String ENCODING ="UTF-8";

}

FTPEntity.java

package com.zzg.common.entity;

import lombok.Data;

@Data
public class FTPEntity {
	 private String address;
	 private int port;
	 private String username;
	 private String password;
}

FTPActuator.java

package com.zzg.common.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.stereotype.Component;

import com.zzg.common.constant.FTPConstant;
import com.zzg.common.entity.FTPEntity;

public class FTPActuator {

	private FTPClient client = null;

	private FTPActuator(FTPEntity entity,String path) throws SocketException, IOException {
		if(client == null){
			client = new FTPClient();
		}
		client.setControlEncoding(FTPConstant.ENCODING);
		client.connect(entity.getAddress(), entity.getPort());// 连接FTP服务器
		client.login(entity.getUsername(), entity.getPassword());// 登录
		int reply = client.getReplyCode(); 
		if (!FTPReply.isPositiveCompletion(reply)) {
			 // FTP服务器应答失败处理
			 client.disconnect();
			 client = null;
			 // 终止FTP构造函数的执行
			 return;
        }
		client.setFileType(FTPClient.BINARY_FILE_TYPE);
		client.makeDirectory(path);
		client.changeWorkingDirectory(path);
	}

	// 单列模式
	public static FTPActuator getInstance(FTPEntity entity, String path) throws SocketException, IOException {
		return new FTPActuator(entity,path);
	}
	
	// 获取ftp 对象
	public FTPClient getFTPClient(){
		return client;
	}
	
	// 关闭连接
	public void close() throws IOException{
		// 判断是否连接
		if(client.isConnected()){
			client.disconnect();
			client = null;
		}
	}
	
	// ftp 文件上传
	public boolean uploadFile(InputStream stream, String fileName) throws IOException{
		if(client != null){
			client.storeFile(fileName, stream);
			stream.close();
			client.logout();
			close(); // ftp 关闭
			return true;
		}
		return false;
	}
	
	public boolean downloadFile(String fileName,String localPath) throws IOException{
		if(client != null){
			FTPFile[] fs = client.listFiles();
			for(FTPFile ff:fs){
				if(ff.getName().equals(fileName)){
					File localFile = new File(localPath+"/"+ff.getName());
					// 指定文件不存在就创建
					if(!localFile.exists()){
						localFile.createNewFile();
					}
					OutputStream is = new FileOutputStream(localFile); 
					client.retrieveFile(ff.getName(), is);
					is.close();
					client.logout(); //退出cmd 指令
					close(); //ftp 关闭
					return true;
				}
			}

		}
		return false;
	}

}

ftp.properties(ftp 参数配置)

ftp.remote.address: 192.168.60.176
ftp.remote.port: 21
ftp.remote.username: cent
ftp.remote.password: wz123456

FTPTest.java(代码测试)

package com.zzg.common.ftp;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.SocketException;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.zzg.common.config.FTPConfig;
import com.zzg.common.entity.FTPEntity;
import com.zzg.common.util.FTPActuator;

@RunWith(SpringRunner.class)
@SpringBootTest
public class FTPTest {
	@Autowired
	private FTPConfig config;
	
	@Test
	public void uploadFile(){
		FTPEntity entity = new FTPEntity();
		entity.setAddress(config.getAddress());
		entity.setPassword(config.getPassword());
		entity.setPort(config.getPort());
		entity.setUsername(config.getUsername());
		
		String dir = "/";
		try {
			FTPActuator ftp = FTPActuator.getInstance(entity, dir);
			// 上传文件流
			FileInputStream stream = new FileInputStream(new File("C:\\Users\\zzg\\Desktop\\shell.txt"));
			boolean flag = ftp.uploadFile(stream, "shell.txt");
			if(flag){
				System.out.println("文件上传成功");
			} else {
				System.out.println("文件上传失败");
			}
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	@Test
	public void downloadFile(){
		FTPEntity entity = new FTPEntity();
		entity.setAddress(config.getAddress());
		entity.setPassword(config.getPassword());
		entity.setPort(config.getPort());
		entity.setUsername(config.getUsername());
		
		String dir = "/";
		try {
			FTPActuator ftp = FTPActuator.getInstance(entity, dir);
			// 上传文件流
			boolean flag = ftp.downloadFile("shell.txt", "C:\\Users\\zzg\\Desktop");
			if(flag){
				System.out.println("文件下载成功");
			} else {
				System.out.println("文件下载失败");
			}
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值