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
*/
public void delete(String path) {
fastFileStorageClient.deleteFile(path);
}
/**
* 删除
* @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.setContentType("applicatoin/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
ServletOutputStream out = response.getOutputStream();
out.write(bytes);
out.close();
}
}
配置文件
# ===================================
# 分布式文件系统FDFS配置
# ===================================
## 连接超时时间
# fdfs.connect-timeout=600
# # 读取超时时间
# fdfs.so-timeout=1500
# # 缩略图的宽高
# fdfs.thumb-image.height=150
# fdfs.thumb-image.width=150
# # tracker服务配置地址列表,替换成自己服务的IP地址,支持多个
# fdfs.tracker-list=192.168.1.142:22122
# fdfs.pool.jmx-enabled=false
fdfs:
connect-timeout: 600
so-timeout: 1500
thumb-image:
height: 150
width: 130
tracker-list: 192.168.1.142:22122
pool:
jmx-enabled: false