fastDFS工具类
- maven依赖
<!--fastDFS-->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.6</version>
</dependency>
- 配置类
package com.config.fdfs;
import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;
import javax.servlet.MultipartConfigElement;
import java.io.File;
/**
* fastDFS Config By CHENYB date 2019-08-14
*/
@Configuration
@Import(FdfsClientConfig.class)//引入前辈封装的配置
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)//解决jmx重复注册bean的问题
public class FastDFSConfig {
/**
* 设置文件上传临时路径
*/
@Bean
MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
String location = System.getProperty("user.dir") +File.separator+ "data"+File.separator+"tmp";
File tmpFile = new File(location);
if (!tmpFile.exists()) {
tmpFile.mkdirs();
}
factory.setLocation(location);
return factory.createMultipartConfig();
}
}
- 工具类
package com.utils;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.nio.charset.Charset;
/**
* FastDFS 工具类 By CHENYB date 2019-08-14
* fastdfs-client version 1.26.6
*/
@Component
public class FastDFSClient {
private final Logger logger = LoggerFactory.getLogger(FastDFSClient.class);
@Autowired
private FastFileStorageClient storageClient;
/**
* 文件上传 (MultipartFile)
*/
public String uploadFile(MultipartFile file) throws IOException {
StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
FilenameUtils.getExtension(file.getOriginalFilename()), null);
return getResAccessUrl(storePath);
}
/**
* 文件上传 (File)
*/
public String uploadFile(File file) throws IOException {
FileInputStream inputStream = new FileInputStream(file);
StorePath path = storageClient.uploadFile(inputStream, file.length(),
FilenameUtils.getExtension(file.getName()), null);
return getResAccessUrl(path);
}
/**
* 文件上传 (InputStream)
*/
public String uploadFile(InputStream is, long size, String fileName) {
StorePath path = storageClient.uploadFile(is, size, fileName, null);
return getResAccessUrl(path);
}
/**
* 将一段文本文件写到fastdfs的服务器上
*/
public String uploadFile(String content, String fileExtension) {
byte[] buff = content.getBytes( Charset.forName("UTF-8"));
ByteArrayInputStream stream = new ByteArrayInputStream(buff);
StorePath path = storageClient.uploadFile(stream, buff.length, fileExtension, null);
return getResAccessUrl(path);
}
/**
* 返回文件上传成功后的地址名称ַ
*/
private String getResAccessUrl(StorePath storePath) {
String fileUrl = storePath.getFullPath();
return fileUrl;
}
/**
* 下载文件 (文件url 文件路径)
*/
public byte[] download(String fileUrl) {
String group = fileUrl.substring(0, fileUrl.indexOf("/"));
String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
byte[] bytes = storageClient.downloadFile(group, path, new DownloadByteArray());
return bytes;
}
/**
* 删除文件
*/
public void deleteFile(String fileUrl) {
if (StringUtils.isEmpty(fileUrl)) {
return;
}
try {
StorePath storePath = StorePath.parseFromUrl(fileUrl);
storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
} catch (FdfsUnsupportStorePathException e) {
logger.warn(e.getMessage());
}
}
/**
* 返回后缀名包含.
*/
public String getSuffixName(MultipartFile file){
String originalFilename = file.getOriginalFilename();
return originalFilename.substring( originalFilename.lastIndexOf( "." ),originalFilename.length() );
}
/**
* 返回文件名
*/
public String getFileName(MultipartFile file){
String originalFilename = file.getOriginalFilename();
return originalFilename.substring( 0, originalFilename.lastIndexOf( "." ));
}
}
Mr.chenyb 随笔记录,方便自己学习
2019-08-19