搭建FastDfs文件服務器:搭建FastDfs文件服務器
- 導入依賴包
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.27.2</version>
</dependency>
- FastDfs配置類
@Configuration
@Import(FdfsClientConfig.class) // 導入FastDFS-Client組件
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING) // 解決jmx重復注冊bean的問題
public class FdfsConfiguration {
}
- 在application.yml文件中配置參數
ip: 192.168.11.12 #這只是一個例子,根據自己的ip進行更改
# 分布式文件系統FDFS配置
fdfs:
soTimeout: 1500 #socket連接超時時長
connectTimeout: 600 #連接tracker服務器超時時長
trackerList: #TrackerList參數,支持多個,我這里只有一個,如果有多個在下方加- x.x.x.x:port
- ${ip}:22122
web-server-url: http://${ip}:7003/ #FDFS中的nginx的ip和port
- 編寫FDFS工具類
@Component
public class FastDfsUtil {
@Autowired
private FastFileStorageClient fastFileStorageClient;
@Autowired
private FdfsWebServer fdfsWebServer;
public String uploadFile(MultipartFile file) throws IOException {
StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null);
String fullPath = storePath.getFullPath();
getResAccessUrl(fullPath);
return fullPath;
}
public String uploadFile(File file) {
try {
FileInputStream inputStream = new FileInputStream(file);
StorePath storePath = fastFileStorageClient.uploadFile(inputStream, file.length(), FilenameUtils.getExtension(file.getName()), null);
return storePath.getFullPath();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public byte[] downloadFile(String filePath) {
StorePath storePath = StorePath.parseFromUrl(filePath);
byte[] bytes = fastFileStorageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadByteArray());
return bytes;
}
public Boolean deleteFile(String filePath) {
if (StringUtils.isEmpty(filePath)) {
return false;
}
try {
StorePath storePath = StorePath.parseFromUrl(filePath);
fastFileStorageClient.deleteFile(storePath.getGroup(), storePath.getPath());
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
// 封裝文件完整URL地址
public String getResAccessUrl(String path) {
String url = fdfsWebServer.getWebServerUrl() + path;
System.out.println("上傳文件地址為:\n" + url);
return url;
}
}
- 編寫Controller進行測試
@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {
@Autowired
private FastDfsUtil fastDfsUtil;
@PostMapping("/upload")
public void uploadFile(MultipartFile file) throws IOException {
String s = fastDfsUtil.uploadFile(file);
String resAccessUrl = fastDfsUtil.getResAccessUrl(s);
}
@GetMapping("/download")
public void downloadFile(String filePath, HttpServletResponse response) throws IOException {
byte[] bytes = fastDfsUtil.downloadFile(filePath);
String fileName = "哈哈.jpg";
response.setContentType("application/force-download");// 設置強制下載不打開
//方式一
// fileName=new String(fileName.getBytes(), "ISO8859-1")
//方式二
fileName = URLEncoder.encode(fileName, "utf-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
IOUtils.write(bytes, response.getOutputStream());
}
/**
* 流媒體的方式播放視頻,只能從頭看到尾,不能手動點擊重新看已經看過的內容
* @param filePath
* @param response
* @throws IOException
*/
@GetMapping("/play")
public void streamMedia(String filePath, HttpServletResponse response) throws IOException {
byte[] bytes = fastDfsUtil.downloadFile(filePath);
IOUtils.copy(new ByteArrayInputStream(bytes), response.getOutputStream());
response.flushBuffer();
}
@GetMapping("/delete")
public void deleteFile(String filePath) {
Boolean result = fastDfsUtil.deleteFile(filePath);
}
}