springboot access files in the jar with the level of access problems

Recent springboot upload package when it came to a question, that is, the same level of access to the jar file, prior to use most is the form of war, so a good set of static resource path.

But the jar can not see inside the folder, so upload files to the same level with the lower jar upload, so you need to set the upload in the project should also be static resources.

 

Key Code

// static resource allocation 
    @Override
     public  void addResourceHandlers (ResourceHandlerRegistry Registry) {
         // need to configure 1: --- the need to tell the system which is to be treated as static files! Set internal static resource
         // first access path is provided a method of prefixes, a method is provided second resource path 
        registry.addResourceHandler ( "/ static / **") addResourceLocations. ( "CLASSPATH: / static /" ); 
        registry.addResourceHandler ( "/upload/**").addResourceLocations("classpath:/upload/" ); 
        registry.addResourceHandler ( "/templates/**").addResourceLocations("classpath:/templates/" );
        ApplicationHome ( the this .getClass ());
         // local acquisition path D: \ idea \ springboot2.x \ target upload project with the same level jar 
        String path = h.getSource () getParent ();. 
        String realpath = path + " / Upload / " ; 
        registry.addResourceHandler ( " /upload/**").addResourceLocations("file:"+ realpath); 
    }

Once set up, we have encountered a problem, css, js can not be loaded because I'm using nginx handle static resources, but also these proxy, as follows

server {
  listen 80;
  server_name springboot.kingsuper.net;
  access_log /data/wwwlogs/springboot.kingsuper.net_nginx.log combined;
  index index.html index.htm index.jsp;
  root /data/wwwroot/springboot.kingsuper.net;
  
  #error_page 404 /404.html;
  #error_page 502 /502.html;
  
  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
    proxy_pass http://127.0.0.1:8080;
    expires 30d;
    access_log off;
  }

  location ~ .*\.(js|css)?$ {
    proxy_pass http://127.0.0.1:8080;
    expires 7d;
    access_log off;
  }

  location ~ /\.ht {
    deny all;
  }
  
  location ~ {
    proxy_pass http://127.0.0.1:8080;
    include proxy.conf;
  }
}

Once set up, you can visit, over

 

Guess you like

Origin www.cnblogs.com/SeaWxx/p/10935209.html