项目实战
1 搭建图片服务器
1.1 Nginx模块安装 (Storage)
1.
上传
fastdfs-nginx-module_v1.16.tar.gz
到
/opt
2.
解压
nginx
模块
tar -zxvf fastdfs-nginx-module_v1.16.tar.gz
3.
修改
config
文件,将文件中的
/usr/local/
路径改为
/usr/
cd /opt/fastdfs-nginx-module/src
vim config
4.
将
fastdfs-nginx-module/src
下的
mod_fastdfs.conf
拷贝至
/etc/fdfs
下
cp mod_fastdfs.conf /etc/fdfs/
5.
修改
/etc/fdfs/mod_fastdfs.conf
vim /etc/fdfs/mod_fastdfs.conf
base_path=/home/fastdfs
tracker_server=192.168.58.222:22122
#(n个tracker配置n行)
#tracker_server=192.168.58.x:22122
#url中包含group名称
url_have_group_name=true
#指定文件存储路径(上面配置的store路径)
store_path0=/home/fastdfs/fdfs_storage
6.
将
libfdfsclient.so
拷贝至
/usr/lib
下
cp /usr/lib64/libfdfsclient.so /usr/lib/
7.
创建
nginx/client
目录
mkdir -p /var/temp/nginx/client
1.2 Nginx安装 (Tracker)
- 1. 将 nginx-1.14.0.tar.gz上传到/opt(安装过nginx,此步省略)
- 2. 解压:tar -zxvf nginx-1.14.0.tar.gz(安装过nginx,此步省略)
- 3. 安装依赖库(安装过nginx,此步省略)
yum install pcre
yum install pcre-devel
yum install zlib
yum install zlib-devel
yum install openssl
yum install openssl-devel
4.
进入
nginx
解压的目录下
cd /opt/nginx-1.14.0
5.
安装
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi \
--add-module=/opt/fastdfs-nginx-module/src
注意
:上边将临时文件目录指定为
/var/temp/nginx
,需要在
/var
下创建
temp
及
nginx
目录:
mkdir /var/temp/nginx
6.
编译:
make
7.
安装:
make install
8.
拷贝配置文件
cd /opt/FastDFS/conf
cp http.conf mime.types /etc/fdfs/
是否覆盖:yes
9.
修改
nginx
配置文件
cd /usr/local/nginx/conf/
vim nginx.conf
server {
listen 80;
server_name 192.168.58.222;
#charset koi8-r;
#access_log logs/host.access.log main;
location /group1/M00 {
root /home/fastdfs/fdfs_storage/data;
ngx_fastdfs_module;
}
}
10.
关闭
nginx
,并启动
nginx
pkill -9 nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
11.
访问
nginx
并查看图片
http://192.168.58.222
http://192.168.58.222/group1/M00/00/00/CgHc918f8l6AFYp0AAWICfQnHuk889.jpg
2 创建前端页面
<body>
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="fname"><br>
<button>提交</button>
</form>
</body>
3 搭建web服务
3.1 pom.xml
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<java.version>11</java.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<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>
<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>
3.2 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>
3.3 spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/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"
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>
3.4 文件实体类
public class FileSystem implements Serializable {
private String fileId;
private String filePath;
private String fileName;
}
3.5 控制层
package controller;
import entity.FileSystem;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import java.io.File;
import java.util.UUID;
/*
* 处理上传文件的控制器
* */
@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();
// 通过字符串截取的方式盲从文件原是名中获取文件的后缀
String hou = oldFileName.substring(oldFileName.lastIndexOf(".") + 1);
// 为了避免文件因为同名而覆盖,生成全新的文件名
String newFileName = UUID.randomUUID().toString() +"."+ hou;
// 创建web服务器保存文件的目录
File toSaveFile = new File("E:/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, hou, list);
trackerServer.close();
// 封装fileSystem数据对象
fileSystem.setFileId(fileId);
fileSystem.setFileName(oldFileName);
fileSystem.setFilePath(fileId);
return fileSystem;
}
}
3.6 添加fastDFS的配置文件
在resources下创建config目录,在config目录下创建 fastdfs-client.properties
##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=192.168.58.222:22122
3.7 启动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
[root@localhost /]# netstat -ntlp
[root@localhost /]# systemctl stop firewalld.service
[root@localhost /]# cd /home/fastdfs/fdfs_storage/data/
[root@localhost /]# ls
6 典型错误
- 1、重启linux服务器,可能会到nginx启动失败:
[root@localhost logs]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
[root@localhost /]# nginx: [emerg] open() "/var/run/nginx/nginx.pid" failed (2: No such file or directory)
- 导致本次错误的原因,是没有修改pid文件的路径,编辑nginx的配置文件:
vim /usr/local/nginx/conf/nginx.conf
pid /usr/local/nginx/logs/nginx.pid;
- 再次启动nginx,搞定!
- 2、如果在安装fastDFS之前已经安装了nginx 须重新编译,想看1.1->5
- 我这里提示编译失败 ,我的解决办法是删除nginx重新安装编译