1.安装配置nginx
1.1.查看nginx镜像
[root@localhost ~]# docker search nginx
1.2.拉取nginx镜像
[root@localhost ~]# docker pull nginx
1.3.查询本地镜像
[root@localhost ~]# docker images
1.4.创建主要文件,用于docker挂载
[root@localhost ~]# mkdir -p /home/nginx/www /home/nginx/logs /home/nginx/conf
1.5.创建并运行nginx容器
[root@localhost ~]# docker run --name nginx-tmp -p 8888:80 -d nginx
拷贝nginx的配置到本地目录的 /home/nginx目录下
[root@localhost home]# docker cp 782418ae048f:/etc/nginx/nginx.conf /home/nginx/conf/nginx.conf
删除nginx-tmp容器。
[root@localhost home]# docker stop nginx-tmp
nginx-tmp
[root@localhost home]# docker rm nginx-tmp
nginx-tmp
并且重新运行nginx容器
docker run --name nginx -p 8888:80 -v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/nginx/www:/usr/share/nginx/html -v /home/nginx/logs:/var/log/nginx --privileged=true -d nginx
1.6.修改nginx配置文件
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
#解决跨域访问问题
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
add_header Access-Control-Allow-Headers DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type;
}
2.安装配置vsftpd
https://www.jianshu.com/p/c84f6e6de002
2.1.安装vsftpd
yum -y install vsftpd
2.2.添加访问ftp的用户和密码
useradd 用户名
[root@localhost www]# useradd ty
passwd 用户名
[root@localhost /]# passwd ty
更改用户 ty 的密码 。
新的密码:需要输入两次密码
2.3.修改配置文件
vi /etc/vsftpd/vsftpd.conf
# 配置文件默认为YES,即支持匿名访问,设置为不支持
anonymous_enable=NO
# 在最后添加这两行
pasv_min_port=30000
pasv_max_port=30009
2.4.给用户设置访问文件权限
chown ty /home/nginx/www/images
chmod 777 -R /home/nginx/www/images
2.5.设置外部可以通过ftp访问本机
[root@localhost ~]# setsebool -P allow_ftpd_full_access on
3.编写FTP工具类
3.1.添加依赖
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
3.2.编写代码
FtpUtil.java
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import java.io.*;
/**
* @author FuBo
*/
public class FtpUtil {
/**
* ftpAddress: ftp 服务器ip地址
* ftpPort: ftp 服务器port,默认是21
* ftpUsername: ftp 服务器用户名
* ftpPassword: ftp 服务器密码
* ftpBasePath: ftp 服务器存储图片的绝对路径
* imageBaseUrl: ftp 服务器外网访问图片路径
*/
private String ftpAddress="192.168.0.110";
private Integer ftpPort=21;
private String ftpUsername="ty";
private String ftpPassword="ty123456";
private String ftpBasePath="/home/nginx/www/images";
private String imageBaseUrl="https://192.168.0.110:8888/images/";
/**
* 上传文件
* @param inputStream 输入流
* @param name 文件名
* @return 图片 url
* @throws IOException IO异常
*/
public String uploadFile(InputStream inputStream, String name) throws IOException {
FTPClient ftpClient = new FTPClient();
try {
//设置获取数据为被动模式
ftpClient.enterLocalPassiveMode();
//连接ftp服务器
ftpClient.connect(ftpAddress, ftpPort);
//登陆ftp服务器
ftpClient.login(ftpUsername, ftpPassword);
//设置上传文件的目录
ftpClient.changeWorkingDirectory(ftpBasePath);
//设置上传文件的类型
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
//设置编码格式
ftpClient.setControlEncoding("UTF-8");
//上传文件
boolean isSucceed = ftpClient.storeFile(name, inputStream);
if (isSucceed){
return imageBaseUrl + name;
}
}catch (Exception e){
e.printStackTrace();
}finally {
ftpClient.logout();
}
return imageBaseUrl + "error";
}
/**
* 下载文件
* @param fileName 下载的文件名字
* @param localPath 下载到本地的目录
* @return
*/
public boolean downloadFile(String fileName, String localPath) {
boolean result = false;
FTPClient ftpClient = new FTPClient();
try {
//连接ftp服务器
ftpClient.connect(ftpAddress, ftpPort);
//登陆ftp服务器
ftpClient.login(ftpUsername, ftpPassword);
//设置上传文件的目录
ftpClient.changeWorkingDirectory(ftpBasePath);
File tempFile = new File(localPath);
if (!tempFile.exists()){
tempFile.mkdirs();
}
FTPFile[] fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
File localFile = new File(localPath + "/" + ff.getName());
OutputStream is = new FileOutputStream(localFile);
ftpClient.retrieveFile(ff.getName(), is);
is.close();
}
}
ftpClient.logout();
result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
}
}
}
return result;
}
public static void main(String[] args) {
/*File file = new File("F:/ww.png");
System.out.println(file.length());
try {
FileInputStream inputStream = new FileInputStream(file);
new FtpUtil().uploadFile(inputStream,"ww.png");
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}*/
new FtpUtil().downloadFile("zhiyun.sql","d:/sql/");
}
}