Nginx搭建文件服务器实现文件上传
安装Nginx
-
去官网http://nginx.org/下载对应的nginx包,推荐使用稳定版本
-
上传nginx到linux系统
-
安装依赖环境
(1)安装gcc环境
yum install gcc-c++
(2)安装PCRE库,用于解析正则表达式
yum install -y pcre pcre-devel
(3)zlib压缩和解压缩依赖,
yum install -y zlib zlib-devel
(4)SSL 安全的加密的套接字协议层,用于HTTP安全传输,也就是https
yum install -y openssl openssl-devel
-
解压,需要注意,解压后得到的是源码,源码需要编译后才能安装
tar -zxvf nginx-1.16.1.tar.gz
-
编译之前,先创建nginx临时目录,如果不创建,在启动nginx的过程中会报错
mkdir /var/temp/nginx -p
-
在nginx目录,输入如下命令进行配置,目的是为了创建makefile文件
./configure \n --prefix=/usr/local/nginx \n --pid-path=/var/run/nginx/nginx.pid \n --lock-path=/var/lock/nginx.lock \n --error-log-path=/var/log/nginx/error.log \n --http-log-path=/var/log/nginx/access.log \n --with-http_gzip_static_module \n --http-client-body-temp-path=/var/temp/nginx/client \n --http-proxy-temp-path=/var/temp/nginx/proxy \n --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \n --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \n --http-scgi-temp-path=/var/temp/nginx/scgi
-
配置命令:
命令 解释 –prefix 指定nginx安装目录 –pid-path 指向nginx的pid –lock-path 锁定安装文件,防止被恶意篡改或误操作 –error-log 错误日志 –http-log-path http日志 –with-http_gzip_static_module 启用gzip模块,在线实时压缩输出数据流 –http-client-body-temp-path 设定客户端请求的临时目录 –http-proxy-temp-path 设定http代理临时目录 –http-fastcgi-temp-path 设定fastcgi临时目录 –http-uwsgi-temp-path 设定uwsgi临时目录 –http-scgi-temp-path 设定scgi临时目录
-
-
make编译
make
-
安装
make install
-
进入sbin目录启动nginx
./nginx
- 停止:./nginx -s stop
- 重新加载:./nginx -s reload
-
修改配置文件
切换至安装目录
cd /usr/local/nginx/conf
修改文件
vim nginx.conf
主要是修改server配置节的内容,配置文件路径
location ~.* { #可识别的文件 root /usr/local/nginx/image/; #图片的映射路径 autoindex on; #开启自动索引 expires 1h; #过期时间 }
重启nginx
./nginx -s reload
上传文件功能实现
pom.xml
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.54</version>
</dependency>
application.yml
# ftp 服务器配置
ftp:
# 服务器地址
fileUrl: http://10.10.10.15:80/
# 服务器ip
host: 10.10.10.15
# 服务器登录用户名
userName: root
# 服务器密码
password: Sq6sQJXDyTQo
# 服务器端口号
port: 22
# 文件存储路径
rootPath: /usr/local/nginx/file
核心工具类
@Component
public class FtpUtil {
private static final Logger logger = LoggerFactory.getLogger(FtpUtil.class);
/**
* ftp服务器ip地址
*/
private static String host;
/**
* 端口
*/
private static int port;
/**
* 用户名
*/
private static String userName;
/**
* 密码
*/
private static String password;
/**
* 存放文件的根目录
*/
private static String rootPath;
/**
* 存放文件的路径
*/
private static String fileUrl;
@Value("${ftp.host}")
public void setHost(String host) {
FtpUtil.host = host;
}
@Value("${ftp.port}")
public void setPort(int port) {
FtpUtil.port = port;
}
@Value("${ftp.userName}")
public void setUserName(String userName) {
FtpUtil.userName = userName;
}
@Value("${ftp.password}")
public void setPassword(String password) {
FtpUtil.password = password;
}
@Value("${ftp.rootPath}")
public void setRootPath(String rootPath) {
FtpUtil.rootPath = rootPath;
}
@Value("${ftp.fileUrl}")
public void setFileUrl(String fileUrl) {
FtpUtil.fileUrl = fileUrl;
}
private static ChannelSftp getChannel() throws Exception {
JSch jsch = new JSch();
// ->ssh root@host:port
Session sshSession = jsch.getSession(userName, host, port);
// 密码
sshSession.setPassword(password);
Properties sshConfig = new Properties();
// 设置第一次登陆的时候提示,可选值:(ask | yes | no)
sshSession.setConfig("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
//设置登陆超时时间
// 注意!!这里不设置超时间会报错
sshSession.connect(60000);
Channel channel = sshSession.openChannel("sftp");
channel.connect(1000);
return (ChannelSftp) channel;
}
/**
* ftp上传文件
*
* @param inputStream 文件io流
* @param imagePath 路径,不存在就创建目录
* @param imagesName 文件名称
* @return urlStr 文件的存放路径
*/
public static String putFile(InputStream inputStream, String fileName, String type) {
try {
ChannelSftp sftp = getChannel();
String path = rootPath + "/" + type + "/";
createDir(path, sftp);
// 上传文件
sftp.put(inputStream, path + fileName);
logger.info("上传成功!");
sftp.quit();
sftp.exit();
// 处理返回的路径
String resultFile;
resultFile = fileUrl + type + "/" + fileName;
return resultFile;
} catch (Exception e) {
logger.error("上传失败:" + e.getMessage());
}
return "";
}
/**
* 创建目录
*/
private static void createDir(String path, ChannelSftp sftp) throws SftpException {
String[] folders = path.split("/");
sftp.cd("/");
for (String folder : folders) {
if (folder.length() > 0) {
try {
sftp.cd(folder);
} catch (SftpException e) {
sftp.mkdir(folder);
sftp.cd(folder);
}
}
}
}
/**
* 删除文件
*/
public static void delFile(String fileName,String type) {
try {
ChannelSftp sftp = getChannel();
String path = rootPath + "/" + type + "/" + fileName;
sftp.rm(path);
sftp.quit();
sftp.exit();
} catch (Exception e) {
logger.error(" 删除失败:" + e.getMessage());
}
}
}