欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用springboot對外部靜態(tài)資源文件的處理操作

 更新時間:2021年08月20日 11:29:38   作者:Honins  
這篇文章主要介紹了使用springboot對外部靜態(tài)資源文件的處理操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

springboot對外部靜態(tài)資源文件的處理

springboot對外部資源文件的處理主要分為2部分,存和取,通過查看官方文件和看博客踩了坑之后終于搞定了,特此記錄。

1、存方面倒還簡單,這里貼上一個獲取微信臨時素材并保存的方法

/**
     * @功能 下載臨時素材接口
     * @param filePath 文件將要保存的目錄
     * @param method 請求方法,包括POST和GET
     * @param url 請求的路徑
     * @return
     */ 
    public static String saveUrlAs(String url,String filePath,String method){
        //創(chuàng)建不同的文件夾目錄
        File file=new File(filePath);
        //判斷文件夾是否存在
        if (!file.exists())
        {
            //如果文件夾不存在,則創(chuàng)建新的的文件夾
            file.mkdirs();
        }
        FileOutputStream fileOut = null;
        HttpURLConnection conn = null;
        InputStream inputStream = null;
        String savePath = null ;
        try
        {
            // 建立鏈接
            URL httpUrl=new URL(url);
            conn=(HttpURLConnection) httpUrl.openConnection();
            //以Post方式提交表單,默認get方式
            conn.setRequestMethod(method);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            // post方式不能使用緩存
            conn.setUseCaches(false);
            //連接指定的資源
            conn.connect();
            //獲取網(wǎng)絡輸入流
            inputStream=conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(inputStream);
            //判斷文件的保存路徑后面是否以/結(jié)尾
            if (!filePath.endsWith("/")) { 
                filePath += "/"; 
            }
            String filePathDir =  DateUtil.getStringAllDate();
            //寫入到文件(注意文件保存路徑的后面一定要加上文件的名稱)
            savePath = filePath+filePathDir+".png";
            fileOut = new FileOutputStream(savePath);
            BufferedOutputStream bos = new BufferedOutputStream(fileOut);
 
            byte[] buf = new byte[4096];
            int length = bis.read(buf);
            //保存文件
            while(length != -1)
            {
                bos.write(buf, 0, length);
                length = bis.read(buf);
            }
            bos.close();
            bis.close();
            conn.disconnect();
        } catch (Exception e)
        {
            e.printStackTrace();
            logger.error(">>>>>>>>>>>>>>>>下載臨時素材接口拋出異常 [{}]",e.getMessage());
        } 
        return savePath; 
    }

2、取,由于對springboot不熟悉,所以在這上面踩了坑

先看一下springboot官方文檔對靜態(tài)資源這一塊的表述

主要使用到這2個配置

spring.mvc.static-path-pattern=/resources/**    //配置url訪問路徑
spring.resources.static-locations=                      //配置對應的文件路徑

由于我想要將靜態(tài)資源存到項目外部比如 和項目根目錄同級的 static文件夾里,然后配置了

spring.resources.static-locations=  static/
spring.mvc.static-path-pattern=/static/** 

之后,訪問文件一直404

隨后網(wǎng)上查了一下,看到了一篇文章,發(fā)現(xiàn)

spring.resources.static-locations= file:xxx 

使用了file: + 路徑這一配置方法,然后嘗試了一下,變成這樣:

spring:
  resources:
    static-locations: file:${my.config.static-location}
my:
  config:
    static-location: /static/

發(fā)現(xiàn)文件訪問成功了!

所以實際上外部文件是需要file: 來配置的, static-locations默認訪問的是類路徑下的文件

SpringBoot2.x靜態(tài)資源訪問

問題

在springBoot1.5.x版本,訪問靜態(tài)資源直接訪問static目錄下的資源即可,不用帶上static前綴,在2.x以上就失效了,現(xiàn)在記錄下在2.x版本如何訪問靜態(tài)資源

開發(fā)環(huán)境:IDEA

文件目錄:

  • templates存放官方推薦的thymeleaf模板
  • static存放靜態(tài)資源

在controller目錄下新建一個類,繼承WebMvcConfigurationSupport,對靜態(tài)資源目錄說明

代碼

@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
}

原理

當訪問的url中匹配到/static/**時,就去訪問靜態(tài)資源存放地static目錄下尋找資源

在配置了靜態(tài)資源路徑后,就可以訪問靜態(tài)資源了,但是在訪問時需要在路徑前加上static

<a th:href="@{/static/imag1.jpg}" rel="external nofollow" >跳轉(zhuǎn)</a>
<a th:href="@{/static/images/image2.jpg}" rel="external nofollow" >跳轉(zhuǎn)</a>

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論