欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java fastdbs存文件(实测)

程序员文章站 2024-02-27 14:24:33
...

目的:文件上传到fastdbs服务器上,需要的工具类和配置文件,及其调用方法(实测)


配置文件fast_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=10.120.112.95:22122    //地址
#tracker_server=10.120.112.95: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

工具类

import org.apache.commons.io.FilenameUtils;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;


public class FastDFSUtil {

    public static String uploadFile(byte[] file, String name, long size) throws Exception {
        ClassPathResource resource = new ClassPathResource("fast_client.conf");
        ClientGlobal.init(resource.getClassLoader().getResource("fast_client.conf").getPath());
        //连接跟踪器
        TrackerClient trackerClient = new TrackerClient();
        //连接此对象中封装了(Stoage的IP地址)
        TrackerServer trackerServer = trackerClient.getConnection();
        //连接存储节点
        StorageServer storageServer = null;
        StorageClient1 storageClient1 = new StorageClient1(trackerServer,storageServer);
        //上传文件
        String ext = FilenameUtils.getExtension(name);
        NameValuePair[] meta_list = new NameValuePair[3];
        meta_list[0] = new NameValuePair("filename",name);
        meta_list[1] = new NameValuePair("fileext",ext);
        meta_list[2] = new NameValuePair("filesize",String.valueOf(size));
        String path = storageClient1.upload_file1(file, ext, meta_list);
        return path;
    }

}

程序调用

java fastdbs存文件(实测)