FastDFS

$ docker pull delron/fastdfs
$ docker run -d --name fastdfs-tracker \
  --restart=always \
  --network=host \
  -p 22122:22122 \
  delron/fastdfs tracker
mkdir -p /data/fastdfs/storage

docker run -d --name fastdfs-storage \  
  --link fastdfs-tracker:tracker \
  --restart=always \
  --network=host \
  -e TRACKER_SERVER=tracker:22122 \
  -v /data/fastdfs/storage:/var/fdfs \
  -p 23000:23000 -p 8080:8080 -p 8888:8888 \
  delron/fastdfs storage
$ docker exec -it fastdfs-storage /bin/bash
cd /etc/fdfs/
vi storage.conf
firewall-cmd --zone=public --permanent --add-port=8888/tcp
firewall-cmd --zone=public --permanent --add-port=22122/tcp
firewall-cmd --zone=public --permanent --add-port=23000/tcp
# systemctl restart firewalld
$ sudo docker ps
$ docker exec -it fastdfs-storage /bin/bash
cd /var/fdfs/
echo hello testdoc>b.txt
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf b.txt
group1/M00/00/00/rBEABWdHM8iAGXyhAAAAFy0dlFI076.txt

Accessed through a browser

Maven References

<dependency>
    <groupId>com.github.tobato</groupId>
    <artifactId>fastdfs-client</artifactId>
    <version>1.27.2</version>
</dependency>

Specify the FastDFS configuration in the project’s configuration file, application.properties

fdfs.so-timeout=1500
fdfs.connect-timeout=600
#thumbImage param
fdfs.thumb-image.height=150
fdfs.thumb-image.width=150
#TrackerList参数,支持多个
fdfs.tracker-list=192.168.157.129:22122

File upload and download

package com.lm.shop.shopeureka.util;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.IOException;

@Component
public class FastDFSUtil {

    private final Logger logger = LoggerFactory.getLogger(FastDFSUtil.class);
    @Autowired
    private FastFileStorageClient fileStorageClient;
    /**
	* 文件上传
	*
	* @param multipartFile 附件上传
	* @return fastDfs路径
	*/  
	public String uploadFile(MultipartFile multipartFile) throws Exception{
		String originalFilename = multipartFile.getOriginalFilename().
		substring(multipartFile.getOriginalFilename().
		lastIndexOf(".") + 1);
        StorePath storePath = this.fileStorageClient.uploadImageAndCrtThumbImage(
		multipartFile.getInputStream(),
		multipartFile.getSize(),originalFilename , null);
        return storePath.getFullPath() ;
		}
	
    /** 
    * 删除文件
    *   
	* @param filePath 附件路径
	* @return void
	*/
    public void delFile(String filePath) {
	this.fileStorageClient.deleteFile(filePath);
    }

    /**
	* 文件上传
	*
	* @param bytes     文件字节
	* @param fileSize  文件大小
	* @param extension 文件扩展名
	* @return fastDfs路径
	*/    public String uploadFile(byte[] bytes, long fileSize, String extension) {
	ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
	StorePath storePath = fileStorageClient.uploadFile(byteArrayInputStream, fileSize, extension, null);
	logger.info(storePath.getGroup() + "===" + storePath.getPath() + "======" + storePath.getFullPath());
	return storePath.getFullPath();
    }

    /**
	* 下载文件
	*
	* @param fileUrl 文件URL
	* @return 文件字节
	* @throws IOException
	*/
    public byte[] downloadFile(String fileUrl) throws IOException {
	String group = fileUrl.substring(0, fileUrl.indexOf("/"));
	String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
	DownloadByteArray downloadByteArray = new DownloadByteArray();
	byte[] bytes = fileStorageClient.downloadFile(group, path, downloadByteArray);
	return bytes;
    }

}
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus