linux 安装 fastDFS 配置nginx访问(单节点)
添加依赖
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.7</version>
</dependency>
application.yml 配置
# 分布式文件系统FDFS配置
fdfs:
connect-timeout: 600 # 连接超时时间
so-timeout: 1500 # 读取超时时间
tracker-list: 192.168.10.134:22122 # tracker服务配置地址列表,替换成自己服务的IP地址,支持多个
pool:
jmx-enabled: false
thumb-image:
height: 150 # 缩略图的宽高
width: 150
工具类
package com.vxdata.fastDfs.util;
import com.github.tobato.fastdfs.domain.fdfs.MetaData;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.Set;
@Component
public class FastdfsUtils {
public static final String DEFAULT_CHARSET = "UTF-8";
@Autowired
private FastFileStorageClient fastFileStorageClient;
/**
* 上传文件并返回文件路径
*
* @param file
* @return 文件路径
* @throws IOException
*/
public StorePath upload(MultipartFile file) throws IOException {
// 设置文件信息
Set<MetaData> mataData = new HashSet<>();
mataData.add(new MetaData("author", "fastdfs"));
mataData.add(new MetaData("description", file.getOriginalFilename()));
// 上传
StorePath storePath = fastFileStorageClient.uploadFile(
file.getInputStream(), file.getSize(),
FilenameUtils.getExtension(file.getOriginalFilename()),
null);
return storePath;
}
/**
* 根据全路径删除
*
* @param path "fullPath": "group1/M00/00/00/wKgAkGIfF2KAG7qSAAB-jaFjG-Q678.jpg"
*/
public void delete(String path) {
fastFileStorageClient.deleteFile(path);
}
/**
* 根据组和路径删除
* "group": "group1",
* "path": "M00/00/00/wKgAkGIfF2KAG7qSAAB-jaFjG-Q678.jpg",
* "fullPath": "group1/M00/00/00/wKgAkGIfF2KAG7qSAAB-jaFjG-Q678.jpg"
*
* @param group
* @param path
*/
public void delete(String group, String path) {
fastFileStorageClient.deleteFile(group, path);
}
/**
* 文件下载
*
* @param path 文件路径,例如:/group1/path=M00/00/00/itstyle.png
* @param filename 下载的文件命名
* @return
*/
public void download(String path, String filename, HttpServletResponse response) throws IOException {
// 获取文件
StorePath storePath = StorePath.parseFromUrl(path);
if (StringUtils.isBlank(filename)) {
filename = FilenameUtils.getName(storePath.getPath());
}
byte[] bytes = fastFileStorageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadByteArray());
response.reset();
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, DEFAULT_CHARSET));
response.setCharacterEncoding(DEFAULT_CHARSET);
// 设置强制下载不打开
response.setContentType("application/force-download");
ServletOutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
outputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
在启动类上添加注解: @Import(FdfsClientConfig.class)
package com.vxdata.fastDfs;
import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;
//解决jmx重复注册bean的问题
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
//只需要一行注解就可以拥有带有连接池的FastDFS Java客户端了
@Import(FdfsClientConfig.class)
@SpringBootApplication
public class ProjectApplication {
public static void main(String[] args) {
SpringApplication.run(ProjectApplication.class, args);
}
}