Springboot 封装Fastdfs 功能模块

整体项目结构:

第一步: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-fastdfs 项目结构图:

common-fastdfs的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-fastdfs</artifactId>
  <dependencies>
  	<!--fastdfs 客户端  -->
  	<dependency>
    <groupId>com.github.tobato</groupId>
    <artifactId>fastdfs-client</artifactId>
   	<version>1.26.5</version>
	</dependency>
  </dependencies>
</project>

Application.java 代码:

package com.zzg.common;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;

import com.github.tobato.fastdfs.FdfsClientConfig;

@SpringBootApplication
@Import(FdfsClientConfig.class)
public class Application {

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

FastDFSClient.java

package com.zzg.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;

@Component
public class FastDFSClient {
	 	@Autowired
	    private FastFileStorageClient storageClient;
	   
	  
	    // 文件上传
	    public String uploadFile(String path) throws FileNotFoundException{
	    	File file = new File(path);
	    	InputStream input = new FileInputStream(file);
	    	long size = FileUtils.sizeOf(file);
	    	String name = file.getName();
	    	String fileName = name.substring(name.lastIndexOf("/")+1);  
	    	StorePath storePath = storageClient.uploadFile(input,size, FilenameUtils.getExtension(fileName),null);
	    	return storePath.getFullPath();
	    }
	    
	    // 文件删除
	    public boolean deleteFile(String path) {
	    	try{
	    		StorePath storePath = StorePath.parseFromUrl(path);
	    		storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
	    		return true;	    		
	    	} catch(Exception e) {
	    		return false;
	    	}
	    }
	    
	    // 文件下载
	    public boolean downloadFile(String path,String downloadFilePath) throws IOException{
	    	File file = new File(downloadFilePath);
	    	FileOutputStream outputStream = null;
	    	// fastdfs 文件读取
	    	String filepath = path.substring(path.lastIndexOf("group1/")+7);
	    	DownloadByteArray callback = new DownloadByteArray();
            byte[] content = storageClient.downloadFile("group1", filepath,callback);
            // 数据写入指定文件夹中
            outputStream = new FileOutputStream(file);
            outputStream.write(content);
	    	return true;
	    }

}

application.properties(配置文件)

fdfs.soTimeout=1500
fdfs.connectTimeout=600
fdfs.trackerList[0]=192.168.60.178:22122

fastdfs 功能测试:

FastdfsTest.java

package com.zzg.common.fds;

import java.io.FileNotFoundException;
import java.io.IOException;

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 org.springframework.util.StringUtils;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.zzg.common.util.FastDFSClient;

@RunWith(SpringRunner.class)
@SpringBootTest
public class FastdfsTest {
	@Autowired
	private FastDFSClient client;

	@Test
	public void uploadFile() throws FileNotFoundException {
		String filepath = "C:/Users/zzg/Desktop/spring-security.png";
		String result = client.uploadFile(filepath);
		if (!StringUtils.isEmpty(result)) {
			System.out.println("资源路径:" + result);
			System.out.println("文件上传成功");
		} else {
			System.out.println("文件上传失败");
		}
	}

	@Test
	public void deleteFile() {
		String fileUrl = "group1/M00/00/00/wKg8pVyB9E6AQlcJAAMjXuQzK2A959.png";
		if (StringUtils.isEmpty(fileUrl)) {
			return;
		}
		boolean flag = client.deleteFile(fileUrl);
		if(flag){
			System.out.println("文件删除成功");
		} else {
			System.out.println("文件删除失败");
		}
	}
	
	@Test
	public void downloadFile() throws IOException{
		String path = "group1/M00/00/00/wKg8pVyCDsaABTVdAAMjXuQzK2A647.png";
		String downloadFilePath ="C:/Users/zzg/Desktop/test.png";
		boolean flag = client.downloadFile(path, downloadFilePath);
		if(flag){
			System.out.println("文件下载成功");
		} else {
			System.out.println("文件下载失败");
		}
	}

}

猜你喜欢

转载自blog.csdn.net/zhouzhiwengang/article/details/88638705