轻量级分布式文件系统FastDFS(三)之SpringBoot访问
SpringBoot访问FastDFS
1. 引入Maven依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.1-RELEASE</version>
</dependency>
</dependencies>
2.配置文件application.yml
fdfs.connect-timeout=600
fdfs.tracker-list=192.168.59.10:22122,192.168.59.11:22122,192.168.59.12:22122
server.port=8899
3. 编写上传下载文件的Service接口及实现类
package com.snf.service;
public interface FastDFSClientService {
String uploadFile(byte[] bytes, long fileSize, String extension);
byte[] downloadFile(String fileUrl);
}
package com.snf.service.impl;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.snf.service.FastDFSClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.ByteArrayInputStream;
@Service
public class FastDFSClientServiceImpl implements FastDFSClientService {
@Autowired
private FastFileStorageClient fastFileStorageClient;
/**
* 文件上传
*
* @param bytes 文件字节
* @param fileSize 文件大小
* @param extension 文件扩展名
* @return FastDFS路径
*/
public String uploadFile(byte[] bytes, long fileSize, String extension) {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
StorePath storePath = fastFileStorageClient.uploadFile(byteArrayInputStream, fileSize, extension, null);
System.out.println(storePath.getGroup()
.concat(":").concat(storePath.getPath())
.concat(":").concat(storePath.getFullPath()));
return storePath.getFullPath();
}
/**
* 下载文件
*
* @param fileUrl 文件路径
* @return 文件字节
*/
public byte[] downloadFile(String fileUrl) {
String group = fileUrl.substring(0, fileUrl.indexOf("/"));
String path = fileUrl.substring(fileUrl.indexOf("/") + 1);
DownloadByteArray downloadByteArray = new DownloadByteArray();
byte[] bytes = fastFileStorageClient.downloadFile(group, path, downloadByteArray);
return bytes;
}
}
4. 控制器类
package com.snf.controller;
import com.snf.service.FastDFSClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Objects;
@RequestMapping("/fastdfs")
@RestController
public class FastDFSController {
@Autowired
private FastDFSClientService fastDFSClientService;
//fastdfs上传接口
@RequestMapping("/upload")
public String uploadFile(MultipartFile file) throws IOException {
byte[] bytes = file.getBytes();
long size = file.getSize();
String originalFilename = file.getOriginalFilename();
String extension = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
String name = file.getName();
System.out.println(originalFilename
.concat(":").concat(name)
.concat(":").concat(String.valueOf(size))
.concat(":").concat(extension)
.concat(":").concat(String.valueOf(bytes.length)));
return fastDFSClientService.uploadFile(bytes, size, extension);
}
//fastdfs下载接口
@RequestMapping("/download")
public void downloadFile(String fileUrl, HttpServletResponse response) {
byte[] bytes = fastDFSClientService.downloadFile(fileUrl);
ServletOutputStream outputStream = null;
try {
response.setHeader("Content-disposition", "attachment;filename=".concat(URLEncoder.encode(fileUrl, "UTF-8")));
response.setCharacterEncoding("UTF-8");
outputStream = response.getOutputStream();
outputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (Objects.nonNull(outputStream)) {
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
5. 编写启动类
package com.snf;
import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;
//解决jmx重复注册bean的问题
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
@Import(FdfsClientConfig.class)
@SpringBootApplication
public class FastDFSApplication {
public static void main(String[] args) {
SpringApplication.run(FastDFSApplication.class, args);
}
}
6.使用postman进行测试