栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

轻量级分布式文件系统FastDFS(三)之SpringBoot访问

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

轻量级分布式文件系统FastDFS(三)之SpringBoot访问

轻量级分布式文件系统FastDFS(三)之SpringBoot访问 SpringBoot访问FastDFS 1. 引入Maven依赖
	
        org.springframework.boot
        spring-boot-starter-parent
        2.0.5.RELEASE
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            com.github.tobato
            fastdfs-client
            1.26.1-RELEASE
        
    
2.配置文件application.yml
fdfs.connect-timeout=600
fdfs.tracker-list=192.168.59.10:22122,192.168.59.11:22122,192.168.59.12:22122
server.port=8899
3. 编写上传下载文件的Service接口及实现类
package com.snf.service;

public interface FastDFSClientService {

    String uploadFile(byte[] bytes, long fileSize, String extension);

    byte[] downloadFile(String fileUrl);
}
package com.snf.service.impl;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.snf.service.FastDFSClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;

@Service
public class FastDFSClientServiceImpl implements FastDFSClientService {

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    
    public String uploadFile(byte[] bytes, long fileSize, String extension) {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        StorePath storePath = fastFileStorageClient.uploadFile(byteArrayInputStream, fileSize, extension, null);
        System.out.println(storePath.getGroup()
                .concat(":").concat(storePath.getPath())
                .concat(":").concat(storePath.getFullPath()));
        return storePath.getFullPath();
    }

    
    public byte[] downloadFile(String fileUrl) {
        String group = fileUrl.substring(0, fileUrl.indexOf("/"));
        String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
        DownloadByteArray downloadByteArray = new DownloadByteArray();
        byte[] bytes = fastFileStorageClient.downloadFile(group, path, downloadByteArray);
        return bytes;
    }
}
4. 控制器类
package com.snf.controller;

import com.snf.service.FastDFSClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Objects;

@RequestMapping("/fastdfs")
@RestController
public class FastDFSController {

    @Autowired
    private FastDFSClientService fastDFSClientService;

    //fastdfs上传接口
    @RequestMapping("/upload")
    public String uploadFile(MultipartFile file) throws IOException {
        byte[] bytes = file.getBytes();
        long size = file.getSize();
        String originalFilename = file.getOriginalFilename();
        String extension = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        String name = file.getName();
        System.out.println(originalFilename
                .concat(":").concat(name)
                .concat(":").concat(String.valueOf(size))
                .concat(":").concat(extension)
                .concat(":").concat(String.valueOf(bytes.length)));
        return fastDFSClientService.uploadFile(bytes, size, extension);
    }

    //fastdfs下载接口
    @RequestMapping("/download")
    public void downloadFile(String fileUrl, HttpServletResponse response) {
        byte[] bytes = fastDFSClientService.downloadFile(fileUrl);
        ServletOutputStream outputStream = null;
        try {
            response.setHeader("Content-disposition", "attachment;filename=".concat(URLEncoder.encode(fileUrl, "UTF-8")));
            response.setCharacterEncoding("UTF-8");
            outputStream = response.getOutputStream();
            outputStream.write(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (Objects.nonNull(outputStream)) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
5. 编写启动类
package com.snf;

import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;

//解决jmx重复注册bean的问题
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
@Import(FdfsClientConfig.class)
@SpringBootApplication
public class FastDFSApplication {

    public static void main(String[] args) {
        SpringApplication.run(FastDFSApplication.class, args);
    }
}

6.使用postman进行测试



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/962358.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号