SpringBoot 文件上传(可配置文件上传路径)

1. 在application.yml中配置文件上传路径 ,上传文件大小

application:#版本version: 1.0.0#文件上传路径profile: D:/profile/
spring:servlet:multipart:max-file-size: 30MBmax-request-size: 30MB

2. 配置MVC,使文件上传路径可以被项目访问到

加载配置路径

@Component
@ConfigurationProperties(prefix = "application")
public class MyConfig{/*** 版本*/private String version;/*** 上传文件路径*/private static String profile;public String getVersion() {return version;}public void setVersion(String version) {this.version = version;}public static String getProfile() {return profile;}public void setProfile(String profile) {Twlyyx.profile = profile;}}

MVC配置使http://IP:端口号/${server.context-path}/profile/图片路径可以访问到配置的文件夹

@Configuration
public class ResourceConfig implements WebMvcConfigurer {//图片保存路径public static final String PIC_PATH = "/profile/";@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {/** 图片传路径 */registry.addResourceHandler("/profile/**").addResourceLocations("file:" + MyConfig.getProfile());}

3. 上传文件工具类

网上有很多,这里就不写了

4. 前端上传

 <form method="post"  id="bannerForm"><img id="preview" width="200px" height="200px" onclick="show()" /><input type="file"  title="上传图片"  name="file" id="input_file"  accept="image/gif,image/jpeg,image/jpg,image/png,image/svg" onchange="imgPreview(this)"><%--其他表单信息--%><input type="text" name="info"><button  type="submit">提交</button></form><script>//大图预览function show() {var img = new Image();img.src = $("#preview").attr("src");var imgHtml = "<img src='" + img.src + "' />";//捕获页layer.open({type: 1,shade: false,title: false, //不显示标题area: ['600px', '500px'],// area: [img.width + 'px', img.height+'px'],content: imgHtml, //捕获的元素,注意:最好该指定的元素要存放在body最外层,否则可能被其它的相对元素所影响cancel: function () {//layer.msg('捕获就是从页面已经存在的元素上,包裹layer的结构', { time: 5000, icon: 6 });}});}//上传预览function imgPreview(fileDom) {//判断是否支持FileReaderif (window.FileReader) {var reader = new FileReader();} else {alert("您的设备不支持图片预览功能,如需该功能请升级您的设备!");}//获取文件var file = fileDom.files[0];var imageType = /^image\//;//是否是图片if (!imageType.test(file.type)) {alert("请选择图片!");return;}//读取完成reader.onload = function (e) {//获取图片domvar img = document.getElementById("preview");//图片路径设置为读取的图片img.src = e.target.result;};reader.readAsDataURL(file);}
</script>

Ajax提交含有文件的图片

         //使用var form = $("#bannerForm");不生效var form = document.querySelector("#bannerForm");var formData = new FormData(form);// formData.append("file", $('#input_file')[0].files[0]);$.ajax({url: "${ctx}/save",type: 'POST',cache: false, //上传文件不需要缓存data: formData,processData: false, // 告诉jQuery不要去处理发送的数据contentType: false, // 告诉jQuery不要去设置Content-Type请求头success:function (data) {if (data.code == 0) {layer.alert('添加成功!', function () {window.location.href = '${ctx}/market/list';});} else {layer.msg("失败", {icon: 2, time: 1000});}}});

5. 后台接收

 @ResponseBody@RequestMapping("/save")public Object save(Info info, MultipartFile file) {·········return ···; }

SpringBoot 文件上传(可配置文件上传路径)相关推荐

  1. 将 s1 和 r1 上的启动配置文件上传到服务器进行备份,packettracer综合技能练习261...

    <packettracer综合技能练习261>由会员分享,可在线阅读,更多相关<packettracer综合技能练习261(7页珍藏版)>请在技术文库上搜索. 1.CCNAEX ...

  2. SpringBoot文件上传时提示FileUploadBase$SizeLimitExceed

    场景 SpringBoot配置文件上传文件限制时提示: Resolved [org.springframework.web.multipart.MaxUploadSizeExceededExcepti ...

  3. springboot文件上传服务器,SpringBoot: 浅谈文件上传和访问的坑 (MultiPartFile)

    本次的项目环境为 SpringBoot 2.0.4, JDK8.0. 服务器环境为CentOS7.0, Nginx的忘了版本. 前言 SpringBoot使用MultiPartFile接收来自表单的f ...

  4. 解决Springboot文件上传报错,java.io.FileNotFoundException: D:\System\Temp\tomcat.819...00.tmp (系统找不到指定的文件。)

    Springboot文件上传,csdn上的方法无非是下面这两个: imgFile.transferTo(imageFolder); // 方法一/*** 方法二* FileUtils.copyInpu ...

  5. springboot文件上传下载实战 —— 登录功能、展示所有文件

    springboot文件上传下载实战 创建项目 pom.xml 数据库建表与环境准备 建表SQL 配置文件 application.properties 整体架构 前端页面 登录页面 login.ht ...

  6. SpringBoot文件上传与校验

    文章目录 一.简介 1.概述 2.环境与技术介绍 3.简单的文件上传 二.文件校验与上传实战 1. 前提准备 2. 文件枚举类 3. 自定义文件校验注解 4. 文件校验切面 5. 文件上传工具类 6. ...

  7. SpringBoot实现文件单文件上传、批量上传、下载

    一.搭建一个SpringBoot框架 没有搭建的小伙伴,先去搭建一个 SpringBoot框架快速入门搭建Hello Worldhttps://blog.csdn.net/KangYouWei6/ar ...

  8. SpringBoot 文件上传 通过Content-Type和文件头判断文件类型

    SpringBoot 文件上传 通过Content-Type和文件头判断文件类型 一.关于MIME MIME的全称是Multipurpose Internet Mail Extensions,即多用途 ...

  9. springboot文件上传、下载使用ftp工具将文件上传至服务器

    springboot文件上传.下载使用ftp工具 首先在服务器搭建ftp服务 配置文件(在application.properties中) # Single file max size multipa ...

  10. SpringBoot文件上传和下载

    SpringBoot文件上传和下载 一.SpringBoot文件上传 1.SpringMVC文件上传 1.Client处理 选择文件(1)form表单 选择文件 method="post&q ...

最新文章

  1. tomcat使用php+mac_Mac环境下配置tomcat
  2. 【Android 安全】DEX 加密 ( Application 替换 | 分析 Service 组件中调用 getApplication() 获取的 Application 是否替换成功 )
  3. 英国皇家学会院士樊文飞:把大数据变小,突破企业资源限制
  4. 【pmcaff】罗永浩昨日演讲视频:一个理想主义者的创业故事Ⅳ
  5. 引号快捷键_就业有“位”来 能让你早下班的常用快捷键
  6. 蔡超:入门 Go 语言必须跨越的五个思维误区
  7. 数据结构----单源最短路径Dijkstra
  8. honeywell扫码枪取消回车_霍尼韦尔条码扫描器常见问题与解决方法
  9. qt中记录数据的一次接收处理方法
  10. Scala学习笔记06:自定义控制结构
  11. 概述--Nginx集成Vcenter 6.X HTML Console系列之 1--(共4)
  12. JavaScript BOM 随笔谈
  13. SQL Server 的完整下载安装教程
  14. 蓝桥杯python试题_Python爬取蓝桥杯真题讲解课程
  15. mw150um 驱动程序win10_Intel网卡驱动Win10官方下载_Intel网卡驱动Win10专版64位官方安装版-华军软件园...
  16. SVN :找不到这样的主机
  17. Java编程:基于socket实现局域网双人联机对战五子棋
  18. 用这8个办公软件,效率高一倍
  19. 玻尔兹曼机、深度信念网络、编码器等生成模型
  20. DFRobot离线语音识别模块真实测评

热门文章

  1. bilibili视频下载器v1.0.5 支持4K超清
  2. python读不出图片文件
  3. C/C++编程学习 - 第5周 ⑤ 人见人爱A+B
  4. 基于python的opencv图像处理对交通路口的红绿灯进行颜色检测(最简单的方法)
  5. 大学c语言基础知识必备大全,大学 C语言基础知识.ppt
  6. 情人节到了,手把手教你做一个表白墙,附有详细步骤
  7. node.js(二 --- events 、buffer、)
  8. 什么是PBR?pbr入门基础干货
  9. 被历史遗忘的第一骑兵名将 —— 陈庆之
  10. 交流电中为什么要用相量法?