1、整体结构:
2、配置文件:application.yml
fdfs:
connectTimeout: 600
trackerList:
- 192.168.231.142:22122
- 192.168.231.143:22122
- 192.168.231.144:22122
server:
port: 8080
3、FastDfsService:上传及下载方法
package com.cc.fastdfs.service;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@Component
public class FastDfsService {
@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() + ":" + storePath.getPath() + ":" + storePath.getFullPath());
return storePath.getFullPath();
}
/**
* 下载文件
*
* @param fileUrl 文件URL
* @return 文件字节
* @throws IOException
*/
public byte[] downloadFile(String fileUrl) throws IOException {
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、FastDfsController
package com.cc.fastdfs.controller;
import com.cc.fastdfs.service.FastDfsService;
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;
/**
* fastdfs测试
*/
@RestController
public class FastDfsController {
@Autowired
private FastDfsService fastDfsService;
@RequestMapping("/upload")
public String uploadFile(MultipartFile file) throws IOException {
byte[] bytes = file.getBytes();
String originalFileName = file.getOriginalFilename();
String extension = originalFileName.substring(originalFileName.lastIndexOf(".") + 1);
String fileName = file.getName();
long fileSize = file.getSize();
System.out.println(originalFileName + ":" + fileName + ":" + fileSize + ":" + extension + ":" + bytes.length);
return fastDfsService.uploadFile(bytes, fileSize, extension);
}
@RequestMapping("/download")
public void downloadFile(String fileUrl, HttpServletResponse response) throws IOException {
byte[] bytes = fastDfsService.downloadFile(fileUrl);
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileUrl, "UTF-8"));
response.setCharacterEncoding("UTF-8");
ServletOutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
outputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
5、FastDfsApplication
package com.cc.fastdfs;
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)
//只需要一行注解就可以拥有带有连接池的FastDFS Java客户端了
@Import(FdfsClientConfig.class)
@SpringBootApplication
public class FastDfsApplication {
public static void main(String[] args) {
SpringApplication.run(FastDfsApplication.class, args);
}
}
6、postman请求示例
上传:
下载: