fdfs防盗链配置及java开发

来源:互联网 发布:男神的偶像知乎 编辑:程序博客网 时间:2024/05/18 20:48

在上篇文章技术上:http://blog.csdn.net/www_wangzheguilai/article/details/76647443

1、修改配置文件

配置防盗链功能,其实配置端相当简单,如下:

cd /etc/fdfs

vim http.conf

#true-开启防盗链检查,false-关闭防盗链

http.anti_steal.check_token=true

#防盗链密钥

http.anti_steal.secret_key=FastDFS1234567890

#每次生成的访问链的有效时间,单位S

http.anti_steal.token_ttl=900

#校验失败,访问的图片

http.anti_steal.token_check_fail=/etc/fdfs/account_order.png

2、java端

修改java端的fdfs_client.conf文件

http.anti_steal_token = truehttp.secret_key = FastDFS1234567890tracker_server = 10.80.103.63:22122

生成防盗链连接的java代码块如下:

String[] results = new String[2];int errno = StorageClient1.split_file_id(fileId, results);if(errno != 0){throw new MyException("fileId格式错误!");}int ts = (int) (System.currentTimeMillis()/1000);String token = ProtoCommon.getToken(results[1], ts, ClientGlobal.getG_secret_key());return serverAddr + "/" + fileId + "?token=" + token + "&ts=" + ts;

生成防盗链地址:

http://10.80.103.63:9999/group1/M00/00/00/ClBnP1mD8NqAe-VeAABdrZgsqUU923_big.jpg?token=bcf7ed89b01ee6d72010d70eda63e0e8&ts=1501821311