Linux 安装fastDFS
安装教程(官网)
https://github.com/happyfish100/fastdfs/wiki
用Spring boot 操作fastDFS
pom依赖
<!-- 当前日期最新版-->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.7</version>
</dependency>
application.yml
fdfs:
so-timeout: 1501
connect-timeout: 2000
thumb-image: #缩略图生成参数
width: 150
height: 150
tracker-list: #TrackerList参数,支持多个
- 127.0.0.1:22122
controller部分
或
fdfs:
tracker-list: 192.168.128.129:22122
controller部分
@RestController
public class TestApi {
@Autowired
private FastFileStorageClient fastFileStorageClient;
/**
* 文件上传
*
* @param file
* @return
* @throws IOException
*/
@ApiOperation("文件上传")
@PostMapping("/uppload")
public StorePath test(@RequestParam MultipartFile file) throws IOException {
// 设置文件信息
Set<MataData> mataData = new HashSet<>();
mataData.add(new MataData("author", "zonghui"));
mataData.add(new MataData("description", "xxx文件,嘿嘿嘿"));
// 上传 (文件上传可不填文件信息,填入null即可)
StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), mataData);
return storePath;
}
/**
* 文件删除
*
* @param path
* @return
*/
@ApiOperation("文件删除")
@DeleteMapping("/delete")
public String delete(@RequestParam String path) {
// 第一种删除:参数:完整地址
fastFileStorageClient.deleteFile(path);
// 第二种删除:参数:组名加文件路径
// fastFileStorageClient.deleteFile(group,path);
return "恭喜恭喜,删除成功!";
}
/**
* 文件下载
*
* @param path
* @return
*/
@ApiOperation("文件下载")
@GetMapping("/download")
public void downLoad(@RequestParam String group, @RequestParam String path, @RequestParam String fileName, HttpServletResponse response) throws IOException {
// 获取文件
byte[] bytes = fastFileStorageClient.downloadFile(group, path, new DownloadByteArray());
//设置相应类型application/octet-stream (注:applicatoin/octet-stream 为通用,一些其它的类型苹果浏览器下载内容可能为空)
response.reset();
response.setContentType("applicatoin/octet-stream");
//设置头信息 Content-Disposition为属性名 附件形式打开下载文件 指定名称 为 设定的fileName
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
// 写入到流
ServletOutputStream out = response.getOutputStream();
out.write(bytes);
out.close();
}
}