1、在项目中添加依靠到Maven的pom文件
<!-- fastdfs-client -->
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
2、FastDFS客户端东西类,已封装如下(便利直接利用),
本家儿要东西类方式:
/**
* @Description 文件存储上传客户端
* @author javaer
* @time 2020年5月30日 上午9:27:26
*/
public class FastDFSClient {
/**
* MultipartFile 上传文件
*
* @param file MultipartFile
* @return 返回上传当作功后的文件路径
*/
public static String uploadFileWithMultipart(MultipartFile file) throws BusinessException {
return upload(file, null);
}
/**
* 以附件形式下载文件
*
* @param filepath 文件路径
* @param response
*/
public static void downloadFile(String filepath, HttpServletResponse response) throws BusinessException {
download(filepath, null, null, response);
}
/**
* 删除文件
* @param filepath 文件路径
* @return 删除当作功返回 0, 掉败返回其它
*/
public static int deleteFile(String filepath) throws BusinessException {
if (StringUtils.isBlank(filepath)) {
throw new BusinessException(FileErrorCode.FILE_PATH_ISNULL.CODE);
}
TrackerServer trackerServer = TrackerServerPool.borrowObject();
StorageClient1 storageClient = new StorageClient1(trackerServer, null);
int success = 0;
try {
success = storageClient.delete_file1(filepath);
if (success != 0) {
throw new BusinessException(FileErrorCode.FILE_DELETE_FAILED.CODE);
}
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
throw new BusinessException(FileErrorCode.FILE_DELETE_FAILED.CODE);
}
// 返还对象
TrackerServerPool.returnObject(trackerServer);
return success;
}
}
3、将fastdfs-client.properties设置装备摆设文件放置于引用项目标resource目次下
4、如下图,在项目中可直接经由过程FastDFSClient东西类挪用所用的方式
5、项目中上传文件后利用的结果,由前端页面上传文件,至此项目实现分布式存储。
0 篇文章
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!