在linux系统上安装 fastdfs
docker 官网
点击进入下载
docker images 查看 是否有容器
需要关闭 新形防火墙 或者 设置防火墙
find / -name selinux 可查看
看到 /etc/sysconfig/selinux
设置防火墙 请自行学习
编辑
vi /etc/sysconfig/selinux
SELINUX= 改 disabled
在启动容器
docker run --net host --name tracker 自定义名字 -itd 镜像名:版本 不加版本默认最新版
默认的端口: 22122
需要重启linux 系统
reboot 命令
docker run -tid --name storage -v ~/storage_data:/fastdfs/storage/data -v
~/store_path:/fastdfs/store_path --net=host -e TRACKER_SERVER:你的linux地址:22122 season/fastdfs storage
然后进入 tracker 的容器 找到 fsfd_conf 目录
fdfs_monitor 意思是监控 查看storage.conf目录
fdfs_monitor storage.conf
count:0 server_count:1 看到的参数是这样
退出 进入 storage 容器
进入tracker 同样的目录 看到 storage.conf文件修改他的ip 地址
//拷贝出来
docker cp storage:/fsfd_conf /storage.conf ~/
修改完在拷回去
docker cp ~/storage.conf storage:/fsfd_conf /storage.conf
然后进入tracker 容器查看 storage.conf
看到了 修改的 ip 地址
和group name =group1
就是修改成功了
然后启动
docker run -ti --name fdfs_sh --net=host season/fastdfs sh
需要 修改他的storage.conf 文件的ip 地址
他支持的命令
fdfs_appender_test
fdfs_appender_test1
fdfs_append_file
fdfs_crc32
fdfs_delete_file
fdfs_download_file
fdfs_file_info
fdfs_monitor 监控
fdfs_storaged
fdfs_test
fdfs_test1
fdfs_trackerd
fdfs_upload_appender
fdfs_upload_file 上传
可以上传一个文件
进入 fdfs_conf 目录
上传
fdfs_upload_file storage.conf 文件名
然后在linux ~目录找到 store-path 目录 data 00 00目录下就是你上传的文件
spring boot 集成 fastdfs-client
在gtishub 官网搜索fastdfs-client
jar包地址
需要 jar包
pom 文件
//导入Sonatype库
<repositories>
<repository>
<id>sfz</id>
<name>sfz</name>
<url>https://oss.sonatype.org/content/repositories/releases/</url>
</repository>
</repositories>
//spring mvn 的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring boot 集成 fsfd 依赖
//最好导Sonatype库
<dependency>
<groupId>com.luhuiguo</groupId>
<artifactId>fastdfs-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
main 方法
@SpringBootApplication
public class FsfdMain {
public static void main(String[] args) {
SpringApplication.run(FsfdMain.class);
}
}
html 上传
下载 下载application.yml文件
server:
port: 8889
fdfs:
connect-timeout: 10000
so-timeout: 3000
tracker-list:
- 192.168.241.131:22122
spring:
application:
name: 自己的名字
datasource:
url: jdbc:mysql://localhost/你的数据库
username: root
password:密码
driver-class-name: com.mysql.jdbc.Driver
http:
multipart:
//最大多少m
max-file-size: 10487540
下载类
@RestController
public class DsdfConfig {
@Autowired
protected FastFileStorageClient storageClient;
@Autowired
JdbcTemplate jdbcTemplate;
/**
*
* @param myFile 从浏览器提交过来
* @return
* @throws IOException
*/
@PostMapping("/fupload")
public String upload(@RequestParam("myFile") MultipartFile myFile) throws IOException {
//获取文件原来的名字
String extName= FilenameUtils.getExtension(myFile.getOriginalFilename());
//获取连接
StorePath sp=storageClient.uploadFile("group1",myFile.getInputStream(),myFile.getSize(),extName);
//sql语句
String sql="insert into fliegroud(filename,gruoname,filepath) values(?,?,?)";
//插入到数据库
jdbcTemplate.update(sql,myFile.getOriginalFilename(),sp.getGroup(),sp.getPath());
//返回文件的名字
return sp.getFullPath();
}
@GetMapping("/fdownload/{id}")
public void download(@PathVariable String id, HttpServletResponse response) throws IOException {
//获取数据库里面id=1的数据
List list =jdbcTemplate.query("select *from fliegroud where id="+id,new ColumnMapRowMapper());
//拿到list 里面下标为0的数据
Map map =(Map)list.get(0);
//把名字改为utf-8
String fileName= URLEncoder.encode(map.get("filename").toString(),"UTF-8");
//把名字改为string
String groupName=map.get("gruoname").toString();
//把名字改为string
String fileiPath=map.get("filepath").toString();
//告诉浏览器 下载的文件名
response.setHeader("Content-Disposition","attachment; filename="+fileName+"");
//将文件的内容输出到浏览器中
byte[] bt= storageClient.downloadFile(groupName,fileiPath);
response.getOutputStream().write(bt);
}