FastDFS的tracker和storage在刚刚./make.sh install就安装了。这两种角色的安装方式是一样的。不同的是,两种需要不同的配置文件。
我们要启动tracker,就修改上面的默认配置模板,并且启动fdfs_trackerd脚本就可以
cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
vim /etc/fdfs/tracker.conf
#存储日志和数据的根目录
base_path=/opt/fdfs_trackerd
#查看防火墙状态
service iptables status
#关闭防火墙
chkconfig iptables off
/etc/init.d/fdfs_trackerd start
#不过在安装过程中,fdfs已经被设置为系统服务,我们可以采用熟悉的服务启动方式
service fdfs_trackerd start
#检查FastFDS Tracker Server是否启动成功
ps -ef|grep fdfs_trackerd
#设置tracker服务开机启动
chkconfig fdfs_trackerd on
cp /etc/fdfs/storaged.conf.sample /etc/fdfs/storaged.conf
vim /etc/fdfs/storaged.conf
#存储日志和数据的根目录
base_path=/opt/fdfs_storage
#第一个存储目录
store_path0=/opt/fastdfs_storage_data
#
tracker_server=106.15.249.213:22122
#查看防火墙状态
service iptables status
#关闭防火墙
chkconfig iptables off
/etc/init.d/fdfs_storaged start
#不过在安装过程中,fdfs已经被设置为系统服务,我们可以采用熟悉的服务启动方式
service fdfs_storaged start
#检查FastFDS Tracker Server是否启动成功
ps -ef|grep fdfs_storaged
#设置tracker服务开机启动
chkconfig fdfs_storaged on
%s替换字符串
:%s+/usr/local/+/usr/+g
cp /usr/local/fastfds/fastfds-nginx-module-1.20/src/mod_fastdfs.conf /etc/fdfs
vim /etc/fdfs/mod_fastdfs.conf
#连接超时时间20s
connect_timeout=20
#tracker踪迹服务器的地址
tracker_server=106.1*.***.***:22122
#访问连接前缀加上组名
url_have_group_name = true
#文件存储路径
store_path0=/opt/fastdfs_storage_data
#保存退出
:wq
配置媒体类型
cd /usr/local/fastdfs/fastdfs-5.11/conf
cp http.conf mime.types /etc/fdfs/
server {
listen 8081;
server_name localhost;
location ~/group1/M00 {
root /opt/fastdfs_storage_data/data;
ngx_fastdfs_module;
}
}
关闭nginx
./nginx -s stop
在sbin目录下启动ngnix
./nginx
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
</dependency>
package com.leyou.config;
import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;
/**
* @Title:
* @author: Fly
* @date: 2020/5/5 - 17:56
*/
@Configuration
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FastClientImporter {
}
package com.leyou.config;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.domain.ThumbImageConfig;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import java.io.File;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
/**
* @Title:
* @author: Fly
* @date: 2020/5/5 - 18:04
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class FastDFSTest {
@Autowired
private FastFileStorageClient fastFileStorageClient;
@Autowired
private ThumbImageConfig thumbImageConfig;
@Test
public void testUpload() throws FileNotFoundException{
//要上传的文件
File file =new File("E:\\Java\\leyou\\image\\152.jpg");
StorePath storePath = this.fastFileStorageClient.uploadFile(new FileInputStream(file), file.length(), "jpg", null);
System.out.println(storePath.getFullPath());
System.out.println(storePath.getPath());
}
}
以上是访问tracker踪迹系统106.1*.***.***:22122上传
但是访问资源时,需要跟nginx配置的来访问:
package com.leyou.upload.service.impl;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.leyou.upload.service.UploadService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
/**
* @Title:
* @author: Fly
* @date: 2020/5/4 - 13:06
*/
@Service
public class UploadServiceImpl implements UploadService {
private static final List<String> CONTENT_TYPES= Arrays.asList("image/gif","image/jpeg");
public static final Logger LOGGER = LoggerFactory.getLogger(UploadService.class);
@Autowired
private FastFileStorageClient fastFileStorageClient;
@Override
public String uploadImage(MultipartFile file){
//校验文件类型
String originalFilename = file.getOriginalFilename();
// StringUtils.substringAfterLast(originalFilename,".");
String contentType = file.getContentType();
if (!CONTENT_TYPES.contains(contentType)){
LOGGER.info("文件类型不合法:{}",originalFilename);
return null;
}
try {
//校验文件内容
BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
if (bufferedImage==null){
LOGGER.info("文件内容不合法:{}",originalFilename);
return null;
}
//保存到服务器
//将临时存储的文件移动到真实存储路径下
// file.transferTo(new File("E:\\Java\\leyou\\image\\"+originalFilename));
String substringAfterLast = StringUtils.substringAfterLast(originalFilename, ".");
StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), substringAfterLast, null);
//返回url
// return "http://image.fly.com/"+originalFilename;
return "http://106.**.***.***:8081/"+storePath.getFullPath();
} catch (IOException e) {
LOGGER.info("服务器内部错误:{}",originalFilename);
e.printStackTrace();
}
return null;
}
}