一个FastDFS整合Java的坑
背景
本篇博客的主要目的,就是记录一个有关FastDFS
整合Spring(Java)
时的坑
事发环境
1、FastDFS 5.08
安装成功,服务器端测试没问题
测试命令
fdfs_test /etc/fdfs/client.conf upload /usr/local/tools/a.jpg
结果
2、FastDFS
整合Nginx
完毕
Nginx配置
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ /group[1-3]/M00 {
root /usr/local/tools/fastdfs/storage0/data;
ngx_fastdfs_module;
}
}
}
访问图片成功
3、SpringBoot工具类没问题
工具类参照的是纯洁的微笑
大佬的工具类
之所以说工具类没问题,是因为,我试了下朋友的FastDFS
服务器,可以上传。
4、每次测试都报错
测试用例
public static void main(String[] args) {
byte[] file_buff = null;
try {
File file = new File("E:\\lt\\lt.png");
InputStream inputStream = new FileInputStream(file);
if (inputStream != null) {
int len1 = inputStream.available();
file_buff = new byte[len1];
inputStream.read(file_buff);
}
FastDFSFile fastDFSFile = new FastDFSFile("lt.png",file_buff,"png");
String[] upload = FastDFSClient.upload(fastDFSFile);
String path = FastDFSClient.getTrackerUrl() + upload[0] + "/" + upload[1];
System.out.println(path);
} catch (IOException e) {
e.printStackTrace();
}
}
结果,每次都是连接失败
我试了三种工具类,都不行。
问题解决
再试了N种方法,包括:检测端口,更换工具类,重启Nginx,修改Nginx,开放22122端口,关闭防火墙…
最终,发现问题原因,是因为服务器中storage.conf
和 tracker.conf
的ip地址出现了问题!
我在搭建时,ip地址写的都是内网地址,再一一改成外网地址后,终于可以上传了!!!