UploadController
@EnableSwagger2
@RestController
@RequestMapping("/request")
@Api(description = "fdfs文件上传与下载")
public class UploadController {
private static final String URL = "classpath:fdfs_client.conf";
@PostMapping("/uploadFile")
@ApiOperation("fdfs方法上传")
public String uploadFile(@RequestParam("file") MultipartFile uploadFile) throws Exception {
//文件名
String originalName = uploadFile.getOriginalFilename();
//拓展名
String name = originalName.substring(originalName.lastIndexOf('.')+1);
String path = FastDFSUtils.upload_file(uploadFile.getBytes(), originalName, uploadFile.getSize());
return path;
}
//没实现选定下载地址
@GetMapping("/downLoadFile")
@ApiOperation("fdfs方法下载")
public String downLoadFile() throws Exception {
byte[] bytes = FastDFSUtils.download_file("group1", "M00/00/00/wKjngl_wTvWAIDq-AASE-wcdb3Y241.jpg");
// 1.用日期命名文件
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String fileName = dateFormat.format(now) + ".jpg";
System.out.println(fileName);
// 2.转为图片jpg格式
//()中是下载到的地址
FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\LZY\\Downloads\\"+fileName);
fileOutputStream.write(bytes);
fileOutputStream.close();
return "下载成功";
}
@GetMapping("/deleteFile")
@ApiOperation("fdfs方法删除")
public String deleteFile() throws Exception {
FastDFSUtils.delete_file("group1", "M00/00/00/wKjngl_u13iAGTRcAACMjz-Fy6g372.jpg");
return "删除成功";
}
}
FastDFSUtils
public class FastDFSUtils {
static {
try {
//ClientGloble 读配置文件
ClassPathResource resource = new ClassPathResource("fdfs_client.conf");
ClientGlobal.init(resource.getClassLoader().getResource("fdfs_client.conf").getPath());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param pic 文件二进制
* @param name 文件名称
* @param size 文件大小
* @return group1/M00/00/01/wKjIgFWOYc6APpjAAAD-qk29i78248.jpg
* @Description: 上传文件
*/
public static String upload_file(byte[] pic, String name, long size) {
String path = null;
try {
//客户端
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient1 storageClient1 = new StorageClient1(trackerServer, storageServer);
//文件11.jpg 根据文件名称得到文件后缀 jpg
String ext = FilenameUtils.getExtension(name);
NameValuePair[] meta_list = new NameValuePair[3];
meta_list[0] = new NameValuePair("fileName", name);
meta_list[1] = new NameValuePair("fileExt", ext);
meta_list[2] = new NameValuePair("fileSize", String.valueOf(size));
// group1/M00/00/01/wKjIgFWOYc6APpjAAAD-qk29i78248.jpg
path = storageClient1.upload_file1(pic, ext, meta_list);
} catch (Exception e) {
e.printStackTrace();
}
return path;
}
/**
* 文件下载
* return:文件字节码
* group_name:group1
* romte_fileName:/opt/fastdfs/storage/files/data/00/00/wKjngl_u0YmANIh-AACMjz-Fy6g293.jpg
* M00/00/00/wKjng1_u5x6AShRRAACMjz-Fy6g127.jpg
*/
public static byte[] download_file(String group_name, String romte_fileName) throws Exception {
/** 创建存储客户端对象 */
StorageClient storageClient = new StorageClient();
/**
* 文件下载
*/
byte[] bytes = storageClient.download_file(group_name, romte_fileName);
return bytes;
}
/**
* 删除文件
* return:0代表删除成功
* group_name:group1
* romte_fileName:M00/00/01/wKjIgFWOYc6APpjAAAD-qk29i78248.jpg
*/
public static int delete_file(String group_name, String romte_fileName) throws Exception {
/** 创建存储客户端对象 */
StorageClient storageClient = new StorageClient();
/** 删除文件 */
int res = storageClient.delete_file(group_name, romte_fileName);
return res;
}
}
fdfs_client.conf
tracker_server=192.168.231.128:22122
tracker_server=192.168.231.129:22122