当前位置:   article > 正文

linux下使用nginx和ftp做图片服务器_nginx 远程图片

nginx 远程图片

一、搭建nginx

1.1 安装gcc、pcre、zlib、openssl环境

1.1.1  gcc

yum install gcc-c++ 

1.1.2  pcre

yum install -y pcre pcre-devel

1.1.3  zlib

yum install -y zlib zlib-devel

1.1.4 openssl

yum install -y openssl openssl-devel

1.2 安装nginx

1.2.1 上传nginx-1.8.0.tar.gz到指定目录,并解压

个人习惯: 上传至/usr/local/env/nginx路径下
[root@localhost ~]# cd /usr/local/env/nginx/
[root@localhost nginx]# tar -zxvf nginx-1.8.0.tar.gz -C /usr/local/env/nginx/

1.2.2 configure

[root@localhost nginx]# cd nginx-1.8.0
[root@localhost nginx-1.8.0]# ./configure

1.2.3 编译、安装

[root@localhost nginx-1.8.0]# make

此时会生成makefile文件,再install

[root@localhost nginx-1.8.0]# make install

此时,会在/usr/local/路径下生成nginx相关文件夹(注:此路径可以修改,可通过configure配置文件来修改生成位置)


1.2.4 启动并访问nginx主页

[root@localhost nginx-1.8.0]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ./nginx -c /usr/local/nginx/conf/nginx.conf

注: 可以通过-c来指定nginx启动的配置文件nginx.conf

默认的nginx.conf会通过配置,将访问路径/映射到自己的html文件夹下的index.html主页上


访问主页,如果看到欢迎页,说明安装nginx成功.


1.2.5 停止nginx

方式1,快速停止:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

  1. [root@localhost sbin]# pwd
  2. /usr/local/nginx/sbin
  3. [root@localhost sbin]# ./nginx -s stop
  4. [root@localhost sbin]#

方式2,完整停止(建议使用)此方式停止步骤是待nginx进程处理任务完毕进行停止。

  1. [root@localhost sbin]# pwd
  2. /usr/local/nginx/sbin
  3. [root@localhost sbin]# ./nginx -s quit
  4. [root@localhost sbin]#

1.2.6 重启nginx

[root@localhost sbin]# ./nginx -s reload

注意:此步骤有时会报pid找不到的错误:


可通过如下方式解决:

[root@localhost sbin]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

此时,即可启动成功。


二、 搭建vsftpd

2.1 使用yum安装vsftpd

[root@localhost /]# yum -y install vsftpd

此时,会默认安装在/etc/vsftpd路径下


2.2 添加ftp用户ftpuser,并设置密码

[root@localhost /]# useradd ftpuser
[root@localhost /]# passwd ftpuser
Changing password for user ftpuser.
New password:
BAD PASSWORD: it is too simplistic/systematic
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.
[root@localhost /]#

此时在/home/路径下会生成一个ftpuser文件夹


2.3 开启21号端口,防火墙默认是不开启的

修改/etc/sysconfig/iptables文件,将22那条记录复制,改为21即可

重启防火墙

[root@localhost /]# service iptables restart

2.4 设置vsftpd开机启动

[root@localhost /]# chkconfig vsftpd on

2.5 修改vsftp配置文件

默认vsftp的配置文件路径为: /etc/vsftpd/vsftpd.conf


2.5.1 关闭匿名访问

将anonymous_enable的YES改为NO


2.5.2 设置vsftp的根路径

在/home/ftpuser/路径下创建images文件夹作为vsftp文件根目录,并设置最大权限

[root@localhost /]# mkdir /home/ftpuser/images
[root@localhost /]# ll /home/ftpuser/
total 4
drwxr-xr-x. 2 root root 4096 Mar 31 00:18 images
[root@localhost /]# chmod -R 777 /home/ftpuser/images
[root@localhost /]# ll /home/ftpuser/
total 4
drwxrwxrwx. 2 root root 4096 Mar 31 00:18 images
[root@localhost /]#

在配置文件中添加根路径

注:local为远程用户访问的根路径,anon为匿名用户访问的根路径    注:等号俩边不要有空格

local_root=/home/ftpuser/images/
anon_root=/home/ftpuser/images/

2.5.3 设置是否限制远程用户访问路径

限制远程用户只能访问ftp根路径及其子路径,不能向上访问。默认为可以访问,为NO,如果需要限制,改为YES即可。

chroot_local_user=NO

注: 此处如果设置为YES,则使用FtpClient链接时会有问题,改为NO,就可以了,原因待解决。

2.5.4 重启vsftp

[root@localhost /]# service vsftpd restart

2.6 修改selinux

使用远程终端连接的时候,无法获取目录列表,需要使用如下俩个命令修改俩个状态:

[root@localhost ~]# setsebool -P allow_ftpd_full_access on
[root@localhost ~]# setsebool -P ftp_home_dir on

注: 这俩个命令都会执行俩分钟左右,请耐心等待



2.7 访问ftp服务器

先关闭服务器防火墙,否则用终端工具链接不上ftp服务器

[root@localhost /]# service iptables stop

先通过远程终端工具(推荐使用MobaXterm)链接ftp服务器:


登陆之后上传一张图片到主目录下,然后使用ftp协议访问:


ftp搭建成功。


三、 配置nginx.conf的ftp服务器ip和用户

3.1 修改用户为root


3.2 添加server指向自己的服务器IP和ftp根路径

server{
        listen   80;
        server_name  ftp所在服务器的ip;
        location / {
            root /home/ftpuser;
        }
    }

注: 此时在访问服务器IP/路径的时候会自动将/映射为/home/ftpuser/路径

3.3 重启nginx

[root@localhost sbin]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ./nginx -s reload

3.4 使用http协议访问ftp服务器图片


四.  FtpUtil

4.1 pom.xml


4.2 测试是否可以成功上传

  1. public class FTPTest {
  2. @Test
  3. public void testFtpClient() throws Exception {
  4. // 创建一个FtpClent对象
  5. FTPClient ftpClient = new FTPClient();
  6. // 创建ftp连接,安装ftp服务的虚拟机ip,默认端口为21
  7. ftpClient.connect("xxx.xxx.xxx.xxx",21);
  8. // 使用用户名和密码登录ftp服务器
  9. ftpClient.login("xxxxxx", "xxxxxx");
  10. // 读取本地要上传文件,以流的形式创建
  11. FileInputStream inputStream = new FileInputStream(new File("F:\\temp\\1.png"));
  12. // 设置上传的路径
  13. ftpClient.changeWorkingDirectory("/home/ftpuser/images");
  14. // 修改上传文件的格式
  15. ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
  16. // 上传文件
  17. // 第一个参数为: 服务器端的文件名
  18. // 第二个参数为: 上传文件用的输入流
  19. ftpClient.storeFile("hello.png", inputStream);
  20. // 关闭连接
  21. ftpClient.logout();
  22. }
  23. }

注: 有时候会提示soket失败,可通过如下方式解决:

    eclipse->windows->preferences->java->install JREs --> edit --> Default VM arguments

    添加  -Djava.net.preferIPv4Stack=true 即可


4.3 FtpUtil

  1. package com.taota.common.utils;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import org.apache.commons.net.ftp.FTP;
  10. import org.apache.commons.net.ftp.FTPClient;
  11. import org.apache.commons.net.ftp.FTPFile;
  12. import org.apache.commons.net.ftp.FTPReply;
  13. /**
  14. * ftp上传下载工具类
  15. * <p>Title: FtpUtil</p>
  16. * <p>Description: </p>
  17. * @version 1.0
  18. */
  19. public class FtpUtil {
  20. /**
  21. * Description: 向FTP服务器上传文件
  22. * @param host FTP服务器hostname
  23. * @param port FTP服务器端口
  24. * @param username FTP登录账号
  25. * @param password FTP登录密码
  26. * @param basePath FTP服务器基础目录
  27. * @param filePath FTP服务器文件存放路径。例如分日期存放:/2015/01/01。文件的路径为basePath+filePath
  28. * @param filename 上传到FTP服务器上的文件名
  29. * @param input 输入流
  30. * @return 成功返回true,否则返回false
  31. */
  32. public static boolean uploadFile(String host, int port, String username, String password, String basePath,
  33. String filePath, String filename, InputStream input) {
  34. boolean result = false;
  35. FTPClient ftp = new FTPClient();
  36. try {
  37. int reply;
  38. ftp.connect(host, port);// 连接FTP服务器
  39. // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
  40. ftp.login(username, password);// 登录
  41. reply = ftp.getReplyCode();
  42. if (!FTPReply.isPositiveCompletion(reply)) {
  43. ftp.disconnect();
  44. return result;
  45. }
  46. //切换到上传目录
  47. if (!ftp.changeWorkingDirectory(basePath+filePath)) {
  48. //如果目录不存在创建目录
  49. String[] dirs = filePath.split("/");
  50. String tempPath = basePath;
  51. for (String dir : dirs) {
  52. if (null == dir || "".equals(dir)) continue;
  53. tempPath += "/" + dir;
  54. if (!ftp.changeWorkingDirectory(tempPath)) {
  55. if (!ftp.makeDirectory(tempPath)) {
  56. return result;
  57. } else {
  58. ftp.changeWorkingDirectory(tempPath);
  59. }
  60. }
  61. }
  62. }
  63. //设置上传文件的类型为二进制类型
  64. ftp.setFileType(FTP.BINARY_FILE_TYPE);
  65. //上传文件
  66. if (!ftp.storeFile(filename, input)) {
  67. return result;
  68. }
  69. input.close();
  70. ftp.logout();
  71. result = true;
  72. } catch (IOException e) {
  73. e.printStackTrace();
  74. } finally {
  75. if (ftp.isConnected()) {
  76. try {
  77. ftp.disconnect();
  78. } catch (IOException ioe) {
  79. }
  80. }
  81. }
  82. return result;
  83. }
  84. /**
  85. * Description: 从FTP服务器下载文件
  86. * @param host FTP服务器hostname
  87. * @param port FTP服务器端口
  88. * @param username FTP登录账号
  89. * @param password FTP登录密码
  90. * @param remotePath FTP服务器上的相对路径
  91. * @param fileName 要下载的文件名
  92. * @param localPath 下载后保存到本地的路径
  93. * @return
  94. */
  95. public static boolean downloadFile(String host, int port, String username, String password, String remotePath,
  96. String fileName, String localPath) {
  97. boolean result = false;
  98. FTPClient ftp = new FTPClient();
  99. try {
  100. int reply;
  101. ftp.connect(host, port);
  102. // 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
  103. ftp.login(username, password);// 登录
  104. reply = ftp.getReplyCode();
  105. if (!FTPReply.isPositiveCompletion(reply)) {
  106. ftp.disconnect();
  107. return result;
  108. }
  109. ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
  110. FTPFile[] fs = ftp.listFiles();
  111. for (FTPFile ff : fs) {
  112. if (ff.getName().equals(fileName)) {
  113. File localFile = new File(localPath + "/" + ff.getName());
  114. OutputStream is = new FileOutputStream(localFile);
  115. ftp.retrieveFile(ff.getName(), is);
  116. is.close();
  117. }
  118. }
  119. ftp.logout();
  120. result = true;
  121. } catch (IOException e) {
  122. e.printStackTrace();
  123. } finally {
  124. if (ftp.isConnected()) {
  125. try {
  126. ftp.disconnect();
  127. } catch (IOException ioe) {
  128. }
  129. }
  130. }
  131. return result;
  132. }
  133. // 工具类的使用测试
  134. public static void main(String[] args) {
  135. try {
  136. FileInputStream in=new FileInputStream(new File("F:\\temp\\1.png"));
  137. boolean flag = uploadFile("xxx.xxx.xxx.xxx", 21, "xxx", "xxx", "/home/ftpuser/images","/2016/09/21", "hello.png", in);
  138. System.out.println(flag);
  139. } catch (FileNotFoundException e) {
  140. e.printStackTrace();
  141. }
  142. }
  143. }



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/715630
推荐阅读
相关标签
  

闽ICP备14008679号