上一篇 《Linux之安装FastDFS并整合Nginx》安装并配置好FastDFS+Nginx后,现在介绍下FastDFS的客户端使用。
服务端:确认FastDFS+Nginx的环境处于可上传可访问状态
1、启动tracker服务
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
2、启动storage服务
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf
3、启动nginx服务
/usr/local/nginx/sbin/nginx
4、服务器内上传图片
cd /etc/fdfs
/usr/bin/fdfs_test /etc/fdfs/client.conf upload anti-steal.jpg
查看信息,出现图片路径等信息则OK
5、测试是否可通过nginx访问图片
复制上一步上传的图片路径,在浏览器访问
访问得到则环境可用,进行下一部分。
客户端:
Maven项目在pom.xml
引入以下依赖:
<!-- fastDFS-Client -->
<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27.0.0</version>
</dependency>
配置文件client.conf
:
tracker_server = 192.168.41.132:22122
测试代码:
public class FastdfsTest {
@Test
public void testUpload() throws Exception {
// 1.将FastDFS提供的jar包导入
// 2.初始化全局变量。加载配置文件
ClientGlobal.init("C:\\eclipse-workspace\\taotao-manager\\taotao-manager-web\\src\\main\\resources\\properties\\client.conf");
// 3.创建一个TrackerClient对象
TrackerClient trackerClient = new TrackerClient();
// 4.创建一个TrackerServer对象
TrackerServer trackerServer = trackerClient.getConnection();
// 5.声明一个StorageServer
StorageServer storageServer = null;
// 6.获得StorageClient对象
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
// 7.通过StorageClient对象方法上传
String[] strings = storageClient.upload_file("C:\\Users\\WWW\\Pictures\\Camera Roll\\1.jpg", "jpg", null);
// 8.获得返回信息
for (String string : strings) {
System.out.println(string);
}
}
}
FastDFSClient工具类
public class FastDFSClient {
private TrackerClient trackerClient = null;
private TrackerServer trackerServer = null;
private StorageServer storageServer = null;
private StorageClient1 storageClient = null;
// 这里需要传入配置文件路径,比如"classpath:properties/client.conf"
// 文件内容为"tracker_server = 192.168.41.132:22122"
public FastDFSClient(String conf) throws Exception {
if (conf.contains("classpath:")) {
conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());
}
ClientGlobal.init(conf);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageServer = null;
storageClient = new StorageClient1(trackerServer, storageServer);
}
/**
* 上传文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
* @param fileName 文件全路径
* @param extName 文件扩展名,不包含(.)
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
String result = storageClient.upload_file1(fileName, extName, metas);
return result;
}
public String uploadFile(String fileName) throws Exception {
return uploadFile(fileName, null, null);
}
public String uploadFile(String fileName, String extName) throws Exception {
return uploadFile(fileName, extName, null);
}
/**
* 上传文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
* @param fileContent 文件的内容,字节数组
* @param extName 文件扩展名
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception {
String result = storageClient.upload_file1(fileContent, extName, metas);
return result;
}
public String uploadFile(byte[] fileContent) throws Exception {
return uploadFile(fileContent, null, null);
}
public String uploadFile(byte[] fileContent, String extName) throws Exception {
return uploadFile(fileContent, extName, null);
}
}
项目中的使用
@Service
public class PictureServiceImpl implements PictureService {
@Value("${IMAGE_SERVER_BASE_URL}")
private String IMAGE_SERVER_BASE_URL;
@Override
public PictureResult uploadPic(MultipartFile picFile) {
PictureResult result = new PictureResult();
// 判断图片是否为空
if(picFile == null) {
result.setError(1);
result.setMessage("图片为空");
return result;
}
// 上传到图片服务器
try {
// 取图片扩展名
String originalFilename = picFile.getOriginalFilename();
String extName = originalFilename.substring(originalFilename.lastIndexOf(".")+1);
FastDFSClient client = new FastDFSClient("classpath:properties/client.conf");
String url = client.uploadFile(picFile.getBytes(), extName);
// 拼接图片服务器完整图片地址
url = IMAGE_SERVER_BASE_URL + url;
result.setError(0);
result.setUrl(url);
} catch (Exception e) {
e.printStackTrace();
result.setError(1);
result.setMessage("图片上传失败");
}
return result;
}
}
问题
1、客户端上传图片失败
java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.csource.fastdfs.ClientGlobal.getSocket(ClientGlobal.java:208)
at org.csource.fastdfs.StorageServer.<init>(StorageServer.java:43)
at org.csource.fastdfs.TrackerClient.getStoreStorage(TrackerClient.java:144)
...
解决:防火墙开放22122、23000端口
/sbin/iptables -I INPUT -p tcp --dport 22122 -j ACCEPT
/sbin/iptables -I INPUT -p tcp --dport 23000 -j ACCEPT
/etc/rc.d/init.d/iptables save