nginx+ftp搭建圖片服務器(Windows Server服務器環境下)

幾種圖片服務器的對比
一、直接使用ftp服務器,訪問圖片路徑爲  ftp://帳戶:密碼@192.168.0.106/31275-105.jpg
不採用這種方式,不安全容易暴露ftp帳戶信息
二、直接使用IIS或Tomcat等服務器在項目中訪問,圖片少的狀況能夠考慮。商城網站則不行,圖片訪問處理需搭建圖片服務器
三、ftp+nginx服務器,ftp負責上傳圖片,nginx負責圖片的訪問
 
1、須要的組件
一、ftp服務器(圖片上傳,本文選用IIS爲容器)
ps Linux操做系統  可安裝 vsftpd做爲服務器
二、nginx服務器
a、http服務:可使用nginx作靜態資源服務器。也可使用apache。推薦使用nginx,效率更高。
b 、反向代理 實現 負載均衡
 
2、nginx服務器的部署
nginx下載地址  http://nginx.org/en/download.html
 解壓安裝包,免安裝運行
 
nginx cmd命令
start nginx //啓動nginx
nginx -s stop // 中止nginx
nginx -s reload // 從新加載配置文件
nginx -s quit // 退出nginx
nginx -t //檢查配置文件是否正確
         nginx -v //查看nginx版本號
 
一、配置端口號和訪問路徑
conf目錄下的   nginx.conf 配置文件
 
修改端口號 爲9090 
啓動nginx   回到nginx文件夾根目錄,按住shift鍵點擊鼠標右鍵,選擇右鍵菜單中的在此處打開命令窗口,輸入start nginx 命令
 成功訪問
 
 
二、添加圖片服務訪問配置
location ~ \.(gif|jpg|jpeg|png|bmp|swf)$ {
            #默認的圖片路徑,也是ftp上傳文件存放的路徑,只要後綴是以上的都會到這個路徑下搜索
	    root         C:/imgextra;     
}

  

 
 
添加完成之後在cmd中執行  nginx -s reload 從新加載配置使其生效
圖片文件所在路徑 

 訪問成功
 
三、圖片存放路徑分析
先來看看大型商城的圖片路徑
天貓圖片存儲
https://gdp.alicdn.com/imgextra/i2/1856815898/TB2QxuMuHBnpuFjSZFGXXX51pXa_!!1856815898.jpg
京東圖片存儲
https://img30.360buyimg.com/sku/jfs/t4816/236/2599170601/86961/b24bbc4/5902ff58Nc4ceea7c.jpg
噹噹圖片存儲
http://img3x5.ddimg.cn/19/20/1206933175-1_x_6.jpg
 
分析發現幾乎都採用CDN和單獨的域名來做爲獨立的圖片服務器,目的是爲了減小業務服務器的併發訪問量。
關於圖片服務器架構介紹戳這裏  
http://blog.csdn.net/dinglang_2009/article/details/31450731
 
 
3、FTP服務的安裝與使用介紹
一、windows中安裝使用FTP服務器請參考
http://blog.csdn.net/w1014074794/article/details/52075285
 
二、java上傳文件到FTP服務器工具類
所需jar包     commons-net-1.4.1.jar
http://files.cnblogs.com/files/cczheng-666/commons-net-1.4.1.zip
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;

public class FtpUtil {

	/** 
	 * Description: 向FTP服務器上傳文件 
	 * @param host FTP服務器hostname 
	 * @param port FTP服務器端口 
	 * @param username FTP登陸帳號 
	 * @param password FTP登陸密碼 
	 * @param basePath FTP服務器基礎目錄,須要絕對路徑 好比:/home/ftpuser/www/images
	 * @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;
	}
}
相關文章
相關標籤/搜索