zoukankan      html  css  js  c++  java
  • fasrdfs在spring cloud中的使用

    在微服务中我们需要在开启一个服务然后通过Fegin接口调用

    pom导包

    如果没有意外的话你的包是肯定导不了的,然后你需要去官方文档下载源码或者jar

    https://github.com/happyfish100/fastdfs-client-java

    <!--exclusions排除包-->
            <dependency>
                <groupId>org.csource</groupId>
                <artifactId>fastdfs-client-java</artifactId>
                <version>1.29-SNAPSHOT</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-log4j12</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

    在resources文件夹下写一个配置fdfs_client.conf

    118.25.154.214这是是你部署的FastDFS的端口

    tracker_server=118.25.154.214:22122

    然后使用一个工具类

    package cn.jiedada.utils;
    
    import org.csource.fastdfs.*;
    
    
    @SuppressWarnings("Duplicates")
    public class FastDfsUtil {
    
        //加载fastdfs配置文件
        public static String CONF_FILENAME  = FastDfsUtil.class.getClassLoader()
                .getResource("fdfs_client.conf").getFile();
    
        /**
         * 上传文件
         * @param file : 文件,byte[]格式
         * @param extName :扩展名
         * @return
         */
        public static  String upload(byte[] file,String extName) {
            try {
                //初始化配置
                ClientGlobal.init(CONF_FILENAME);
                //创建Tracker客户端
                TrackerClient tracker = new TrackerClient();
                //获取一个Tracker服务
                TrackerServer trackerServer = tracker.getTrackerServer();
                //创建一个Storage服务
                StorageServer storageServer = null;
                //得到文件存储对象的客户端
                StorageClient storageClient = new StorageClient(trackerServer, storageServer);
                //执行上传 :文件,扩展名 , 额外参数
                //返回:文件的路径(groupName 组名, path 文件路径)
                String fileIds[] = storageClient.upload_file(file,extName,null);
                //  /group1/M00/00/00/dhma1l6ecFeAckIhAABGKnIct1M652.jpg
                String filePath =   "/"+fileIds[0]+"/"+fileIds[1];
                System.out.println("文件上传成功:"+filePath);
                return filePath;
            } catch (Exception e) {
                e.printStackTrace();
                return  null;
            }
        }
        /**
         * 上传文件
         * @param path :文件的绝对路径  d:/xxx.jpg
         * @param extName:扩展名
         * @return
         */
        public static  String upload(String path,String extName) {
     
            try { 
                ClientGlobal.init(CONF_FILENAME);
                TrackerClient tracker = new TrackerClient();
                TrackerServer trackerServer = tracker.getTrackerServer();
                StorageServer storageServer = null;
                StorageClient storageClient = new StorageClient(trackerServer, storageServer);
                String fileIds[] = storageClient.upload_file(path, extName,null);
                return  "/"+fileIds[0]+"/"+fileIds[1];
            } catch (Exception e) {
                e.printStackTrace();
                return  null;
            }
        }
    
        /**
         * 下载文件
         * @param groupName :组名
         * @param fileName :文件路径
         * @return
         */
        public static byte[] download(String groupName,String fileName) {
            try {
                ClientGlobal.init(CONF_FILENAME);
                TrackerClient tracker = new TrackerClient();
                TrackerServer trackerServer = tracker.getTrackerServer();
                StorageServer storageServer = null;
                StorageClient storageClient = new StorageClient(trackerServer, storageServer);
                byte[] b = storageClient.download_file(groupName, fileName);
                return  b;
            } catch (Exception e) {
                e.printStackTrace();
                return  null;
            } 
        }
         
    
        /**
         * 删除文件
         * @param groupName
         * @param fileName
         */
        public static boolean delete(String groupName,String fileName){
            try { 
                ClientGlobal.init(CONF_FILENAME);
                TrackerClient tracker = new TrackerClient();
                TrackerServer trackerServer = tracker.getTrackerServer();
                StorageServer storageServer = null;
                StorageClient storageClient = new StorageClient(trackerServer,
                        storageServer); 
                int i = storageClient.delete_file(groupName,fileName);
                System.out.println( i==0 ? "删除成功" : "删除失败:"+i);
                return i == 0;
            } catch (Exception e) {
                e.printStackTrace();
                throw  new RuntimeException("删除异常,"+e.getMessage());
            } 
        }
    }
    View Code

     然后写入Controller

    @RequestMapping("/upload")
        public AjaxResult upload(@RequestBody MultipartFile file){
            try {
                //获取扩展名: 原始的文件名 oo.xxx.jpg : common-io
                String extName = FilenameUtils.getExtension(file.getOriginalFilename());
                //上传文件
                String filePath = FastDfsUtil.upload(file.getBytes() , extName);
                //返回路径
                return AjaxResult.me().setResultObj(filePath);
            } catch (IOException e) {
                e.printStackTrace();
                return AjaxResult.me().setSuccess(false).setMessage("上传失败["+e.getMessage()+"]");
            }
        }

    如果谁需要调用则用Fegin接口调用

  • 相关阅读:
    PHP迭代器
    PDO

    五种常见的 PHP 设计模式
    php fastcgi_finish_request 函数的理解
    vagrant up 启动虚拟机报错
    thrift php 的使用
    python 基本知识学习(一)
    PHP实现进程间通信:消息队列 msg_get_queue 函数不存在
    [转]Python函数的各种参数用法(含星号参数)
  • 原文地址:https://www.cnblogs.com/xiaoruirui/p/13673819.html
Copyright © 2011-2022 走看看