当前位置:   article > 正文

FastDFS使用教程_tracker-list

tracker-list

FastDFS

下载必要安装包

wget -c "https://github.com/happyfish100/fastdfs/archive/V6.06.tar.gz"
wget -c "https://github.com/happyfish100/libfastcommon/archive/V1.0.43.tar.gz"
wget -c "https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.22.tar.gz"
Nginx

libfastcommon

解压后放到/usr/local ./make.sh && ./make.sh install

fastdfs

解压后放到/usr/local ./make.sh && ./make.sh install

cd /etc/fdfs/
cp storage.conf.sample storage.conf
cp client.conf.sample client.conf
cp tracker.conf.sample tracker.conf
mkdir -p /fastdfs/tracker
mkdir -p /fastdfs/storage
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
tracker配置

vi /etc/fdfs/tracker.conf

base_path = /fastdfs/tracker
  • 1

启动tracker:/etc/init.d/fdfs_trackerd start
停止tracker:/etc/init.d/fdfs_trackerd stop

storage配置

vi /etc/fdfs/storage.conf

base_path = /fastdfs/storage 
store_path0 = /fastdfs/storage
tracker_server = tracker的IP:22122
http.server_port = 80
  • 1
  • 2
  • 3
  • 4

启动storage:/etc/init.d/fdfs_storaged start

client配置(开发不需要)

vi /etc/fdfs/client.conf

base_path = /fastdfs/tracker
tracker_server = tracker的IP:22122
  • 1
  • 2

#设置开机启动:vi /etc/rc.d/rc.local

/etc/init.d/fdfs_trackerd start
/etc/init.d/fdfs_storaged start
  • 1
  • 2

fastdfs-nginx-module 和 Nginx

fastdfs-nginx-module解压后放到/usr/local

nginx添加模块(解压目录,不是nginx主目录)
./configure --prefix=/usr/local/nginx --with-http_stub_status_module

fastdfs-nginx-module配置文件修改并将config文件中的/usr/local替换成/usr:cd /usr/local/fastdfs-nginx-module-1.22/src/ vi config

nginx添加模块:./configure --add-module=/usr/local/fastdfs-nginx-module-1.22/src/

nginx编译安装:make && make install

fastdfs-nginx-module配置文件复制
cp /usr/local/fastdfs-nginx-module-1.22/src/mod_fastdfs.conf /etc/fdfs/
并修改配置文件
vi /etc/fdfs/mod_fastdfs.conf

connect_timeout=10
tracker_server=IP:22122
url_have_group_name=true
store_path0=/fastdfs/storage
  • 1
  • 2
  • 3
  • 4

进入fastdfd的conf目录, 将http.conf,mime.types两个文件拷贝到/etc/fdfs/目录下
cp http.conf mime.types /etc/fdfs/

创建一个软连接,在/fastdfs/storage文件存储目录下创建软连接,将其链接到实际存放数据的目录
ln -s /fastdfs/storage/data/ /fastdfs/storage/data/M00

修改nginx.conf
vi /usr/local/nginx/conf/nginx.conf

server {
    listen       80;
    server_name  IP;
    location ~/group([0-9])/M00 {
            root  /fastdfs/storage/data;
            ngx_fastdfs_module;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

启动nginx
根据路径访问文件

结合SpringBoot

pom
<dependency>
    <groupId>com.github.tobato</groupId>
    <artifactId>fastdfs-client</artifactId>
    <version>1.26.7</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
application
fdfs.so-timeout = 1501
fdfs.connect-timeout = 601
fdfs.thumb-image.width= 150
fdfs.thumb-image.height= 150
fdfs.tracker-list=IP:22122
fdfs.web-server-url=IP:23001/

更改文件大小上限
spring.servlet.multipart.max-file-size=30MB
spring.servlet.multipart.max-request-size=30MB
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
service
@Component
public class FastDFSClient {

    @Autowired
    private FastFileStorageClient storageClient;

    @Autowired
    private FdfsWebServer fdfsWebServer;

    /**
     * 上传文件
     * @param file 文件对象
     * @return 文件访问地址
     * @throws IOException
     */
    public String uploadFile(MultipartFile file) throws IOException {
        StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()),null);
        return fdfsWebServer.getWebServerUrl() + storePath.getFullPath();//完整url
    }

    /**
     * 下载文件
     * @param fileUrl 文件url
     * @return
     */
    public byte[]  download(String fileUrl) {
        String group = fileUrl.substring(0, fileUrl.indexOf("/"));
        String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
        byte[] bytes = storageClient.downloadFile(group, path, new DownloadByteArray());
        return bytes;
    }

    /**
     * 删除文件
     * @param fileUrl 文件访问地址
     * @return
     */
    public void deleteFile(String fileUrl) {
        if (StringUtils.isEmpty(fileUrl)) {
            return;
        }
        try {
            StorePath storePath = StorePath.parseFromUrl(fileUrl);
            storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
        } catch (FdfsUnsupportStorePathException e) {
            logger.warn(e.getMessage());
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
controller
@RestController
public class FastDFSController {

    @Autowired
    private FastDFSClient fastDFSClient;

    @PostMapping("/upload")
    public String uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        byte[] bytes = file.getBytes();
        String originalFileName = file.getOriginalFilename();
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        String fileName = file.getName();
        long fileSize = file.getSize();
        System.out.println(originalFileName + "==========" + fileName + "===========" + fileSize + "===============" + extension + "===========" + bytes.length);
        return fastDFSClient.uploadFile(file);
    }

    @PostMapping("/download")
    public void downloadFile(@RequestParam("fileUrl")String fileUrl, HttpServletResponse response) throws IOException {
        byte[] bytes = fastDFSClient.download(fileUrl);
        // 这里只是为了整合fastdfs,所以写死了文件格式。需要在上传的时候保存文件名。下载的时候使用对应的格式
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("1.jpg", "UTF-8"));
        response.setCharacterEncoding("UTF-8");
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
            outputStream.write(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                outputStream.flush();
                outputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @GetMapping("/deleteFile")
    public String deleteFile(@RequestParam("fileUrl")String fileUrl) {
        fastDFSClient.deleteFile(fileUrl);
        return "success";
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

使用命令上传文件

文件不能擅自修改名称 或 自己直接上传
/usr/bin/fdfs_upload_file /etc/fdfs/client.conf test.jpg

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/169871
推荐阅读
相关标签
  

闽ICP备14008679号