Fork me on GitHub

linux下纯手动安装fastDFS/nginx,基于springboot实现fastDFS文件上传下载

第一部分linux下实现安装fastDFS以及nginx

安装所需的环境:

yum install perl pcre pcre-devel zlib zlib-devel openssl openssl-devel 

1.需要四个安装包:

fastdfs-5.11.tar.gz

nginx-1.15.2.tar.gz

fastdfs-nginx-module_v1.16.tar.gz

libfastcommon-1.0.35

2.安装创建目录上传所有安装包

mkdir -p /fileservice/fast

3.开始逐个安装配置

  3.1. libfastcommon-1.0.35的安装配置

    3.1.1 解压  tar zxvf  libfastcommon-1.0.35

    3.1.2 进入解压好的目录  cd  libfastcommon-1.0.35

    3.1.3 执行编译安装  ./make.sh   &&  ./make.sh install  

  3.2. fastdfs-5.11的安装配置

    3.2.1 解压  tar zxvf fastdfs-5.11.tar.gz

    3.2.2 进入解压好的目录  cd  fastdfs-5.11

    3.2.3 执行编译安装  ./make.sh   &&  ./make.sh install

    3.2.4 进入   cd /etc/fdfs/  复制配置 如下:      

        cp client.conf.sample    client.conf
       cp storage.conf.sample    storage.conf
       cp storage_ids.conf.sample    storage_ids.conf
       cp tracker.conf.sample    tracker.conf

    3.2.5 修改tracker的存放数据和日志的目录
       mkdir -p  /home/fastdfs/tracker

    3.2.6 配置和启动tracker 
       cd /etc/fdfs/

    3.2.7 .修改文件

      vim tracker.conf

 

       修改 base_path=/home/fastdfs/tracker 存放数据和日志的目录


    3.2.8 配置storage 

       切换目录到  cd /etc/fdfs/

       修改文件 vim storage.conf     

       修改路径 base_path=/home/fastdfs/storage

       store存放文件的位置 store_path0=/home/fastdfs/storage

    3.2.9 创建文件 mkdir -p /home/fastdfs/storage

    3.2.10 运行storaged  service fdfs_storaged  start

    3.2.11 修改client.conf   vim client.conf     

        base_path=/home/fastdfs/storage
       tracker_server=192.168.43.209:22122

    3.2.12 模拟上传: /usr/bin/fdfs_upload_file  /etc/fdfs/client.conf  /home/333.jpg  成功即ok

 

  3.3   fastdfs-nginx-module_v1.16的安装配置

    3.3.1 路径 cd /fileservice/fast

    3.3.2解压 tar zxvf fastdfs-nginx-module_v1.16.tar.gz

    3.3.3 切换目录  cd fastdfs-nginx-module/src

    3.3.4 修改 vim config   替换掉所有local:   %s/local\///g

    3.3.5 拷贝文件  cp mod_fastdfs.conf  /etc/fdfs/

    3.3.6编辑拷贝的配置文件  vim /etc/fdfs/mod_fastdfs.conf

    3.3.7修改如下:

      store_path0=/home/fastdfs/storage

      tracker_server=192.168.43.209:22122

      url_have_group_name = trues

    3.3.8 进入 cd /fileservice/fast/fastdfs-5.11/conf  将俩个文件移入/etc/fdfs/目录:    cp http.conf mime.types /etc/fdfs/

  3.4  nginx的安装配置

    3.4.1 进入压缩包路径 cd /fileservice/fast

    3.4.2 解压   tar zxvf  nginx-1.15.2.tar.gz

    3.4.3 进入解压包:cd nginx-1.15.2

    3.4.4 加入模块命令配置: 

      ./configure --prefix=/opt/ngnix --sbin-path=/usr/bin/nginx --add-module=/fileservice/fast/fastdfs-nginx-module/src

    3.4.5 执行编译安装:make && make install

    3.4.6 修改nginx配置: cd /opt/ngnix/conf      vim nginx.conf

       配置如下:

        location ~/group([0-9]){
        # root html;
        #index index.html index.htm;
        ngx_fastdfs_module;}

    3.4.7 进入下列目录:cd /usr/bin  启动ngnix : ./nginx

至此linux下完美安装好了fastDFS以及nginx!!

 

第二部分 springboot下实现fastDFS文件上传下载

  1.1 pom

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- taobao fastdfs依赖 -->
        <dependency>
            <groupId>com.github.tobato</groupId>
            <artifactId>fastdfs-client</artifactId>
            <version>1.26.5</version>
        </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>

  1.2 yml配置

server:
  port: 8080
fdfs:
  connect-timeout: 600
  so-timeout: 1500
  pool:
    jmx-enabled: false
  thumb-image:
    width: 100
    height: 50
    #跟踪器地址TrackerList参数,支持多个,我这里只有一个,如果有多个在下方加- x.x.x.x:port
  trackerList: 192.xxx.xx.xxx:22122  #你linux下的地址
spring:
  thymeleaf:
    prefix: classpath:/templates/
    suffix: .html

  1.3 工具类实现文件上传,下载,删除

@Component
public class FastDFSClientUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(FastDFSClientUtil.class);
    @Resource
    private FastFileStorageClient storageClient;

    /**
     * 上传文件
     *
     * @param multipartFile
     * @return
     */
    public String upload(MultipartFile multipartFile) throws IOException {
        String originalFilename = multipartFile.getOriginalFilename().substring(
                multipartFile.getOriginalFilename().lastIndexOf(".") + 1
        );
        StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(
                multipartFile.getInputStream(),
                multipartFile.getSize(),
                originalFilename,
                null
        );
        return storePath.getFullPath();

    }
    /**
     * 删除文件
     */
    public void deleteFile(String fileUrl) {
        if (StringUtils.isEmpty(fileUrl)) {
            LOGGER.info("fileUrl == >>文件路径为空...");
            return;
        }
        try {
            StorePath storePath = StorePath.parseFromUrl(fileUrl);
            storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
        } catch (Exception e) {
            LOGGER.info(e.getMessage());
        }
    }

    /**
     * 下载文件
     */
    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 = this.storageClient.downloadFile(group, path, downloadByteArray);
        try {
            
            FileOutputStream fileOutputStream = new FileOutputStream("f:\\111.jpg");
            fileOutputStream.write(bytes);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bytes;
    }

}

1.4 controller层

@Controller
public class FsatFDSController {
    @Autowired
    private FastDFSClientUtil clientUtil;

    /**
     * 文件下载
     * @param fileUrl
     * @return
     */
    @RequestMapping("/download")
    @ResponseBody
    public byte[] downloadFile(String fileUrl){
        return clientUtil.downloadFile("group1/M00/00/00/wKgr0V9ZkH6AIq_JAAAg2jn0LiI783.jpg");
    }


    /**
     * 文件上传
     * @param mf
     * @return
     */
    @RequestMapping("/upload")
    @ResponseBody
    public String upload(MultipartFile mf){
        try {
            String upload = clientUtil.upload(mf);
            return upload;
        } catch (IOException e) {
            e.printStackTrace();
            return "上传失败";
        }
    }
    @RequestMapping("/page")
    public String page(){
        return "index";
    }

}

1.5 lindex页面:只有上传功能

<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
    <input type="file" name="mf">
    <input type="submit" value="上传">
</form>
</body>
</html>

 

posted @ 2020-09-10 11:41  亲爸爸  阅读(531)  评论(0编辑  收藏  举报