为什么要使用FastDFS来管理图片?
我们先来看一下传统体系只有一个tomcat时图片的处理方式:
传统的处理方式就是将图片直接存放在tomcat服务器里面,在并发量不高的单台tomcat时是可行的。
由于单台tomcat服务器最多支持500的连接,随着并发量的增加,我们需要水平扩展,比如:使用nginx做负载均衡搭建tomcat集群。
此时如果用户上传图片,nginx将图片上传转发给tomcat1,然后用户访问图片时,nginx将访问图片的请求转发给tomcat2,用户就访问不到图片。
所以我们使用FastDFS分布式文件系统,来专门管理图片(或者文件)。
FastDFS安装步骤 https://blog.csdn.net/pdsu161530247/article/details/81743889
用户上传图片请求都通过nginx交给tomcat服务器,由tomcat服务器将图片保存在FastDFS图片服务器里面。
FastDFS只是管理图片的系统,所以我们访问图片需要借助http服务器(比如:nginx),所以FastDFS上还需要安装一个http服务器。用户访问图片请求都通过nginx交给FastDFS上的http服务器,由FastDFS上的http服务器返回图片交给nginx,nginx在交给用户。
使用FastDFSClient工具类上传图片到FastDFS服务器
将fastdfs_client(fastdfs_client下载链接)打包安装,然后在pom.xml中引入
<!-- 图片服务器 -->
<dependency>
<groupId>fastdfs_client</groupId>
<artifactId>fastdfs_client</artifactId>
<version>1.25</version>
</dependency>
fast_dfs.conf:
tracker_server=192.168.1.5:22122
FastDFSClient工具类:
package com.taotao.utils;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
public class FastDFSClient {
private TrackerClient trackerClient = null;
private TrackerServer trackerServer = null;
private StorageServer storageServer = null;
private StorageClient1 storageClient = null;
public FastDFSClient(String conf) throws Exception {
/**
* 如果用户传入的文件路径是相对路径(相对路径以src/main/resources目录为根目录),
* 比如用户传入的文件路径是”classpath:applications.properties”,
* 那么需要转为绝对路径,因此需要把”classpath:”给替换掉,改为F:/Java/my-taotao/taotao-manager-web/src/main/resources。
* 而且封装类中使用的Storage客户端是StorageClient1而不是StorageClient。
* 这个客户端的好处是能够帮我们自动把文件所在的组以及存放位置拼接到一块。
*/
if (conf.contains("classpath:")) {
conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());
}
ClientGlobal.init(conf);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageServer = null;
storageClient = new StorageClient1(trackerServer, storageServer);
}
/**
* 上传文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
* @param fileName 文件全路径
* @param extName 文件扩展名,不包含(.)
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
String result = storageClient.upload_file1(fileName, extName, metas);
return result;
}
public String uploadFile(String fileName) throws Exception {
return uploadFile(fileName, null, null);
}
public String uploadFile(String fileName, String extName) throws Exception {
return uploadFile(fileName, extName, null);
}
/**
* 上传文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
* @param fileContent 文件的内容,字节数组
* @param extName 文件扩展名
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception {
String result = storageClient.upload_file1(fileContent, extName, metas);
return result;
}
public String uploadFile(byte[] fileContent) throws Exception {
return uploadFile(fileContent, null, null);
}
public String uploadFile(byte[] fileContent, String extName) throws Exception {
return uploadFile(fileContent, extName, null);
}
public static void main(String[] args) throws Exception {
FastDFSClient FastDFSClient = new FastDFSClient("classpath:resource/client.conf");
String uploadFile = FastDFSClient.uploadFile("C:\\Users\\Administrator\\Desktop\\1.jpg");
System.out.println(uploadFile);
}
}
运行测试,上传成功
访问图片
fastdfs服务器ip+group1/M00/00/00/rBPrOVt29nCAQyTQAAAohYR4kys663.jpg即可访问。