溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何使用springboot對外部靜態(tài)資源文件進行處理

發(fā)布時間:2021-08-20 13:43:14 來源:億速云 閱讀:261 作者:chen 欄目:開發(fā)技術

本篇內容主要講解“如何使用springboot對外部靜態(tài)資源文件進行處理”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“如何使用springboot對外部靜態(tài)資源文件進行處理”吧!

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

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

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

      • 主要使用到這2個配置

      • 之后,訪問文件一直404

  • SpringBoot2.x靜態(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();
                    //獲取網絡輸入流
                    inputStream=conn.getInputStream();
                    BufferedInputStream bis = new BufferedInputStream(inputStream);
                    //判斷文件的保存路徑后面是否以/結尾
                    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

        隨后網上查了一下,看到了一篇文章,發(fā)現

        spring.resources.static-locations= file:xxx

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

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

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

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

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

        問題

        在springBoot1.5.x版本,訪問靜態(tài)資源直接訪問static目錄下的資源即可,不用帶上static前綴,在2.x以上就失效了,現在記錄下在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" >跳轉</a>
        <a th:href="@{/static/images/image2.jpg}" rel="external nofollow" >跳轉</a>

        到此,相信大家對“如何使用springboot對外部靜態(tài)資源文件進行處理”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

        向AI問一下細節(jié)

        免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

        AI