目录
项目结构
pom依赖
yml配置
config文件夹下的配置类
和文件服务器交互的类包装
demo编写
fastdfs基础知识点记录
项目结构
pom依赖
<!--fastdfs-->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.2</version>
</dependency>
<!--模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--devtools热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
yml配置
server:
port: 8081
spring:
application:
name: test-thymeleaf
mvc:
static-path-pattern: /static/**
resources:
static-locations: classpath:/static/
fdfs:
so-timeout: 1500 #socket连接超时时长
connect-timeout: 600 #连接tracker服务器超时时长
resHost: 192.168.239.129 #文件服务器的ip
storagePort: 80 #服务器nginx端口
pool:
jmx-enabled: true
thumb-image: #缩略图生成参数,可选
width: 100
height: 100
tracker-list: #TrackerList参数,支持多个,我这里只有一个,如果有多个在下方加- x.x.x.x:port
- 192.168.239.129:22122
config文件夹下的配置类
FastClientImporter
package com.ldgroup.ldtest.thymeleafbootstrap.config;
import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;
/**
* @Description
* @Author by mocar小师兄
* @Date 2020/4/14 0:21
**/
@Configuration
@Import(FdfsClientConfig.class)
// 解决jmx重复注册bean的问题
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FastClientImporter {
}
AppConfig
package com.ldgroup.ldtest.thymeleafbootstrap.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @Description
* @Author by mocar小师兄
* @Date 2020/4/14 0:22
**/
@Component
public class AppConfig {
@Value("${fdfs.resHost}")
private String resHost;
@Value("${fdfs.storagePort}")
private String storagePort;
public String getResHost() {
return resHost;
}
public void setResHost(String resHost) {
this.resHost = resHost;
}
public String getStoragePort() {
return storagePort;
}
public void setStoragePort(String storagePort) {
this.storagePort = storagePort;
}
}
和文件服务器交互的类包装
FastDFSClientWrapper
package com.ldgroup.ldtest.thymeleafbootstrap.utils;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.ldgroup.ldtest.thymeleafbootstrap.config.AppConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
/**
* @Description
* @Author by mocar小师兄
* @Date 2020/4/14 0:43
**/
@Component
public class FastDFSClientWrapper {
@Autowired
private AppConfig appConfig; // 项目参数配置
@Autowired
private FastFileStorageClient fastFileStorageClient;
public String uploadFile(MultipartFile file) throws IOException {
String fileName = file.getOriginalFilename();
String fileExtName = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
//上传文件并创建缩略图的方式上传
StorePath storePath = fastFileStorageClient.uploadFile
(file.getInputStream(),file.getSize(),fileExtName,null);
return getResAccessUrl(storePath);
}
// 封装图片完整URL地址
private String getResAccessUrl(StorePath storePath) {
String fileUrl = "http://" + appConfig.getResHost()
+ ":" + appConfig.getStoragePort() + "/" + storePath.getFullPath();
return fileUrl;
}
}
demo编写
测试类:
package com.ldgroup.ldtest.thymeleafbootstrap;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
/**
* @Description
* @Author by mocar小师兄
* @Date 2020/4/7 23:41
**/
@SpringBootTest
@RunWith(SpringRunner.class)
public class ThymeleafTest {
@Autowired
private FastFileStorageClient fastFileStorageClient;
@Test
public void test(){
System.out.println("test");
}
@Test
public void FastCliConnectionTest(){
File file = new File("C:\\Users\\mocar\\Desktop\\vue\\vue-router\\dashboard.png");
try {
String fileName = file.getName();
String fileExtName = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
//上传文件并创建缩略图的方式上传
StorePath storePath = fastFileStorageClient.uploadImageAndCrtThumbImage
(new FileInputStream(file),file.length(),fileExtName,null);
String fullPath = storePath.getFullPath();
String group = storePath.getGroup();
String path = storePath.getPath();
System.out.println( "fullPath:\t" + fullPath);
System.out.println( "group:\t" + group);
System.out.println( "path:\t" + path);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
参考:
结果:
fullPath: group1/M00/00/00/wKjvgV6UlR2AeTrbAAyRZZrP0gA250.png
group: group1
path: M00/00/00/wKjvgV6UlR2AeTrbAAyRZZrP0gA250.png
浏览器访问:
http://192.168.239.129/group1/M00/00/00/wKjvgV6UlR2AeTrbAAyRZZrP0gA250.png
页面跳转成功!
fastdfs基础知识点记录
执行流程:
client调用服务,发送http请求,到达服务器,Nginx监听到/group1/M00/的路径,交给Nginx_fdfs_model插件解析,
解析后找到tracker追踪器,tracker拿到storage_server集群服务器中的其中一个节点ip+端口返回给tracker,tracker再返回给client,
client拿到storage_server其中一个节点ip+端口后再上传到对应的存储节点上。上传成功后,storage返回路径
yml配置
连接超时时间,tracker_server,缩略图的大小,img_server服务器地址,用于上传的时候和返回的full_path路径拼接,用@value可以注入yml配置的属性
服务器的端口开放。tracker:22122 storage_server:23000
client是和先和tracker交互的,返回可用的storage存储节点,然后再直接上传到服务器上
前端引入js和样式
jquery uploadfify:有个问题,文件上传的服务单独作为一个服务启动,有自己的端口号,发送请求时怎么设置端口,只看到了设置路径
学习uploadfify的使用
参考:
http://www.jq22.com/jquery-info103
uploadfify文件:
https://github.com/SmallLeaves/Uploadify
回显需要两个控件
一个是<img> display=none,一个是<input type=text>,随提交按钮把数据提交到数据库
新建一个web层文件服务,
文件上传,删除,以及修改(新增文件和删除之前的文件)等
上传可以分为图片上传,和文件上传,根据后缀名ext识别,返回封装类
上传的接口方法参数要和前端发送请求时,设置的filename对的上
fastdfs引发的问题
1、垃圾文件:重新上传多分文件,无效文件垃圾的积累
2、安全问题的考虑:回显的<img>的src路径有文件服务器的ip,别人可以拿这个服务器当作自己的文件服务器上传东西
解决方案:验证只能自己的ip段上传东西
3、盗链的图片
用你的文件服务器的图片资源
解决方案:
1、图片+水印
2、显示此图片为盗链图片