docker 搭建fastdfs 并实现上传下载

 0.先下载镜像

docker 搭建fastdfs 并实现上传下载 

1.安装跟踪器 跟踪器默认端口为22122

docker run  -d --name trakcer -v /fastdfs/tracker_data:/fastdfs/tracker/data --net=host season/fastdfs tracker

  2.安装存储器 192.168.160.128 是你的主机ip

docker run -d --name storage -v /fastdfs/storage_data:/fastdfs/storage/data -v /fastdfs/store_path:/fastdfs/store_path --net=host -e TRACKER_SERVER:192.168.160.128:22122 season/fastdfs storage

  3.查看是否启动成功

   docker ps

docker 搭建fastdfs 并实现上传下载

4.修改存储器中对应的跟踪器的ip 地址

 docker exec -it storage bash

 根据上述命令可以看到,配置文件的位置:

docker 搭建fastdfs 并实现上传下载

你可以在容器中修改, 也可以复制出来,然后改完之后复制进去,我用的是复制的形式

  在宿主机上执行:

docker cp storage:/fdfs_conf/storage.conf /fastdfs/storage.conf 

   docker 搭建fastdfs 并实现上传下载

  然后再把该配置文件复制回去

docker cp /fastdfs/storage.conf storage:/fdfs_conf/storage.conf 

  重启存储器

docker restart storage

 5.java中进行测试

 在pom 中添加依赖

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.41</version>
        </dependency>

配置文件

fdfs:
  so-timeout: 1501
  connect-timeout: 601
  thumb-image: # 缩略图
    width: 60
    height: 60
  tracker-list: 192.168.160.128:22122

添加service

@Service
public class FastDFSClientService {
    @Autowired
    private FastFileStorageClient fileStorageClient;

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

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

 }

 controller

@RestController
@RequestMapping("/fdfs")
public class FastDFSController {
    @Autowired
    private FastDFSClientService dfsClientService;

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public JsonResult upload(MultipartFile file) throws IOException {
        String url = dfsClientService.uploadFile(file);
        return JsonResult.success(url);
    }

    @RequestMapping("/download")
    public void download(String fileUrl, HttpServletResponse response) throws IOException {
        byte[] data = dfsClientService.download(fileUrl);
        String fileName = fileUrl.substring(fileUrl.lastIndexOf("/")+1);
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-type", "application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        response.setContentType("application/octet-stream");
        ServletOutputStream outputStream = response.getOutputStream();
        IOUtils.write(data, outputStream);
    }

}

 

  JsonResult 是工具类,返回前端的统一格式而已,可以自行封装

     至此,docker搭建的fastdfs就可以实现上传下载了!

 

 

 

 

 

 

 

 

 

 

 

 

docker 搭建fastdfs 并实现上传下载docker 搭建fastdfs 并实现上传下载 csdnmuyi 发布了52 篇原创文章 · 获赞 6 · 访问量 6万+ 私信 关注
上一篇:FastDFS--storage文件上传和下载问题


下一篇:nginx添加fastdfs-nginx-module模块(非覆盖安装)(on ubuntu18.04)