文章目录
搭建图片服务器
Nginx模块安装(Storage)
# 上传 fastdfs-nginx-module_v1.16.tar.gz 到 /opt 目录下(压缩包请自行下载)
# 解压
tar -xf fastdfs-nginx-module_v1.16.tar.gz
# 修改 config 文件,将文件中的 /usr/local/ 路径改为 /usr/
vim /opt/fastdfs-nginx-module/src/config
# 将 fastdfs-nginx-module/src下的 mod_fastdfs.conf 拷贝至 /etc/fdfs 下
cp mod_fastdfs.conf /etc/fdfs
# 修改 /etc/fdfs/mod_fastdfs.conf
vim /etc/fdfs/mod_fastdfs.conf
base_path=/home/fastdfs
tracker_server=:22122
# (n个tracker配置n行)
# tracker_server=10.1.220.x:22122
# url中包含group名称
url_have_group_name=true
# 指定文件存储路径(配置的store路径)
store_path0=/home/fastdfs/fdfs_storage
# 将 libfdfsclient.so 拷贝至 /usr/lib 下
cp /usr/lib64/libfdfsclient.so /usr/lib/
# 创建nginx/client目录
mkdir -p /var/temp/nginx/client
Nginx安装(Tracker)
- 安装Nginx:参考链接
- Nginx安装之后进行以下操作
# 拷贝配置文件
cd /opt/FastDFS/conf
cp http.conf mime.types /etc/fdfs/ # 是否覆盖:yes
# 修改nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name 10.1.220.247;
#charset koi8-r;
#access_log logs/host.access.log main;
location /group1/M00 {
root /home/fastdfs/fdfs_storage/data;
ngx_fastdfs_module;
}
# 重启Nginx
pkill -9 nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
创建前端页面
index.jsp
<%--上传文件,文件与文字相比较起来,内容较大,必须使用post方式提交--%>
<%--上传文件,和普通文件有区别,action接收参数也会区别对待,所以声明带文件提交的表单为"多部件表单"--%>
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="fname">
<br>
<button>提交</button>
</form>
搭建web服务
pom.xml
<!-- 打成war包 -->
<packaging>war</packaging>
<dependencies>
<!-- 因为有jsp页面,所以引用servlet依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
<version>2.5</version>
</dependency>
<!-- 页面提交过来的请求,使用springmvc来处理-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<!-- java连接fastDFS的客户端工具-->
<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27.0.0</version>
</dependency>
<!-- 图片上传到FastDFS需要用的到IO工具-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<!-- 图片保存到web服务器需要用到的IO工具-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<!--用来转换java对象和json字符串,注意,2.7以上版本必须搭配spring5.0以上-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
</dependencies>
<!-- 使用插件将Tomcat内嵌到web项目中 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<port>8001</port>
<path>/</path>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--扫描注解的包-->
<context:component-scan base-package="controller"/>
<!--扫描控制器中的注解:@Response-->
<mvc:annotation-driven/>
<!--上传文件的解析器(规定上传文件的大小限制)-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 上传文件最大限制:2GB-->
<property name="maxUploadSize" value="2048000000"/>
</bean>
</beans>
文件实体类
public class FileSystem implements Serializable {
private String fileId;
private String filePath;
private String fileName;
}
控制层
@Controller
public class FileAction {
/**
* @param request 多部件表单的请求对象
* @return 上传文件对象的json对象
*
* 上传文件的流程:
* 1.把文件保存到web服务器
* 2.从web服务器将文件上传到FastDFS
*/
@RequestMapping("upload")
// MultipartHttpServletRequest:是httpservletRequest的强化版本,不仅可以装文本信息,还可以装图片文件信息
public @ResponseBody FileSystem upload(MultipartHttpServletRequest request) throws Exception {
FileSystem fileSystem = new FileSystem();
/* 1.把文件保存到web服务器*/
// 从页面请求中,获取上传的文件对象
MultipartFile file = request.getFile("fname");
// 从文件对象中获取 文件的原始名称
String oldFileName = file.getOriginalFilename();
// 通过字符串截取的方式,从文件原始名中获取文件的后缀 1. jpg
String suffix = oldFileName.substring(oldFileName.lastIndexOf(".") + 1);
// 为了避免文件因为同名而覆盖,生成全新的文件名
String newFileName = UUID.randomUUID().toString() + "." + suffix;
// 创建web服务器保存文件的目录(预先创建好D:/upload目录,否则系统找不到路径,会抛出异常)
File toSaveFile = new File("D:/upload/" + newFileName);
// 将路径转换成文件
file.transferTo(toSaveFile);
// 获取服务器的绝对路径
String newFilePath = toSaveFile.getAbsolutePath();
/* 2、把文件从web服务器上传到FastDFS*/
ClientGlobal.initByProperties("config/fastdfs-client.properties");
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient1 client = new StorageClient1(trackerServer, storageServer);
NameValuePair[] list = new NameValuePair[1];
list[0] = new NameValuePair("fileName", oldFileName);
String fileId = client.upload_file1(newFilePath, suffix, list);
trackerServer.close();
// 封装fileSystem数据对象
fileSystem.setFileId(fileId);
fileSystem.setFileName(oldFileName);
fileSystem.setFilePath(fileId);
// 已经上传到FastDFS上,通过fileId来访问图片,所以fileId即为文件路径
return fileSystem;
}
}
添加fastDFS配置文件
fastdfs-client.properties:
fastdfs.connect_timeout_in_seconds = 5
fastdfs.network_timeout_in_seconds = 30
fastdfs.charset = UTF-8
fastdfs.http_anti_steal_token = false
fastdfs.http_secret_key = FastDFS1234567890
fastdfs.http_tracker_http_port = 80
fastdfs.tracker_servers = 10.1.220.247:22122
启动FastDFS服务,开始测试
[root@localhost /] /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
[root@localhost /] /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
[root@localhost /] /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf