SpringBoot项目访问静态资源

1、一般项目的上传路径都是配置在yml的配置文件中,并且多是相对路径存储。windows 而言相对路径于项目所在磁盘的文件地址 ,如/home/etcom 项目在E: 那么文件地址在E:/home/etcom;Linux 而言相对root 或者当前登录用户所在空间的根目录+/home/etcom

2、上传的文件在项目如何能展示呢

方法1:
//前端 发送ajax请求,进行下载的动作:
function showQrcode(row){
   $("#devQrcodeBase").attr("src", ctxPath+"resources/images/signature.png");
   //$("#showIconModal").modal("show");

   var params = {};
   params.filePath = row.qrcode;
   params.fileName = row.name;
   var type = "application/octet-stream";

   var xhr = new XMLHttpRequest();
   xhr.open('POST', rsManage+"/devFileInfo/showIcon", true); //
   xhr.responseType = "blob"; // 返回类型blob
   xhr.setRequestHeader("Content-Type","application/json;charset=UTF-8");//设置请求内容类型
   xhr.setRequestHeader("token",token);//设置请求内容类型

   xhr.onload = function() {
       if (this.status != 200) {// 判断请求状态码,看请求是否成功执行
           alert("下载失败!");
           return;
       }
       var content = this.response; //获取后台响应内容
       var excelBlob = new Blob([ content ], {type : type});
       var fileName = row.name;//
       if (false) {
           window.navigator.msSaveOrOpenBlob(excelBlob, fileName);
       } else {
           $("#devQrcodeBase").prop("src",URL.createObjectURL(excelBlob));
           /*var a = document.get('a');
           a.href = URL.createObjectURL(excelBlob);
           a.download = fileName;
           a.click();
           a.href = "";*/
       }
   };
   //发送ajax请求
   xhr.send(JSON.stringify(params));
}

//后台:
/**
 * 用于列表中的点击查看图片
 * @param response
 */
@PostMapping("/showIcon")
@ApiOperation(value = "用于列表中的点击查看图片", notes = "用于列表中的点击查看图片.")
public void downloadIcon(@RequestBody RsAttachedFileInfo rsAttachedFileInfo, HttpServletResponse response){
    try(OutputStream os = response.getOutputStream();
        FileInputStream is = new FileInputStream(new File(rsAttachedFileInfo.getPath()))) {
        response.setContentType("application/force-download");// 设置强制下载不打开
        //设置响应头
        response.setHeader("Content-disposition", "attachment; filename="+new String(rsAttachedFileInfo.getName().getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
        IOUtils.copy(is,os);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

这种方法 使用比较受限制。直接在img的src中赋值,数据库中保存的相对路径更好,如下。

方法2:
@Configuration
public class WebMvcConfgInterceptor implements WebMvcConfigurer {
	@Autowired
	private WebDefineProperties wdp;
	/**
	 * 静态资源
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static");
		registry.addResourceHandler("/swagger/**").addResourceLocations("classpath:/static/swagger/");
		registry.addResourceHandler("swagger-ui.html")
				.addResourceLocations("classpath:/META-INF/resources/");
		//addResourceLocations 这个参数要注意
		registry.addResourceHandler("/upload/**").addResourceLocations("file:" +wdp.getAudioPath()+"/");
		registry.addResourceHandler("/home/etcom/resourcesManagement/attachedFile/**/**/**").addResourceLocations("file:" +wdp.getAttachedPath()+"/");
		WebMvcConfigurer.super.addResourceHandlers(registry);
	}
}

需要特别注意的是,这种访问路径映射转化的,addResourceLocations中的file: 不可少,并且文件物理路径中的最后要+ / 否则会一直404.

访问:http://localhost:8001/upload/xiaoguo.png

或者 http://localhost:8001/upload/a/xiaoguo.png

addResourceHandler 中的参数是访问路径中存在的信息。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值