Springboot 封住ftp 功能模块解析

你猜 阅读:207 2021-03-31 21:06:43 评论:0

整体项目结构:

第一步: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(); 
		} 
		 
		 
	} 
	 
	 
 
} 

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
KIKK导航

KIKK导航

关注我们