FTP和nginx搭建檔案存取器
FTPUtil工具類 --檔案上傳(在多tomcat時,檔案不能存到tomcat裡,需要使用ftp和nginx搭建一個存檔案,讀取檔案的地方)
安裝FTP伺服器 執行並登陸 測試:在瀏覽器上輸入ftp:****** ******為ip
需要注意的是,在伺服器或者linux上需要建立ftp使用者,將ftp上傳檔案的資料夾使用者設為ftp使用者
下圖的ftp.server.http.prefix的值為nginx代理的域名
public class FTPUtil { private static final Logger logger = LoggerFactory.getLogger(FTPUtil.class); private static String ftpIp = PropertiesUtil.getProperty("ftp.server.ip"); private static String ftpUser = PropertiesUtil.getProperty("ftp.user"); private static String ftpPass = PropertiesUtil.getProperty("ftp.pass"); public FTPUtil(String ip,int port,String user,String pwd){ this.ip = ip; this.port = port; this.user = user; this.pwd = pwd; } public static boolean uploadFile(List<File> fileList) throws IOException { FTPUtil ftpUtil = new FTPUtil(ftpIp,21,ftpUser,ftpPass); logger.info("開始連線ftp伺服器"); //注意:如果nginx代理指向的資料夾中包括img資料夾,訪問圖片時需要在mmall.properties的ftp.server.http.prefix值後新增img/ boolean result = ftpUtil.uploadFile("img",fileList); logger.info("結束上傳,上傳結果:{}",result); return result; } private boolean uploadFile(String remotePath,List<File> fileList) throws IOException { boolean uploaded = false; FileInputStream fis = null; //連線FTP伺服器 if(connectServer(this.ip,this.port,this.user,this.pwd)){ try { ftpClient.changeWorkingDirectory(remotePath); //需不需要切換資料夾 ftpClient.setBufferSize(1024); ftpClient.setControlEncoding("UTF-8"); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); //二進位制檔案型別 ftpClient.enterLocalPassiveMode(); //開啟本地的被動模式 for(File fileItem : fileList) { fis = new FileInputStream(fileItem); ftpClient.storeFile(fileItem.getName(), fis); uploaded = true; } } catch (IOException e) { logger.error("ftp上傳檔案異常",e); e.printStackTrace(); } finally { fis.close(); ftpClient.disconnect(); } } return uploaded; } private boolean connectServer(String ip,int port,String user,String pwd){ boolean isSuccess = false; ftpClient = new FTPClient(); try { ftpClient.connect(ip); //連線ftp isSuccess = ftpClient.login(user,pwd); //登陸ftp } catch (IOException e) { logger.error("連線FTP伺服器異常",e); } return isSuccess; } private String ip; private int port; private String user; private String pwd; private FTPClient ftpClient; public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public FTPClient getFtpClient() { return ftpClient; } public void setFtpClient(FTPClient ftpClient) { this.ftpClient = ftpClient; } }
nginx部分:
在瀏覽器中輸入一個網址會先check一下你本地的hosts檔案,如果有做對映的話就直接通過對映的ip訪問你的web伺服器(這邊是nginx) 當這個請求被nginx 獲得後 他會check一下請求的域名和servername是否匹配,
匹配到的話就根據相應的配置返回內容,沒有匹配到的話就根據預設的配置返回內容
linux下
在/usr/local/conf中vim nginx.conf新增include vhost/*.conf; 即匯入在conf下的所有以.conf結尾的配置檔案
在/conf中mkdir vhost
在/etc/hosts中新增例如:******** image.imooc.com 前面是伺服器ip 後面是域名
在/conf/vhost中vim image.imooc.com.conf(可以與/etc/hosts中的名字不一致)
編輯image.imooc.com.conf 監聽80埠,碰到image.imooc.com轉到location 裡面
server { listen 80; autoindex off; //是否顯示目錄 server_name image.imooc.com; access_log /usr/local/nginx/logs/access.log combined; index index.html index.htm index.jsp index.php; #error_page 404 /404.html; if ( $query_string ~* ".*[\;'\<\>].*" ){ return 404; } location ~ /(mmall_fe|mmall_admin_fe)/dist/view/* { deny all; } location / { root /ftpfile/img/; add_header Access-Control-Allow-Origin *; } }
重啟: ../../sbin/nginx -s reload
window
在nginx的安裝目錄下找到conf資料夾下的nginx.conf裡面新增include vhost/*.conf
在/conf中mkdir vhost
在/conf/vhost中vim image.imooc.com裡面
server {
listen 80;
autoindex on;
server_name image.imooc.com www.image.imooc.com;
access_log D:/nginx/nginx-1.10.2/logs/access.log combined;
index index.html index.htm index.jsp index.php;
#error_page 404 /404.html;
if ( $query_string ~* ".*[\;'\<\>].*" ){ return 404; }
location ~ /(mmall_fe|mmall_admin_fe)/dist/view/* { deny all; }
location / {
root D:\ftpfile;
add_header Access-Control-Allow-Origin *; }
}
在c:\Windows\System32\drivers\etc\hosts中新增
例如:****** www.imooc.com 前面是伺服器ip 後面是域名
nginx.exe -t 測試配置檔案是否正確
nginx.exe -s reload 重啟