默認映射路徑css
在日常的 web 開發中,避免不了須要訪問靜態資源,如常規的樣式,JS,圖片,上傳文件等;Spring Boot 默認配置對靜態資源映射提供了以下路徑的映射 /static (or /public or /resources or /META-INF/resources) ,以下:html
/META-INF/resources/ classpath:/resources/ classpath:/static/ classpath:/public/
能夠在源碼中能夠查看到 nginx
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/" };
固然能夠經過配置的方式自定義靜態資源的路徑,可是會覆蓋默認約定的映射目錄(默認的配置不可用) git
# Locations of static resources.
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
通常狀況沒有特殊需求不建議自定義配置,使用默認的就好,約定大於配置嘛。也就是在 resources 目錄下 public、resources、static(新建項目自帶) 三個目錄。github
默認訪問路由web
靜態資源的默認路由匹配 /** , 路由會從這三個目錄中尋找靜態資源,若是有則返回,固然能夠配置改變默認的路由,以下:spring
spring.mvc.static-path-pattern=/resources/**瀏覽器
OK,我新建了一個項目,結構目錄以下:spring-mvc
好比在 static 目錄下新增一個 MP_verify_46Daxcm7OmhjBcYa.txt 文本文件,瀏覽器訪問 http://localhost:8080/MP_verify_46Daxcm7OmhjBcYa.txt 便可,引用 css,js ,圖片等資源也不須要再加 static 目錄了。服務器
自定義靜態資源映射目錄能夠經過上述配置的方式配置,固然也能夠經過編碼的方式實現
WebConfig
@Configuration public class WebConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { //將訪問/static/** 的路由映射到classpath:/static/ 目錄下 registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); } @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("index"); registry.addViewController("/groovy").setViewName("hello"); registry.addViewController("/app").setViewName("app"); } }
默認 Spring Boot 是內置了 Tomcat 以單個 Jar 包的運行(固然也能夠使用 war 包的方式運行在容器裏),Spring Boot 項目啓動的時候會跟根據約定把靜態文件加載到 classpath 目錄下,若是要上傳文件或寫文件日誌的話,必須把訪問的路由映射到服務器的文件目錄。
FileUploadControllerprivate final ResourceLoader resourceLoader; @Autowired public UploadController(ResourceLoader resourceLoader) { this.resourceLoader = resourceLoader; } /** * 上傳文件 * @param file * @param model * @param request * @return */ @RequestMapping(method = RequestMethod.POST, value = "/") public String upload(@RequestParam("file") MultipartFile file, Model model, HttpServletRequest request) { if (!file.isEmpty()) { try { Files.copy(file.getInputStream(), Paths.get("/upload", file.getOriginalFilename())); model.addAttribute("message", "You successfully uploaded " + file.getOriginalFilename() + "!"); } catch (IOException|RuntimeException e) { model.addAttribute("message", "Failued to upload " + file.getOriginalFilename() + " => " + e.getMessage()); } } else { model.addAttribute("message", "Failed to upload " + file.getOriginalFilename() + " because it was empty"); } return "redirect:/"; } } /** * 綁定微信用戶 (頭像圖片編碼base64) * @param weixinBindRequest * @param session * @return */ @ResponseBody @RequestMapping(value = "/uploadImgbase64",method= RequestMethod.POST) public WeixinBindResponse uploadImgbase64(@RequestBody WeixinBindRequest weixinBindRequest, HttpSession session) { //logger.info("weixinbind req: " + JSON.toJSONString(weixinBindRequest)); WeixinBindResponse response= new WeixinBindResponse(); logger.info("weixinbind openId: "+weixinBindRequest.getOpenid()); //保存用戶頭像 for (int i=0;i<weixinBindRequest.getImgs().length;i++){ String dataPrix = ""; String data = ""; String suffix = ""; String [] d = weixinBindRequest.getImgs()[i].split("base64,"); if(d != null && d.length == 2){ dataPrix = d[0]; data = d[1]; } if("data:image/jpeg;".equalsIgnoreCase(dataPrix)){//data:image/jpeg;base64,base64編碼的jpeg圖片數據 suffix = ".jpg"; } else if("data:image/x-icon;".equalsIgnoreCase(dataPrix)){//data:image/x-icon;base64,base64編碼的icon圖片數據 suffix = ".ico"; } else if("data:image/gif;".equalsIgnoreCase(dataPrix)){//data:image/gif;base64,base64編碼的gif圖片數據 suffix = ".gif"; } else if("data:image/png;".equalsIgnoreCase(dataPrix)){//data:image/png;base64,base64編碼的png圖片數據 suffix = ".png"; } String fileName = weixinBindRequest.getOpenid() +"_"+i + suffix; try{ // request.getSession().getServletContext().getRealPath("upload"); // String path = String.valueOf(Paths.get("upload", weixinBindRequest.getOpenid())); FileUtils.writeByteArrayToFile(new File("upload", fileName), Base64Utils.decodeFromString(data)); }catch(Exception ee){ } } //微信綁定用戶信息 boolean flag = this.userSerice.bindWeixinUser(weixinBindRequest); if(!flag){ response.setIsError(true); response.setErrorCode(500); response.setErrorMsg("微信綁定用戶信息失敗!!!"); return response; } response.setIsError(false); response.setErrorCode(200); response.setErrorMsg("微信綁定用戶信息成功!!!"); return response; } /** * 顯示圖片 * @param filename * @return */ @RequestMapping(method = RequestMethod.GET, value = "/{filename:.+}") @ResponseBody public ResponseEntity<?> getFile(@PathVariable String filename) { try { return ResponseEntity.ok(resourceLoader.getResource("file:" + Paths.get("upload", filename).toString())); } catch (Exception e) { return ResponseEntity.notFound().build(); } }
上傳文件的路徑會項目根目錄上建立,因此不能被直接訪問到 ,Spring 提供了 ResourceLoader ,利於這個類能夠加載非應用目錄的裏文件而後返回,具體看官方的列子吧:
https://spring.io/guides/gs/uploading-files/
https://github.com/spring-guides/gs-uploading-files.git
上傳文件大小限制
最後別忘了配置文件大小的限制
spring.http.multipart.max-file-size=128KB
spring.http.multipart.max-request-size=128KB
遇到的問題:
若是使用了 nginx 代理提示以下錯誤:
nginx 中配置 client_max_body_size 容許的大小
client_max_body_size 100m;
client_body_buffer_size 128k;
配置附錄:
# SPRING RESOURCES HANDLING (ResourceProperties)
spring.resources.add-mappings=true # Enable default resource handling.
spring.resources.cache-period= # Cache period for the resources served by the resource handler, in seconds.
spring.resources.chain.cache=true # Enable caching in the Resource chain.
spring.resources.chain.enabled= # Enable the Spring Resource Handling chain. Disabled by default unless at least one strategy has been enabled.
spring.resources.chain.gzipped=false # Enable resolution of already gzipped resources.
spring.resources.chain.html-application-cache=false # Enable HTML5 application cache manifest rewriting.
spring.resources.chain.strategy.content.enabled=false # Enable the content Version Strategy.
spring.resources.chain.strategy.content.paths=/** # Comma-separated list of patterns to apply to the Version Strategy.
spring.resources.chain.strategy.fixed.enabled=false # Enable the fixed Version Strategy.
spring.resources.chain.strategy.fixed.paths=/** # Comma-separated list of patterns to apply to the Version Strategy.
spring.resources.chain.strategy.fixed.version= # Version string to use for the Version Strategy.
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ # Locations of static resources.
REFER:
https://spring.io/blog/2014/07/24/spring-framework-4-1-handling-static-web-resources
https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-spring-mvc-static-content