1. 程式人生 > >訪問路徑對映到本地實現圖片的線上預覽

訪問路徑對映到本地實現圖片的線上預覽

 1.簡介

對於檔案的下載展示一般有兩種方法:

1.直接以流的形式返回給前端,在我之前文章中已經介紹過了下載和線上預覽。

2.把檔案地址對映到本地,把本地地址給前端

 

2.對映到本地實現

2.1 首先在ssm專案中的common.properties 中地址對映的路徑

#二維碼圖片儲存路徑
pictureStorePath=C:/aaa/pic/
pictureReadPath=/videoshot/pic/

#excel匯出儲存路徑
excelSotrPath=C:/aaa/excel/
excelReadPath=/videoshot/excel/

2.2配置的檔案

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
@Component
public class StaticFile extends WebMvcConfigurerAdapter {
    @Value("${pictureStorePath}")//    "D:/staticFile/pic/"
    private String pictureStorePath;
    
    @Value("${pictureReadPath}")// "/videoshot/pic/"
    private String pictureReadPath;
    
    @Value("${excelSotrPath}")// "D:/staticFile/excel/"
    private String excelSotrPath;
    
    @Value("${excelReadPath}")//  "/videoshot/excel/"
    private String excelReadPath;   
    
    
     @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler(pictureReadPath+"/**").addResourceLocations("file:"+pictureStorePath+"/");
            registry.addResourceHandler(excelReadPath+"/**").addResourceLocations("file:"+excelSotrPath+"/");
            super.addResourceHandlers(registry);
        }
}

 

 2.3 用法

@Value("${pictureReadPath}")
    private String xpath;

qr.setDevice_qr_code(xpath+uid+".jpg");

 

2.4 結果展示

 訪問圖片

 地址+埠+專案名+訪問的對映路徑

 

================================================================================

小小的餅乾---記錄