1.文件上传
结构如下:
(1)导入依赖包
? <dependency> ? ? ? ? ? ? <groupId>net.oschina.zcx7878</groupId> ? ? ? ? ? ? <artifactId>fastdfs-client-java</artifactId> ? ? ? ? ? ? <version>1.27.0.0</version> ?</dependency>
(2)?fdfs_client.conf文件
# connect_timeout:连接超时时间,单位为秒。
# network_timeout:通信超时时间,单位为秒。发送或接收数据时。假设在超时时间后还不能发送或接收数据,则本次网络通信失败
# charset: 字符集
# http.tracker_http_port :tracker的http端口
# tracker_server: tracker服务器IP和端口设置
connect_timeout=60
network_timeout=60
charset=UTF-8
http.tracker_http_port=8080
tracker_server=192.168.2.2:22122
(3)application.yml文件
spring:
servlet:
multipart:
max-file-size: 10MB
max-request-size: 10MB
application:
name: file
server:
port: 18082
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:7001/eureka
instance:
prefer-ip-address: true
feign:
hystrix:
enabled: true
(4)主类
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) //排除掉数据库自动加载
@EnableEurekaClient
public class FileApplication {
public static void main(String[] args) {
SpringApplication.run(FileApplication.class,args);
}
}
(5)FastDFSUtil类
package com.changgou.util;
import com.changgou.file.FastDFSFile;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.core.io.ClassPathResource;
import java.io.IOException;
/**
* 实现FastDFS文件管理
* 文件上传
* 文件删除
* 文件下载
* 文件信息获取
* storage信息获取
* tracker信息获取
*/
public class FastDFSUtil {
/**
* 加载Tracker链接信息
*/
static {
try {
//查找classpath下的文件路径
String filename = new ClassPathResource("fdfs_client.conf").getPath();
ClientGlobal.init(filename);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 文件上传
*/
public static String[] upload(FastDFSFile fastDFSFile) throws Exception {
//创建一个Tracker访问的客户端对象TrackerClient
TrackerClient trackerClient = new TrackerClient();
//通过TrackerClient访问TrackerServer服务,获取连接信息
TrackerServer trackerServer = trackerClient.getConnection();
//通过TrackerServer的链接信息可以获取Storage的链接信息,创建StorageClient对象存储Storage的链接信息
StorageClient storageClient = new StorageClient(trackerServer, null);
/**
* 通过StorageClient访问Storage,实现文件上传,并且获取文件上传后的存储信息
* 三个参数:
* 上传文件的字节数组
* 文件的扩展名
* 附加参数
*
*/
String[] uploads = storageClient.upload_file(fastDFSFile.getContent(), fastDFSFile.getExt(), null);
/**
* 返回的数组中
* [0];文件上传所存储的Storage 的组的名字 group1
* [1]:文件存储到Storage上的文件名字 M00/.....
*/
return uploads;
}
}
(6)FastDFSFile类
package com.changgou.file;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* 封装文件上传信息:
* 时间、上传人、类型、大小、附加信息、后缀、文件内容(文件的字节数组)。。。。
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class FastDFSFile implements Serializable {
//文件名字
private String name;
//文件内容
private byte[] content;
//文件扩展名
private String ext;
//文件MD5摘要值
private String md5;
//文件创建作者
private String author;
public FastDFSFile(String name, byte[] content, String ext) {
this.name = name;
this.content = content;
this.ext = ext;
}
}
(7)FileUploadController类
package com.changgou.controller;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.file.FastDFSFile;
import com.changgou.util.FastDFSUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequestMapping("/upload")
@CrossOrigin
public class FileUploadController {
/**
* 文件上传
*/
@PostMapping
public Result upload(@RequestParam("file") MultipartFile multipartFile) throws Exception {
FastDFSFile fastDFSFile = new FastDFSFile(
multipartFile.getOriginalFilename(),//文件名字 比如1.jpg
multipartFile.getBytes(), //文件字节数组
StringUtils.getFilenameExtension(multipartFile.getOriginalFilename()) //获取文件扩展名
);
String[] upload = FastDFSUtil.upload(fastDFSFile);
//拼接访问地址
String url="http://192.168.2.2/"+upload[0]+"/"+upload[1];
return new Result(true, StatusCode.OK,"上传成功!",url);
}
}
(8)测试上传
当然前提是linux系统上的fastdfs服务都开启了,包括nginx也开启了
?(9)浏览器访问得到的存储的路径
总结:
完成这些过程需要开启:
pkill -9 fdfs
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf
#开启nginx
去 nginx/sbin/./nginx
2.获取文件信息
/**
* 获取文件信息
* @param groupName :文件的组名
* @param remoteFileName :文件的存储路径名
* @return
*/
public static FileInfo getFile(String groupName, String remoteFileName) throws Exception {
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageClient storageClient = new StorageClient(trackerServer, null);
//获取文件信息
FileInfo fileInfo = storageClient.get_file_info(groupName, remoteFileName);
return fileInfo;
}
/**
* 测试获取文件信息
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//group1/M00/00/00/wKgCAmKRBj-AEEO0AADlvLiT6Tw558.jpg
FileInfo fileInfo = getFile("group1", "M00/00/00/wKgCAmKRBj-AEEO0AADlvLiT6Tw558.jpg");
System.out.println("文件大小====="+fileInfo.getFileSize());
System.out.println("创建时间======"+fileInfo.getCreateTimestamp());
System.out.println("资源ip地址======="+fileInfo.getSourceIpAddr());
//文件大小=====58812
//创建时间======Sat May 28 01:11:27 CST 2022
//资源ip地址=======192.168.2.2
}
3.文件下载
/**
* 文件下载
* @param groupName:文件的组名
* @param remoteFilename:文件的存储路径名
* @throws Exception
*/
public static InputStream downloadFile(String groupName, String remoteFilename) throws Exception {
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageClient storageClient = new StorageClient(trackerServer, null);
byte[] filebytes = storageClient.download_file(groupName, remoteFilename);
ByteArrayInputStream inputStream = new ByteArrayInputStream(filebytes);
return inputStream;
}
package com.changgou.controller;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.util.FastDFSUtil;
import org.springframework.web.bind.annotation.*;
import java.io.FileOutputStream;
import java.io.InputStream;
/**
* 文件下载
*/
@RestController
@RequestMapping("/download")
@CrossOrigin
public class downloadFileController {
@PostMapping
public Result downFile(@RequestParam("groupName") String groupName,
@RequestParam("remoteFileName") String remoteFileName) throws Exception {
InputStream is = FastDFSUtil.downloadFile(groupName, remoteFileName);
//将文件写入到本地磁盘
FileOutputStream os = new FileOutputStream("G:/1.jpg");
//定义一个空字节数组
byte[] bytes = new byte[1024];
while(is.read(bytes)!=-1){
os.write(bytes);
}
os.flush();
os.close();
is.close();
return new Result<>(true, StatusCode.OK,"下载成功!");
}
}
测试运行:
?
4.文件删除
/**
* 文件删除
* @param groupName:文件的组名
* @param remoteFilename:文件的存储路径名
* @throws Exception
*/
public static void deleteFile(String groupName, String remoteFilename) throws Exception {
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageClient storageClient = new StorageClient(trackerServer, null);
//删除文件
storageClient.delete_file(groupName, remoteFilename);
}
|