springboot 静态资源访问:

这是springboot 默认的静态资源访问路径  访问顺序依次从前到后(http://localhost:8080/bb.jpg)

spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

自定义静态资源访问路径 (http://localhost:8080/bb.jpg)

# 静态文件请求匹配方式 (只要是请求路径配到到了 就访问下面配置的默认静态资源路径)
spring.mvc.static-path-pattern=/**
# 修改默认的静态寻址资源目录 多个使用逗号分隔
spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/upload/

//自定义 不在项目下的路径(比如: c:/upload2)  通过http://localhost:8080/bb.jpg 也能访问  记得加配置

# 静态文件请求匹配方式 (只要是请求路径配到到了 就访问下面配置的默认静态资源路径)
spring.mvc.static-path-pattern=/**
# 修改默认的静态寻址资源目录 多个使用逗号分隔
spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/upload/,classpath:/ c:/upload2

springboot  实现多文件上传

对于上传路径问题  可以通过上面讲的自定义路径来进行配置:下载到电脑的某个位置然后进行访问 和上面的配置一模一样 只是classpath=>file

web.upload-path=/Users/jack/Desktop
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/test/,file:${web.upload-path} 下面贴代码:(文件下载到tomcate下)

html:

<body>
<form enctype="multipart/form-data" method="post" action="/upload">
文件:<input type="file" name="head_img"/>
姓名:<input type="text" name="name"/>
<input type="submit" value="上传"/>
</form> </body> 下载工具类:
/**
* 提取上传方法为公共方法
* @param uploadDir 上传文件目录
* @param file 上传对象
* @throws Exception
*/
private void executeUpload(String uploadDir,MultipartFile file) throws Exception
{
//文件后缀名
String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
//上传文件名
String filename = UUID.randomUUID() + suffix;
//服务器端保存的文件对象
File serverFile = new File(uploadDir + filename);
//将上传的文件写入到服务器端文件内
file.transferTo(serverFile);
}
controller:
@RequestMapping(value = "/uploads",method = RequestMethod.POST)
public @ResponseBody String uploads(HttpServletRequest request,MultipartFile[] file)
{
try {
//上传目录地址
// 随意 String uploadDir = C:/img/;
String uploadDir=ResourceUtils.getURL("classpath:").getPath()+"/static/up/";
System.out.println(uploadDir);
//如果目录不存在,自动创建文件夹
File dir = new File(uploadDir);
if(!dir.exists())
{
dir.mkdir();
}
//遍历文件数组执行上传
for (int i =0;i<file.length;i++) {
if(file[i] != null) {
//调用上传方法
executeUpload(uploadDir, file[i]);
}
}
}catch (Exception e)
{
//打印错误堆栈信息
e.printStackTrace();
return "上传失败";
}
return "上传成功";
}

然后文件下载路径就到了tomcate 下。

需要配置

web.upload-path=/C:/img/
spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/templates/,file:${web.upload-path},file:/static/

也可以通过 http://localhost:8080/up/bb.jpg 访问

springboot 静态资源访问,和文件上传 ,以及路径问题的更多相关文章

  1. 一个简单的QQ隐藏图生成算法 通过jQuery和C#分别实现对.NET Core Web Api的访问以及文件上传

    一个简单的QQ隐藏图生成算法   隐藏图不是什么新鲜的东西,具体表现在大部分社交软件中,预览图看到的是一张图,而点开后看到的又是另一张图.虽然很早就看到过这类图片,但是一直没有仔细研究过它的原理,今天 ...

  2. ASP.NET MVC 文件上传和路径处理

    ASP.NET MVC 文件上传和路径处理总结 目录 文件的上传和路径处理必须解决下面列出的实际问题: 1.重复文件处理 2.单独文件上传 3.编辑器中文件上传 4.处理文章中的图片路径 5.处理上传 ...

  3. springBoot(3)---目录结构,文件上传

    目录结构,文件上传 一.目录结构 1.目录讲解 src/main/java:存放代码      src/main/resources                   static: 存放静态文件, ...

  4. SpringBoot静态资源访问+拦截器+Thymeleaf模板引擎实现简单登陆

    在此记录一下这十几天的学习情况,卡在模板引擎这里已经是四天了. 对Springboot的配置有一个比较深刻的认识,在此和大家分享一下初学者入门Spring Boot的注意事项,如果是初学SpringB ...

  5. 七、springBoot 简单优雅是实现文件上传和下载

    前言 好久没有更新spring Boot 这个项目了.最近看了一下docker 的知识,后期打算将spring boot 和docker 结合起来.刚好最近有一个上传文件的工作呢,刚好就想起这个脚手架 ...

  6. 聊聊、SpringBoot 静态资源访问

    SpringBoot 1.X 版本和 SpringBoot 2.X 版本在静态资源访问上有一些区别,如果直接从 1.X 升级到 2.X 肯定是有问题的.这篇文章就来讲讲这方面问题,也是项目中的坑. 先 ...

  7. SpringBoot --web 应用开发之文件上传

    原文出处: oKong 前言 上一章节,我们讲解了利用模版引擎实现前端页面渲染,从而实现动态网页的功能,同时也提出了兼容jsp项目的解决方案.既然开始讲解web开发了,我们就接着继续往web这个方向继 ...

  8. SpringBoot 文件上传临时文件路径问题

    年后放假回来,一向运行OK的项目突然图片上传不了了,后台报错日志如下: java.io.IOException: The temporary upload location [/tmp/tomcat. ...

  9. 通过jQuery和C#分别实现对.NET Core Web Api的访问以及文件上传

    准备工作: 建立.NET Core Web Api项目 新建一个用于Api请求的UserInfo类 public class UserInfo { public string name { get; ...

随机推荐

  1. 网页免费转换为可编辑的PDF

    Chrome自带的"打印"功能中,另存为PDF 可选择保存选中的内容.如果浏览器/网络出错,不能纠正.(推荐0) https://www.printfriendly.com (有C ...

  2. dedecms:限制栏目列表生成的最大页数防止被采集

    dedecms:限制栏目列表生成的最大页数防止被采集 如果您的网站数据量较大,列表很多的话甚至达到上千页,生成列表时就特别耗费时间,这个缺点可以被优化掉:网站好不容易建起来,担心网站内容被采集走,如果 ...

  3. 将excel表格导入到DataGridView

    using System.Data.OleDb; 添加一个button控件,一个textBox控件,用于显示选择路径  private void loadxls() { String fileName ...

  4. sql从n月到m月数据汇总,没有数据,当月显示0

    做个备份 -- 按月份统计select date1, MONTHS, createtime, nvl(count2, 0)+count1 from ( SELECT TO_CHAR(ADD_MONTH ...

  5. Make It Connected CodeForces - 1095F (建图+最小生成树)

    Make It Connected CodeForces - 1095F You are given an undirected graph consisting of nn vertices. A ...

  6. php 把数字拆分成数组

    用str_split $a = 1234567890; //拆分数字为数组 var_dump( str_split($a, 1) ); 打印结果 : Array ( [0] =2 [1] =5 )

  7. initramfs机制

    1.什么是 Initramfs 在2.6版本的linux内核中,都包含一个压缩过的cpio格式的打包文件.当内核启动时,会从这个打包文件中导出文件到内核的rootfs文件系统,然后内核检查rootfs ...

  8. canvas drawImage图片不显示问题

    初次学习canvas,用来做笔记记录下遇到的问题及解决方案 这里是要将一张图片写到canvas里,按照网上搜索,初写了段代码,可是却没显示,以为是路径问题,不能跨域名使用,后来改为相对路径后,仍然无效 ...

  9. 2018-2019 ACM-ICPC Brazil Subregional Programming Contest B. Marbles(博弈)

    题目链接:https://codeforc.es/gym/101908/problem/B 题意:两个人玩游戏,有 n 块石头,初始坐标为(x,y),一次操作可以将一块石头移动到(x - u,y),( ...

  10. Codeforces Round #587 (Div. 3) A. Prefixes

    链接: https://codeforces.com/contest/1216/problem/A 题意: Nikolay got a string s of even length n, which ...