1. 程式人生 > >nginx+ftp實現圖片的上傳與訪問

nginx+ftp實現圖片的上傳與訪問

      根據專案的開發要求,使用ftp實現上傳圖片,通過nginx搭建圖片伺服器,即對nginx的簡單功能的一種應用。

      關於vsftp和nginx的安裝就不在這裡詳細演示,下面的程式碼是關於nginx.conf的配置,即將ftp上傳的圖片路徑對映到nginx.conf中。

                             

    下面介紹程式碼是如何實現上傳圖片的。

     jar包的引入

	<!-- 時間操作元件 -->
		<dependency>
			<groupId>joda-time</groupId>
			<artifactId>joda-time</artifactId>

		</dependency>
		<!-- Apache工具元件 -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>

		</dependency>
		<!-- 處理io流的工具 -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-io</artifactId>

		</dependency>
		<!--tomcat上傳到伺服器工具  -->
		<dependency>
			<groupId>commons-net</groupId>
			<artifactId>commons-net</artifactId>

		</dependency>
   程式碼實現
public void testFtpClient() throws Exception{
		//建立ftpClient物件
		FTPClient ftpClient= new FTPClient();
		//建立ftp連結,預設是21埠
		ftpClient.connect("192.168.*.*",21);
		
		//登入ftp伺服器,使用使用者名稱和密碼
		ftpClient.login("ftpuser", "**");
		
		//上傳檔案
		//讀取本地檔案
		FileInputStream inputStream=new FileInputStream(new File("H:\\04 美麗記憶\\2015鳥巢\\IMG_20150716_010643.JPG"));
		
		//設定上傳的路徑
		ftpClient.changeWorkingDirectory("/home/ftpuser/www/images");
		
		//修改上傳格式
		ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
		//第一個引數:伺服器端文件名
		//第二個引數,上傳文件的inputStream
		ftpClient.storeFile("rest.png", inputStream);
		//關閉連結
		ftpClient.logout();
		
		
	}
     對上傳方法的進一步抽取
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;
	}
}
      上傳圖片的功能很普遍,也根據這個功能的實現學習瞭如何抽取工具類的方法,程式碼一步步的優化。