Nginx组件
如果FastDFS中保存的是图片信息。希望在WEB应用中可以直接访问FastDFS中的图片进行显示。如果操作?
安装Nginx是为了WEB应用中可以使用HTTP协议直接访问Storage服务中存储的文件。在storage结点所在服务器安装Nginx组件。
需要安装两部分内容。
Nginx应用,在安装nginx应用的时候,同时要在nginx中增加一个FastDFS的组件。
fastdfs-nginx-module模块
上传并解压
tar -zxf fastdfs-nginx-module_v1.16.tar.gz
修改配置
vi /usr/local/fastdfs/fastdfs-nginx-module/src/config
CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
编译安装Nginx
./configure --prefix=/usr/local/tengine
--add-module=/root/fastdfs-nginx-module/src/
make && make install
配置fastdfs-nginx-module
拷贝配置文件
cp /root/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
修改配置文件 mod_fastdfs.conf
tracker_server=192.168.2.109:22122
url_have_group_name = true
store_path0=/var/data/fastdfs-storage/store
拷贝http服务需要的配置
复制FastDFS安装包中的两个配置文件(http.conf和mime.types)到/etc/fdfs目录中
创建网络访问存储服务的软连接
在上传文件到FastDFS后,FastDFS会返回group1/M00/00/00/xxxxxxxxxx.xxx。其中group1是卷名,在mod_fastdfs.conf配置文件中已配置了url_have_group_name,以保证URL解析正确。
而其中的M00是FastDFS保存数据时使用的虚拟目录,需要将这个虚拟目录定位到真实数据目录上。
ln -s /var/data/fastdfs-storage/store/data/ /var/data/fastdfs-storage/store/data/M00
修改nginx配置文件
location ~ /group([0-9])/M00 {
ngx_fastdfs_module;
}
http://192.168.150.11/group1/M00/00/00/wKiWC10xxc6AfHCKAAAib-i5DLU543_big.log
文件名
add_header Content-Disposition "attachment;filename=$arg_attname";
JavaApi
https://github.com/tobato/FastDFS_Client
RAID
配置
fdfs:
so-timeout: 1500
connect-timeout: 600
tracker-list:
- 192.168.150.13:22122
上传文件
// 元数据
Set<MetaData> metaDataSet = new HashSet<MetaData>();
metaDataSet.add(new MetaData("Author", "yimingge"));
metaDataSet.add(new MetaData("CreateDate", "2016-01-05"));
try {
StorePath uploadFile = null;
uploadFile = fc.uploadFile(filename.getInputStream(), filename.getSize(), getFileExtName(filename.getOriginalFilename()), metaDataSet);
account.setPassword(password);
account.setLocation(uploadFile.getPath());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
获取文件后缀
private String getFileExtName(String name) {
// TODO Auto-generated method stub
return (name.substring(name.lastIndexOf(".")+1));
}
或
FilenameUtils.getExtension
返回结果带group
uploadFile.getFullPath() : group1/M00/00/00/wKiWDV0u7ZKALKtNAAADP9sEx2w432.sql
不带group
uploadFile.getPath() : M00/00/00/wKiWDV0u7ZKALKtNAAADP9sEx2w432.sql
缩略图
配置
thumb-image:
width: 150
height: 150
uploadFile = fc.uploadImageAndCrtThumbImage(filename.getInputStream(), filename.getSize(), FilenameUtils.getExtension(filename.getOriginalFilename()), metaDataSet);
下载文件
@RequestMapping("/down")
@ResponseBody
public ResponseEntity<byte[]> down(HttpServletResponse resp) {
DownloadByteArray cb = new DownloadByteArray();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", "aaa.xx");
byte[] bs = fc.downloadFile("group1", "M00/00/00/wKiWDV0vAb-AcOaYABf1Yhcsfws9181.xx", cb);
return new ResponseEntity<>(bs,headers,HttpStatus.OK);
}
来源:https://blog.csdn.net/qq_43186095/article/details/102758633