zoukankan      html  css  js  c++  java
  • FastDFS简单入门小demo

    图片上传

    需要引入 FastDFS 相关的jar包,但是这个jar没有在中央仓库,所以还得需要找到这个jar手动安装到自己的本地仓库才能使用。

    需要一个配置文件   fdfs_client.conf

    需要一个 FastDFS 服务器

    看代码:

    fdfs_client.conf

    # connect timeout in seconds
    # default value is 30s
    connect_timeout=30
    
    # network timeout in seconds
    # default value is 30s
    network_timeout=60
    
    # the base path to store log files
    base_path=/home/fastdfs
    
    # tracker_server can ocur more than once, and tracker_server format is
    #  "host:port", host can be hostname or ip address
    tracker_server=192.168.25.133:22122    #服务器地址
    
    #standard log level as syslog, case insensitive, value list:
    ### emerg for emergency
    ### alert
    ### crit for critical
    ### error
    ### warn for warning
    ### notice
    ### info
    ### debug
    log_level=info
    
    # if use connection pool
    # default value is false
    # since V4.05
    use_connection_pool = false
    
    # connections whose the idle time exceeds this time will be closed
    # unit: second
    # default value is 3600
    # since V4.05
    connection_pool_max_idle_time = 3600
    
    # if load FastDFS parameters from tracker server
    # since V4.05
    # default value is false
    load_fdfs_parameters_from_tracker=false
    
    # if use storage ID instead of IP address
    # same as tracker.conf
    # valid only when load_fdfs_parameters_from_tracker is false
    # default value is false
    # since V4.05
    use_storage_id = false
    
    # specify storage ids filename, can use relative or absolute path
    # same as tracker.conf
    # valid only when load_fdfs_parameters_from_tracker is false
    # since V4.05
    storage_ids_filename = storage_ids.conf
    
    
    #HTTP settings
    http.tracker_server_port=80
    
    #use "#include" directive to include HTTP other settiongs
    ##include http.conf
    View Code

    Test.java

    package cn.itcast.demo;
    
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    import org.csource.fastdfs.ClientGlobal;
    import org.csource.fastdfs.StorageClient;
    import org.csource.fastdfs.StorageServer;
    import org.csource.fastdfs.TrackerClient;
    import org.csource.fastdfs.TrackerServer;
    
    public class Test {
    
        public static void main(String[] args) throws FileNotFoundException, IOException, Exception {
            // 1.加载配置文件
            ClientGlobal.init("D:\pinyougou_bigdata1\fastDFSdemo\src\main\resources\fdfs_client.conf");
            // 2.构建一个管理者客户端
            TrackerClient client=new TrackerClient();
            // 3.连接管理者服务端
            TrackerServer trackerServer = client.getConnection();
            //4. 声明存储服务端
            StorageServer storageServer=null;
            //5. 获取存储服务器的客户端对象
            StorageClient storageClient=new StorageClient(trackerServer, storageServer);
            //6.上传文件
            String[] strings = storageClient.upload_file("e:\image\b.jpg", "jpg", null);
            //7.显示上传结果 file_id
            for(String str:strings){
                System.out.println(str);
            }        
            
        }
    
    }
  • 相关阅读:
    入门(一)---Java的发展史
    移除元素
    TCP的 “三次握手” 和“四次挥手”,到底是什么鬼?
    功能测试框架
    python学习笔记之--__new__方法和__init__方法
    HTTP协议状态码详解
    python学习笔记之--hasattr函数
    一文总结软件测试工程师面试前必背的面试题(持续更新中)
    MYSQL安装file /usr/share/mysql/charsets/README from install of MySQL-server-5.6.35-1.el6.x86_64 conflicts with file from package mariadb-libs-1:5.5.60-1.el7_5.x86_64报错
    centos7 安装salt起不来处理
  • 原文地址:https://www.cnblogs.com/coder-lzh/p/9129660.html
Copyright © 2011-2022 走看看