我们在 分布式文件系统FastDFS的搭建 搭建好FastDfs后。线上我们就可以使用FastDFS.接下来我们就集成下。
一:使用jar包
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.1-RELEASE</version>
<!--排除这个slf4j-log4j12-->
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</exclusion>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
二:配置文件(application.properties)
##fsdf配置
#连接超时时间
fdfs.connect-timeout=601
#读取时间
fdfs.so-timeout=1501
#TrackerList参数,持多个
##正式环境和配置
fdfs.tracker-list= fastdfs的ip:22122
fdfs.tracker_server=fastdfs的ip:22122
#前端获取url
fdfs.web-server-url=返回前端图片的url.
二:准备FastDfs的util
import com.github.tobato.fastdfs.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
/**
* @version 1.0
* @date 2020/12/3 10:26
*/
@Component
public class FastDFSClient {
@Resource
private FastFileStorageClient fastFileStorageClient;
@Resource
private FdfsWebServer fdfsWebServer;
/**
* 上传文件
* @param file 文件对象
* @return 文件访问地址
* @throws IOException
*/
public String uploadFile(MultipartFile file) {
try {
StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(),file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()),null);
return getResAccessUrl(storePath);
}catch (Exception e){
e.printStackTrace();
return null;
}
}
/**
* 上传文件
* @param file 文件对象
* @return 文件访问地址
* @throws IOException
*/
public String uploadFile(File file) throws IOException {
FileInputStream inputStream = new FileInputStream (file);
StorePath storePath = fastFileStorageClient.uploadFile(inputStream,file.length(), FilenameUtils.getExtension(file.getName()),null);
return getResAccessUrl(storePath);
}
/**
* 将一段字符串生成一个文件上传
* @param content 文件内容
* @param fileExtension
* @return
*/
public String uploadFile(String content, String fileExtension) {
byte[] buff = content.getBytes(Charset.forName("UTF-8"));
ByteArrayInputStream stream = new ByteArrayInputStream(buff);
StorePath storePath = fastFileStorageClient.uploadFile(stream,buff.length, fileExtension,null);
return getResAccessUrl(storePath);
}
// 封装图片完整URL地址
private String getResAccessUrl(StorePath storePath) {
String fileUrl = fdfsWebServer.getWebServerUrl() + storePath.getFullPath();
return fileUrl;
}
//获取相对路径
private String getRelativeUrl(StorePath storePath){
return storePath.getFullPath();
}
//获取fastDfs服务器路径
public String getWebServerUrl(){
return fdfsWebServer.getWebServerUrl();
}
/**
* 删除文件
* @param fileUrl 文件访问地址
* @return
*/
public void deleteFile(String fileUrl) throws Exception {
if (StringUtils.isEmpty(fileUrl)) {
return;
}
try {
StorePath storePath = StorePath.praseFromUrl(fileUrl);
fastFileStorageClient.deleteFile(storePath.getGroup(), storePath.getPath());
} catch (Exception e) {
e.printStackTrace();
}
}
}
三:使用Controller
@Resource
private FastDFSClient fastdfsUtil;
@ApiOperation(value = "上传图片(后台生成推广海报使用)")
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public void upload(@RequestParam(value = "file", required = false) MultipartFile resourceFile) throws Exception {
long size = resourceFile.getSize();
System.out.println("文件大小:"+size);
String fileName = resourceFile.getOriginalFilename();
System.out.println("文件名称:"+fileName);
String filePath = fastdfsUtil.uploadFile(resourceFile);
System.out.println("返回图片的地址:"+filePath);
}