欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Springboot 配置 上传服务器资源路径映射到本地路径,使图片能使用url地址进行回显

程序员文章站 2022-07-10 17:18:01
...

 文件上传下载是web开发常见项目,本地开发时一般都使用本地存储进行测试,但是图片上传时往往需要回显,这就需要url地址否则不能正常回显,所以需要地址进行映射

第一种方式:(添加配置类)

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class UploadFilePathConfig implements WebMvcConfigurer {
    private static String UPLOAD_PATH = "xxx";
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //  /gbx/file/**为前端URL访问路径  后面 file:xxxx为本地磁盘映射
        registry.addResourceHandler("/gbx/file/**").addResourceLocations("file:C:" + UPLOAD_PATH);
    }
}

第二种方式:(使用nginx)

Springboot 配置 上传服务器资源路径映射到本地路径,使图片能使用url地址进行回显

 完整nginx.conf配置:


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
	
	client_max_body_size 1024m;

    #gzip  on;

    server {
	
        #charset koi8-r;

        #access_log  logs/host.access.log  main;
		
        listen       9231;
        server_name  136.31.17.251;
		
        location / {

            root   D:/testRoadFile;
            index  index.html index.htm;
        }

        #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   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

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


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

如上所示,在保存了nginx.conf配置并重启nginx后,只需要访问 http://136.31.17.251:9231/xxx 就可以访问对应本地目录下的xxx了。

PS:温馨提示,上传时注意nginx和springboot都有文件大小限制,自己重新配置下!