SpringBoot中靜態(tài)資源訪問(wèn)配置以及內(nèi)置tomcat虛擬文件映射路徑是怎樣的

本篇文章為大家展示了SpringBoot中靜態(tài)資源訪問(wèn)配置以及內(nèi)置tomcat虛擬文件映射路徑是怎樣的,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

成都創(chuàng)新互聯(lián)主營(yíng)泰山網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都app開(kāi)發(fā),泰山h5微信小程序開(kāi)發(fā)搭建,泰山網(wǎng)站營(yíng)銷(xiāo)推廣歡迎泰山等地區(qū)企業(yè)咨詢(xún)

靜態(tài)資源訪問(wèn)配置

@Configuration
@EnableWebMvc
public class StaticResourceConfig implements WebMvcConfigurer {

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

}

內(nèi)置tomcat虛擬文件映射路徑

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/image/**").addResourceLocations("file:C:/image/");
    }

}

應(yīng)用場(chǎng)景

把文件獨(dú)立出來(lái)放到其他盤(pán)符,比如我的服務(wù)在 /server 文件放到 /data
通常情況下放到對(duì)應(yīng)項(xiàng)目的 webapps 下的文件才能正常訪問(wèn),現(xiàn)在我需要訪問(wèn)E盤(pán)中的內(nèi)容,這時(shí)候我就需要 自定義靜態(tài)資源映射

實(shí)現(xiàn)方法

實(shí)現(xiàn)類(lèi)繼承 WebMvcConfigurerAdapter 并重寫(xiě)方法 addResourceHandlers,代碼如下

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class FileServerConfig extends WebMvcConfigurerAdapter {
    @Value("${local.fileserver.dir}")
    private String localFileServerDir;

    @Value("${local.fileserver.path}")
    private String localFileServerPath;

    //訪問(wèn)圖片方法
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/" + this.getLocalFileServerPath() + "/**").addResourceLocations("file:" + this.getLocalFileServerDir() + "/");

        // 本地文件夾要以"flie:" 開(kāi)頭,文件夾要以"/" 結(jié)束,example:
        //registry.addResourceHandler("/abc/**").addResourceLocations("file:D:/pdf/");
        super.addResourceHandlers(registry);
    }

    /**
     * 文件實(shí)際路徑轉(zhuǎn)為服務(wù)器url路徑
     * @param absolutePath
     * @return
     */
    public String toServerPath(String absolutePath) {
        absolutePath = absolutePath.replaceAll("\\\\", "/");
        return "/" + absolutePath.replace(localFileServerDir, localFileServerPath);
    }

    public String getLocalFileServerDir() {
        return localFileServerDir;
    }

    public void setLocalFileServerDir(String localFileServerDir) {
        this.localFileServerDir = localFileServerDir;
    }

    public String getLocalFileServerPath() {
        return localFileServerPath;
    }

    public void setLocalFileServerPath(String localFileServerPath) {
        this.localFileServerPath = localFileServerPath;
    }
}

注意:本地文件夾要以"flie:" 開(kāi)頭,文件夾要以"/" 結(jié)束,example:  
registry.addResourceHandler("/pdf/**").addResourceLocations("file:D:/pdf/");

意思是你的服務(wù)器路徑 serverdata 對(duì)應(yīng)你本地的 /data目錄這時(shí)你的,假如要訪問(wèn) /data/test.jpg圖片,對(duì)應(yīng)的服務(wù)器路徑為 http://localhost:serverdata/test.jpg  

上述內(nèi)容就是SpringBoot中靜態(tài)資源訪問(wèn)配置以及內(nèi)置tomcat虛擬文件映射路徑是怎樣的,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)頁(yè)標(biāo)題:SpringBoot中靜態(tài)資源訪問(wèn)配置以及內(nèi)置tomcat虛擬文件映射路徑是怎樣的
新聞來(lái)源:http://bm7419.com/article44/pcdeee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、自適應(yīng)網(wǎng)站小程序開(kāi)發(fā)、搜索引擎優(yōu)化、App設(shè)計(jì)移動(dòng)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營(yíng)