天天看點

linux下使用nginx和ftp做圖檔伺服器一、搭建nginx二、 搭建vsftpd三、 配置nginx.conf的ftp伺服器ip和使用者四.  FtpUtil

一、搭建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路徑下

[[email protected] ~]# cd /usr/local/env/nginx/
[[email protected] nginx]# tar -zxvf nginx-1.8.0.tar.gz -C /usr/local/env/nginx/
           

1.2.2 configure

[[email protected] nginx]# cd nginx-1.8.0
[[email protected] nginx-1.8.0]# ./configure
           

1.2.3 編譯、安裝

[[email protected] nginx-1.8.0]# make
           

此時會生成makefile檔案,再install

[[email protected] nginx-1.8.0]# make install
           

此時,會在/usr/local/路徑下生成nginx相關檔案夾(注:此路徑可以修改,可通過configure配置檔案來修改生成位置)

linux下使用nginx和ftp做圖檔伺服器一、搭建nginx二、 搭建vsftpd三、 配置nginx.conf的ftp伺服器ip和使用者四.  FtpUtil

1.2.4 啟動并通路nginx首頁

[[email protected] nginx-1.8.0]# cd /usr/local/nginx/sbin/
[[email protected] sbin]# ./nginx -c /usr/local/nginx/conf/nginx.conf
           

注: 可以通過-c來指定nginx啟動的配置檔案nginx.conf

預設的nginx.conf會通過配置,将通路路徑/映射到自己的html檔案夾下的index.html首頁上

linux下使用nginx和ftp做圖檔伺服器一、搭建nginx二、 搭建vsftpd三、 配置nginx.conf的ftp伺服器ip和使用者四.  FtpUtil

通路首頁,如果看到歡迎頁,說明安裝nginx成功.

linux下使用nginx和ftp做圖檔伺服器一、搭建nginx二、 搭建vsftpd三、 配置nginx.conf的ftp伺服器ip和使用者四.  FtpUtil

1.2.5 停止nginx

方式1,快速停止:此方式相當于先查出nginx程序id再使用kill指令強制殺掉程序。

[[email protected] sbin]# pwd
/usr/local/nginx/sbin
[[email protected] sbin]# ./nginx -s stop
[[email protected] sbin]#
           

方式2,完整停止(建議使用):此方式停止步驟是待nginx程序處理任務完畢進行停止。

[[email protected] sbin]# pwd
/usr/local/nginx/sbin
[[email protected] sbin]# ./nginx -s quit
[[email protected] sbin]#
           

1.2.6 重新開機nginx

[[email protected] sbin]# ./nginx -s reload
           

注意:此步驟有時會報pid找不到的錯誤:

linux下使用nginx和ftp做圖檔伺服器一、搭建nginx二、 搭建vsftpd三、 配置nginx.conf的ftp伺服器ip和使用者四.  FtpUtil

可通過如下方式解決:

[[email protected] sbin]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
           

此時,即可啟動成功。

二、 搭建vsftpd

2.1 使用yum安裝vsftpd

[[email protected] /]# yum -y install vsftpd
           

此時,會預設安裝在/etc/vsftpd路徑下

linux下使用nginx和ftp做圖檔伺服器一、搭建nginx二、 搭建vsftpd三、 配置nginx.conf的ftp伺服器ip和使用者四.  FtpUtil

2.2 添加ftp使用者ftpuser,并設定密碼

[[email protected] /]# useradd ftpuser
[[email protected] /]# 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.
[[email protected] /]#
           

此時在/home/路徑下會生成一個ftpuser檔案夾

linux下使用nginx和ftp做圖檔伺服器一、搭建nginx二、 搭建vsftpd三、 配置nginx.conf的ftp伺服器ip和使用者四.  FtpUtil

2.3 開啟21号端口,防火牆預設是不開啟的

修改/etc/sysconfig/iptables檔案,将22那條記錄複制,改為21即可

linux下使用nginx和ftp做圖檔伺服器一、搭建nginx二、 搭建vsftpd三、 配置nginx.conf的ftp伺服器ip和使用者四.  FtpUtil

重新開機防火牆

[[email protected] /]# service iptables restart
           

2.4 設定vsftpd開機啟動

[[email protected] /]# chkconfig vsftpd on
           

2.5 修改vsftp配置檔案

預設vsftp的配置檔案路徑為: /etc/vsftpd/vsftpd.conf

linux下使用nginx和ftp做圖檔伺服器一、搭建nginx二、 搭建vsftpd三、 配置nginx.conf的ftp伺服器ip和使用者四.  FtpUtil

2.5.1 關閉匿名通路

将anonymous_enable的YES改為NO

linux下使用nginx和ftp做圖檔伺服器一、搭建nginx二、 搭建vsftpd三、 配置nginx.conf的ftp伺服器ip和使用者四.  FtpUtil

2.5.2 設定vsftp的根路徑

在/home/ftpuser/路徑下建立images檔案夾作為vsftp檔案根目錄,并設定最大權限

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

在配置檔案中添加根路徑

注: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

[[email protected] /]# service vsftpd restart
           

2.6 修改selinux

使用遠端終端連接配接的時候,無法擷取目錄清單,需要使用如下倆個指令修改倆個狀态:

[[email protected] ~]# setsebool -P allow_ftpd_full_access on
[[email protected] ~]# setsebool -P ftp_home_dir on
           

注: 這倆個指令都會執行倆分鐘左右,請耐心等待

linux下使用nginx和ftp做圖檔伺服器一、搭建nginx二、 搭建vsftpd三、 配置nginx.conf的ftp伺服器ip和使用者四.  FtpUtil

2.7 通路ftp伺服器

先關閉伺服器防火牆,否則用終端工具連結不上ftp伺服器

[[email protected] /]# service iptables stop
           

先通過遠端終端工具(推薦使用MobaXterm)連結ftp伺服器:

linux下使用nginx和ftp做圖檔伺服器一、搭建nginx二、 搭建vsftpd三、 配置nginx.conf的ftp伺服器ip和使用者四.  FtpUtil
linux下使用nginx和ftp做圖檔伺服器一、搭建nginx二、 搭建vsftpd三、 配置nginx.conf的ftp伺服器ip和使用者四.  FtpUtil

登陸之後上傳一張圖檔到主目錄下,然後使用ftp協定通路:

linux下使用nginx和ftp做圖檔伺服器一、搭建nginx二、 搭建vsftpd三、 配置nginx.conf的ftp伺服器ip和使用者四.  FtpUtil

ftp搭建成功。

三、 配置nginx.conf的ftp伺服器ip和使用者

3.1 修改使用者為root

linux下使用nginx和ftp做圖檔伺服器一、搭建nginx二、 搭建vsftpd三、 配置nginx.conf的ftp伺服器ip和使用者四.  FtpUtil

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

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

注: 此時在通路伺服器IP/路徑的時候會自動将/映射為/home/ftpuser/路徑

3.3 重新開機nginx

[[email protected] sbin]# cd /usr/local/nginx/sbin/
[[email protected] sbin]# ./nginx -s reload
           

3.4 使用http協定通路ftp伺服器圖檔

linux下使用nginx和ftp做圖檔伺服器一、搭建nginx二、 搭建vsftpd三、 配置nginx.conf的ftp伺服器ip和使用者四.  FtpUtil

四.  FtpUtil

4.1 pom.xml

linux下使用nginx和ftp做圖檔伺服器一、搭建nginx二、 搭建vsftpd三、 配置nginx.conf的ftp伺服器ip和使用者四.  FtpUtil

4.2 測試是否可以成功上傳

public class FTPTest {
	
	@Test
	public void testFtpClient() throws Exception {
		// 建立一個FtpClent對象
		FTPClient ftpClient = new FTPClient();
		// 建立ftp連接配接,安裝ftp服務的虛拟機ip,預設端口為21
		ftpClient.connect("xxx.xxx.xxx.xxx",21);
		// 使用使用者名和密碼登入ftp伺服器
		ftpClient.login("xxxxxx", "xxxxxx");
		
		// 讀取本地要上傳檔案,以流的形式建立
		FileInputStream inputStream = new FileInputStream(new File("F:\\temp\\1.png"));
		// 設定上傳的路徑
		ftpClient.changeWorkingDirectory("/home/ftpuser/images");
		// 修改上傳檔案的格式
		ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
		// 上傳檔案
		// 第一個參數為: 伺服器端的檔案名
		// 第二個參數為: 上傳檔案用的輸入流
		ftpClient.storeFile("hello.png", inputStream);
		// 關閉連接配接
		ftpClient.logout();
	}
	
}
           

注: 有時候會提示soket失敗,可通過如下方式解決:

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

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

4.3 FtpUtil

package com.taota.common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

/**
 * ftp上傳下載下傳工具類
 * <p>Title: FtpUtil</p>
 * <p>Description: </p>
 * @version 1.0
 */
public class FtpUtil {

	/** 
	 * Description: 向FTP伺服器上傳檔案 
	 * @param host FTP伺服器hostname 
	 * @param port FTP伺服器端口 
	 * @param username FTP登入賬号 
	 * @param password FTP登入密碼 
	 * @param basePath FTP伺服器基礎目錄
	 * @param filePath FTP伺服器檔案存放路徑。例如分日期存放:/2015/01/01。檔案的路徑為basePath+filePath
	 * @param filename 上傳到FTP伺服器上的檔案名 
	 * @param input 輸入流 
	 * @return 成功傳回true,否則傳回false 
	 */  
	public static boolean uploadFile(String host, int port, String username, String password, String basePath,
			String filePath, String filename, InputStream input) {
		boolean result = false;
		FTPClient ftp = new FTPClient();
		try {
			int reply;
			ftp.connect(host, port);// 連接配接FTP伺服器
			// 如果采用預設端口,可以使用ftp.connect(host)的方式直接連接配接FTP伺服器
			ftp.login(username, password);// 登入
			reply = ftp.getReplyCode();
			if (!FTPReply.isPositiveCompletion(reply)) {
				ftp.disconnect();
				return result;
			}
			//切換到上傳目錄
			if (!ftp.changeWorkingDirectory(basePath+filePath)) {
				//如果目錄不存在建立目錄
				String[] dirs = filePath.split("/");
				String tempPath = basePath;
				for (String dir : dirs) {
					if (null == dir || "".equals(dir)) continue;
					tempPath += "/" + dir;
					if (!ftp.changeWorkingDirectory(tempPath)) {
						if (!ftp.makeDirectory(tempPath)) {
							return result;
						} else {
							ftp.changeWorkingDirectory(tempPath);
						}
					}
				}
			}
			//設定上傳檔案的類型為二進制類型
			ftp.setFileType(FTP.BINARY_FILE_TYPE);
			//上傳檔案
			if (!ftp.storeFile(filename, input)) {
				return result;
			}
			input.close();
			ftp.logout();
			result = true;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (ftp.isConnected()) {
				try {
					ftp.disconnect();
				} catch (IOException ioe) {
				}
			}
		}
		return result;
	}
	
	/** 
	 * Description: 從FTP伺服器下載下傳檔案 
	 * @param host FTP伺服器hostname 
	 * @param port FTP伺服器端口 
	 * @param username FTP登入賬号 
	 * @param password FTP登入密碼 
	 * @param remotePath FTP伺服器上的相對路徑 
	 * @param fileName 要下載下傳的檔案名 
	 * @param localPath 下載下傳後儲存到本地的路徑 
	 * @return 
	 */  
	public static boolean downloadFile(String host, int port, String username, String password, String remotePath,
			String fileName, String localPath) {
		boolean result = false;
		FTPClient ftp = new FTPClient();
		try {
			int reply;
			ftp.connect(host, port);
			// 如果采用預設端口,可以使用ftp.connect(host)的方式直接連接配接FTP伺服器
			ftp.login(username, password);// 登入
			reply = ftp.getReplyCode();
			if (!FTPReply.isPositiveCompletion(reply)) {
				ftp.disconnect();
				return result;
			}
			ftp.changeWorkingDirectory(remotePath);// 轉移到FTP伺服器目錄
			FTPFile[] fs = ftp.listFiles();
			for (FTPFile ff : fs) {
				if (ff.getName().equals(fileName)) {
					File localFile = new File(localPath + "/" + ff.getName());

					OutputStream is = new FileOutputStream(localFile);
					ftp.retrieveFile(ff.getName(), is);
					is.close();
				}
			}

			ftp.logout();
			result = true;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (ftp.isConnected()) {
				try {
					ftp.disconnect();
				} catch (IOException ioe) {
				}
			}
		}
		return result;
	}
	
	
	// 工具類的使用測試
	public static void main(String[] args) {
		try {  
	        FileInputStream in=new FileInputStream(new File("F:\\temp\\1.png"));  
	        boolean flag = uploadFile("xxx.xxx.xxx.xxx", 21, "xxx", "xxx", "/home/ftpuser/images","/2016/09/21", "hello.png", in);  
	        System.out.println(flag);  
	    } catch (FileNotFoundException e) {  
	        e.printStackTrace();  
	    }  
	}
	
}
           

繼續閱讀