FastDFS 分布式文件插件(Java)

FastDFS 分布式文件插件(Java)

  1. 首先是fastDFS 的util 工具类:
public class FastDFSUtils {

    private static StorageClient client1;

    static {
        try {
            ClientGlobal.initByProperties("fastdfs-client.properties");
            TrackerClient trackerClient = new TrackerClient();
            TrackerServer trackerServer = trackerClient.getConnection();
            client1 = new StorageClient(trackerServer,null);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static  String upload(MultipartFile file) {
        String oldName = file.getOriginalFilename();
        try {
            String[] strings = client1.upload_file(file.getBytes(), oldName.substring(oldName.lastIndexOf(".") + 1), null);
            String fileName="";
            for (String str:strings){
                //System.out.println("Name:----"+str);
                fileName+="/"+str;
            }
            return fileName;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
  1. java 调用:
@PostMapping("/CommonController/IMG")
    public JSON insertCommodityIMG(MultipartFile file){
        log.info("serveraddress------>"+serveraddress);
        String fileId = FastDFSUtils.upload(file);
        String url = serveraddress + fileId;
        if(url!=null){
            return ResultData.getResponseData(url,ResultCode.IMG_SUCCESS);
        }else {
            return ResultData.getResponseData(null,ResultCode.IMG_ERROR);
        }
    }
  1. 最后是pom引用:
<!--fdfs-->
        <dependency>
            <groupId>net.oschina.zcx7878</groupId>
            <artifactId>fastdfs-client-java</artifactId>
            <version>1.27.0.0</version>
        </dependency>

最后附上 fastdfs-client.properties:

##fastdfs-clint.properties

fastdfs.connect_timeout_in_seconds = 5
fastdfs.network_timeout_in_seconds = 5

fastdfs.charset = UTF-8

fastdfs.http_anti_steal_token = false
##fastdfs.http_secret_key = FasDFS1234567890
fastdfs.http_tracker_http_port = 80

fastdfs.tracker_servers = IP:22122

## Whether to open the connection pool, if not, create a new connection every time
fastdfs.connection_pool.enabled = true

## max_count_per_entry: max connection count per host:port , 0 is not limit
fastdfs.connection_pool.max_count_per_entry = 500

##connections whose the idle time exceeds this time will be closed, unit: second,default value id 3600
fastdfs.connection_pool.max_idle_time = 3600

##Maximum waiting time when the maximum number of connections is reached,unit:millisecond, default value is 3600
fastdfs.connection_pool.max_wait_time_in_ms = 1000

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值