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 重启

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值