概述
FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等等。 -----《百度百科》
最近有在项目里面使用到FastDFS,于是在这里做个记录,以便后续使用查看,至于FastDfs的配置,我会专门出一篇文章进行记录,这里不再赘述。
以下是我写的一个简单的工具类,用于做文件的上传,下载,删除等基础功能。
我们首先需要在SpringBoot项目里面集成FastDFS:
- 添加Maven依赖
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.1-RELEASE</version>
<--! 加入fastdfs的依赖之后会和spring的logback冲突,产生了依赖冲突。
解决办法:在fastdfs的依赖中排除自身的logback依赖。当然,如果你的项目里面没有集成logback,可能不需要加这个-->
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
- 在yml文件里面配置FastDFS的参数
fdfs:
soTimeout: 1500
connectTimeout: 600
thumbImage: #缩略图生成参数
width: 150
height: 150
trackerList: #TrackerList参数,支持多个
- 192.168.x.xxx:22122
resHost: http://192.168.xxx/
storagePort: 8888
- 添加Fast DFS的配置类
@Configuration
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FdfsClientConf {
}
- 编写FastDFS工具类
/**
*
* @description file upload of type Mutilpart
* @author feifei.yuan
* @param file: the file will to be uploaded
* @return String
* @throws IOException
* @date Aug 27, 2020 10:30:26 AM
*/
public String upLoadFile(MultipartFile file) {
StorePath storePath = null;
try {
storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
FilenameUtils.getExtension(file.getOriginalFilename()), null);
} catch (IOException e) {
e.printStackTrace();
}
return getResAccessUrl(storePath);
}
/**
*
* @description normal file upload
* @author feifei.yuan
* @param file : the file to be uploaded
* @return String
* @throws FileNotFoundException
* @date Aug 27, 2020 10:31:08 AM
*/
public String upLoadFile(File file) {
InputStream inputStream;
StorePath storePath = null;
try {
inputStream = new FileInputStream(file);
storePath = storageClient.uploadFile(inputStream, file.length(), FilenameUtils.getExtension(file.getName()),
null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return getResAccessUrl(storePath);
}
/**
*
* @description return the address of the uploaded file
* @author feifei.yuan
* @param storePath
* @return String
* @date Aug 27, 2020 10:32:32 AM
*/
public String getResAccessUrl(StorePath storePath) {
String fileUrl = storePath.getFullPath();
return fileUrl;
}
/*
* why the next tow functions need to add lock?
* <p>
* In the case of concurrency, deleting files and downloading files will cause IO exceptions
* <p>
*
* <p>
* There is no high concurrency problem here, but it may be due to network reasons
* or the file download or delete time is too long, the last operation has not been completed,
* the next operation is coming, at this time you need to lock to ensure that there is
* no concurrency security
* <p>
* <p>
* Don't ask me why I know, I just know.
* <p>
*/
/**
*
* @description delete the file based the given fileUrl
* @author feifei.yuan
* @param fileUrl: the file will be deleted on the fastdfs server path
* @return void
* @date Aug 27, 2020 10:32:47 AM
*/
public synchronized void deleteFile(String fileUrl) {
if (StringUtils.isEmpty(fileUrl))
return;
StorePath storePath = StorePath.praseFromUrl(fileUrl);
storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
}
/**
*
* @description download file form fastdfs
* @author fefei.yuan
* @param group: the fastdfs group
* path: the file path
* @return byte[]
* @date Aug 28, 2020 5:37:28 PM
*/
public void downLoadFile(HttpServletResponse response, String fileUrl, String fileName) {
ReentrantLock lock = new ReentrantLock();
lock.lock();
byte[] bs = null;
try {
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
StorePath storePath = StorePath.praseFromUrl(fileUrl);
bs = storageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadByteArray());
response.getOutputStream().write(bs);
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
非非也是第一次使用FastDFS,里面踩了很多坑呢,希望我写的文章可以帮助到大家少踩坑,后续我会出一篇FastDFS+Nginx+FastDFS_Nginx_Moudle的服务器配置,大家敬请期待。
码字不易,动动你们的小手指,给非非一个赞吧,你们的给力就是我写作的动力,我是袁非非!!!
最后
以上就是大气自行车为你收集整理的FastDFS操作文件上传,下载,删除的全部内容,希望文章能够帮你解决FastDFS操作文件上传,下载,删除所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复