在Java代码中用FTPClient上传图片到Nginx后访问报404

问题描述:
在乌班图系统下安装了Nginx作为图片的负载均衡服务器,在windows环境直接访问,能访问到欢迎页面,但是用vsftfd工具上传了了一张图片到虚拟机上。上传后再虚拟机对应的路径下能看到该图片,但是访问图片的时候报404.

在这里插入图片描述

public void testFtpClient() throws Exception{
		//创建一个ftpClient对象
		FTPClient ftpClient = new FTPClient();
		//创建ftp连接,默认端口是21
		ftpClient.connect("192.168.209.128",21);
		//登录ftp服务器,使用用户名和密码
		ftpClient.login("uftp", "123456");
		//上传文件
		//读取本地文件
		FileInputStream inputStream = new FileInputStream(new File("E:\\hello.jpg"));
		//设置上传的路径
		ftpClient.changeWorkingDirectory("/usr/share/nginx/html");
		//第一个参数:服务器端文件名
		//第二个参数:上传文档的inputStream
		ftpClient.storeFile("hello1.jpg", inputStream);
		//
		ftpClient.logout();
	}

配置文件详解
1、nginx.conf 文件,路径为:/etc/nginx/agin.conf

#使用的用户和组
user www-data; 
#指定工作衍生进程数(一般等于CPU总核数或总核数的两倍)
worker_processes 4;
#指定PID存放的路径
pid /run/nginx.pid;
#指定文件描述符数量
worker_rlimit_nofile 51200;
events {
    #使用的网络I/O模型,linux戏台推荐采用epoll模型,freebsd系统采用kqueue模型
    use epoll;
    #允许最大连接数
    worker_connections 51200;
    # multi_accept on;
}
http {
    ##
    # 基础设置
    ##
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    server_names_hash_bucket_size 128;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # 日志设置
    ##
    #指定错误日志存放路径数, 错误日志记录的级别可选项为:[debug|info|notice|warn|error|crit]
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # 压缩设置
    ##
    gzip on;
    gzip_disable "msie6";

    gzip_vary on;
    # gzip_proxied any;
    gzip_comp_level 2;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##
    
    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##
    # 虚拟主机设置
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}
#mail {
#    # See sample authentication script at:
#    # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#    # auth_http localhost/auth.php;
#    # pop3_capabilities "TOP" "USER";
#    # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#    server {
#        listen     localhost:110;
#        protocol   pop3;
#        proxy      on;
#    }
# 
#    server {
#        listen     localhost:143;
#        protocol   imap;
#        proxy      on;
#    }
#}

2、default 服务主机配置文件,路径为:/etc/nginx/sites-available/default
#主机

 server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    #root 指令声明了要查找文件的目录。Nginx 会把请求的 URI 添加到 root 指令指定的路径之后,来获取请求文件对应的目录
    root /usr/share/nginx/html;
    index index.html index.htm;
    # Make site accessible from http://localhost/
    server_name localhost;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }
    # Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
    #location /RequestDenied {
    #       proxy_pass http://127.0.0.1:8080;    
    #}

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    #error_page 500 502 503 504 /50x.html;
     #location = /50x.html {
    #       root /usr/share/nginx/html;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #       fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    #
    #       # With php5-cgi alone:
    #       fastcgi_pass 127.0.0.1:9000;
    #       # With php5-fpm:
    #       fastcgi_pass unix:/var/run/php5-fpm.sock;
    #       fastcgi_index index.php;
    #       include fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #       deny all;
    #}

}
解决方案:
ftp工具上传的图片路径是/home/uftp/images
nginx访问路径是/usr/share/nginx/html
修改nginx的default配置文件,增加到images路径的静态配置。其中/home/uftp/images是绝对路径,/images是相对路径。

       location /images {
         alias  /home/uftp/images/;
         autoindex on;
      }

保存后重启Nginx再次访问。sudo service Nginx restart.
终于没有再报404,说明修改的配置是正确的,但是在遇到了下面的几个问题。
1.访问报nginx访问403 Forbidden
经查询应该是权限不足导致的。-rw------- 1 uftp uftp 71039 12月 12 22:58 hello.jpg
为了验证这个问题暂时提升图片hello.jpg的权限

chmod 777 hello.jpg

修改权限后能正常访问,确定是上传文件的权限问题。
查看nginx.conf配置文件,在第一行定义了nginx默认的访问用户是www-data

user www-data;

在安装vsftpd工具时新建了新用户uftp并且先建的image文件夹所属用户也是uftp,所以这里将默认用户改成uftp.重启nginx后重新上传hello.jpg图片。再次访问就能正常访问了。
2.图片像素很低
图片虽然能展示出来,但是展示的图片像素很低。
原图:
原图
上传后的图:
上传后的图
图片是二进制格式的,在代码中用的FTPClient默认用的是文本格式,修改一下上传文件的格式。重新上传并访问。

		//读取本地文件
		FileInputStream inputStream = new FileInputStream(new File("E:\\hello.jpg"));
		//设置上传的路径
		ftpClient.changeWorkingDirectory("/home/uftp/images");
		//设置上传文件的格式
		ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

总结:
至此,终于在成功实现了在Java代码中上传图片到Nginx的负载均衡服务器上。
解决这一系列问题花费的时间比较多,因为自己没有用过Nginx和ftp工具,遇到问题不想按照百度上的答案简单修改就行了。在改的工程甄别、挑选、实现、思考也花费了很多时间,虽然效率低了点,但是真正的转换成了自己的东西,这个结果还是很让人开心的。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值