一、依赖文件
<link rel="stylesheet" type="text/css" th:href="@{/js/bootstrap/css/bootstrap.css}">
<link rel="stylesheet" type="text/css" th:href="@{/js/bootstrap/fileinput/css/fileinput.css}">
<!-- 加入FileInput -->
<script th:src="@{/js/jquery-3.3.1.min.js}"></script>
<script th:src="@{/js/bootstrap/js/bootstrap.js}"></script>
<script th:src="@{/js/bootstrap/fileinput/js/fileinput.js}"></script>
<script th:src="@{/js/bootstrap/fileinput/js/zh.js}"></script>/*语言环境*/

二、表单

<form class="form" action="#" method="post" enctype="multipart/form-data"  id="pollutionForm">
<!-- 注意事项:Input type类型为file class为样式 id随意 name随意
multiple(如果是要多图上传一定要加上,不加的话每次只能选中一张图)-->
图片:<input type="file" class="file" id="img" multiple name="images"><br>
</form>

三、JavaScript代码

<script>/**/

    var imageData = []; //多图上传返回的图片属性接受数组  这里是因为我在做表单其他属性提交的时候使用,在这里我没有将别的input写出来

    $("#img").fileinput({
language : 'zh',
uploadUrl : "/image/save-test",
showUpload: true, //是否显示上传按钮
showRemove : true, //显示移除按钮
showPreview : true, //是否显示预览
showCaption: false,//是否显示标题
autoReplace : true,
minFileCount: 0,
uploadAsync: true,
maxFileCount: 10,//最大上传数量
browseOnZoneClick: true,
msgFilesTooMany: "选择上传的文件数量 超过允许的最大数值!",
enctype: 'multipart/form-data',
// overwriteInitial: false,//不覆盖已上传的图片
allowedFileExtensions : [ "jpg", "png", "gif" ],
browseClass : "btn btn-primary", //按钮样式
previewFileIcon : "<i class='glyphicon glyphicon-king'></i>"
}).on("fileuploaded", function(e, data) {//文件上传成功的回调函数,还有其他的一些回调函数,比如上传之前...
var res = data.response;
console.log(res)
imageData.push({
"path": res.data.path,
"date":res.data.date
});
console.log(imageData);
});
</script>

四、后台代码

//图片类
import java.util.Date; /**
* 图片类
*/
public class Img { private String name;
private String path;
private Date date; public Img() {
} public Img(String path, Date date) {
this.path = path;
this.date = date;
} public Img(String name, String path, Date date) {
this.name = name;
this.path = path;
this.date = date;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPath() {
return path;
} public void setPath(String path) {
this.path = path;
} public Date getDate() {
return date;
} public void setDate(Date date) {
this.date = date;
} @Override
public String toString() {
return "Img{" +
"name='" + name + '\'' +
", path='" + path + '\'' +
", date=" + date +
'}';
}
} //图片上传Controller
public class UploadController { //fileinput 其实每次只会上传一个文件 多图上传也是分多次请求,每次上传一个文件 所以不需要循环
@RestController
//@RequestParam("images") 这里的images对应表单中name 然后MultipartFile 参数名就可以任意了
@RequestMapping(value = "/image/save-test",method = RequestMethod.POST)
public UploadResponseData saveImg(@RequestParam("images") MultipartFile file) throws IOException {
String pathname = "";
String returnPath = "";
if (!file.isEmpty()){
String fileName = file.getOriginalFilename();
File path = new File(ResourceUtils.getURL("classpath:").getPath());//获取Spring boot项目的根路径,在开发时获取到的是/target/classes/
System.out.println(path.getPath());//如果你的图片存储路径在static下,可以参考下面的写法
File uploadFile = new File(path.getAbsolutePath(), "static/images/upload/");//开发测试模式中 获取到的是/target/classes/static/images/upload/
if (!uploadFile.exists()){
uploadFile.mkdirs();
}
//获取文件后缀名
String end = FilenameUtils.getExtension(file.getOriginalFilename());
DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
//图片名称 采取时间拼接随机数
String name = df.format(new Date());
Random r = new Random();
for(int i = 0 ;i < 3 ;i++){
name += r.nextInt(10);
}
String diskFileName = name + "." +end; //目标文件的文件名
pathname = uploadFile.getPath()+ "/" + diskFileName;
System.out.println(pathname);
returnPath = "images/upload/" + diskFileName;//这里是我自己做返回的字符串 file.transferTo(new File(pathname));//文件转存
}//UploadResponseData 自定义返回数据类型实体{int code, String msg, Object data}
return new UploadResponseData(CodeEnum.SUCCESS.getCode(),MsgEnum.SUCCESS.getMsg(),new Img(returnPath,new Date()));
} }

五、总结吧

最后在这里想说一些问题
    1、spring boot路径获取问题:
     ResourceUtils.getURL("classpath:").getPath(); 在开发环境下获取到的是项目根路径/target/class/

             ResourceUtils.getURL("classpath: /static/images/upload/").getPath(); 测试失败
             File uploadFile = new File(path.getAbsolutePath(), "static/images/upload/"); 在开发环境下获取到的是/target/class/images/upload/
             在将项目打包位war包部署在tomcat之后,/target/class/ --> /WEB-INF/classes/,同理static/images/upload/ --> /WEB-INF/classes/static/images/upload/
         2、fileinput需要有返回参数 参数格式随意(不要太随意哈)
         3、一下子想不起来了,如果有会继续更
 
六、如果有错的地方,还请指出,谢谢了!

Spring Boot+BootStrap fileInput 多图片上传的更多相关文章

  1. bootstrap fileinput +springmvc图片上传-krajee

    引入的文件 <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/4.4.9/css/filei ...

  2. BootStrap fileinput.js文件上传组件实例代码

    1.首先我们下载好fileinput插件引入插件 ? 1 2 3 <span style="font-size:14px;"><link type="t ...

  3. Bootstrap FileInput 多图上传插件 文档属性说明

    Bootstrap FileInput 多图上传插件   原文链接:http://blog.csdn.net/misterwho/article/details/72886248?utm_source ...

  4. spring boot下MultipartHttpServletRequest如何提高上传文件大小的默认值

    前言: 上传下载功能算是一个非常常见的功能,如果使用MultipartHttpServletRequest来做上传功能. 不配置上传大小的话,默认是2M.在有些场景,这个肯定不能满足条件. 上传代码: ...

  5. 从零开始的Spring Boot(3、Spring Boot静态资源和文件上传)

    Spring Boot静态资源和文件上传 写在前面 从零开始的Spring Boot(2.在Spring Boot中整合Servlet.Filter.Listener的方式) https://www. ...

  6. Spring Boot 2.X 实现文件上传(三)

    使用 SpringBoot 项目完成单个.多个文件的上传处理,并将上传的文件保存到指定目录下. 代码演示案例 所有的 HTML 页面文件 index.html <!DOCTYPE html> ...

  7. Bootstrap fileinput:文件上传插件的基础用法

    官网地址:http://plugins.krajee.com/ 官网提供的样例:http://plugins.krajee.com/file-input/demo 基础用法一 导入核心CSS及JS文件 ...

  8. Spring Boot会员管理系统——处理文件上传

    温馨提示 Spring Boot会员管理系统的中,需要涉及到Spring框架,SpringMVC框架,Hibernate框架,thymeleaf模板引擎.所以,可以学习下这些知识.当然,直接入门的话使 ...

  9. Spring Boot 静态资源映射与上传文件路由配置

    默认静态资源映射目录 默认映射路径 在平常的 web 开发中,避免不了需要访问静态资源,如常规的样式,JS,图片,上传文件等;Spring Boot 默认配置对静态资源映射提供了如下路径的映射 /st ...

随机推荐

  1. iOS实现截屏 并合适保存

     本文转载至:http://blog.csdn.net/zeng11088/article/details/8664510 分类: UIImageView2013-03-12 16:42 122人阅读 ...

  2. 《C++ Primer Plus》第4章 学习笔记

    数组.结构和指针是C++的3中符合类型.数组可以在一个数据对象中存储多个同种类型的值.通过使用索引或下标,可以访问数组中各个元素.结构可以将多个不同类型的值存储在同一个数据对象中,可以使用成员关系运算 ...

  3. JZOJ.5289【NOIP2017模拟8.17】偷笑

    Description berber走进机房,边敲门边喊:“我是哔哔”CRAZY转过头:“我警告你,哔哔刚刚来过!”“呵呵呵呵……”这时,哔哔站了起来,环顾四周:“你们笑什么?……”巧了,发出笑声的人 ...

  4. ios 如何改变UISegmentedControl文本的字体大小?

    UIFont *Boldfont = [UIFont boldSystemFontOfSize:16.0f]; NSDictionary *attributes = [NSDictionary dic ...

  5. LeetCode 笔记系列16.2 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]

    题目:Given a string S and a string T, find the minimum window in S which will contain all the characte ...

  6. 报错分析---->jsp自定义标签:类cannot be resolved to a type

    这个困扰我一个晚上,仔细上网查阅发现,主要是因为jsp自定义标签要用到的这个jsp-api.jar的问题 这是我eclipes中的jar: 然而jsp-api.jar这个jar在tomcat中也有(报 ...

  7. LVS,HAPROXY,NGINX各自的优缺点

    Nginx/LVS/HAProxy的基于Linux的开源免费的负载均衡软件. LVS:使用集群技术和Linux操作系统实现一个高性能.高可用的服务器,它具有很好的可伸缩性.可靠性和可管理性,是一款强大 ...

  8. 并发编程 - 线程 - 1.线程queue/2.线程池进程池/3.异步调用与回调机制

    1.线程queue :会有锁 q=queue.Queue(3) q.get() q.put() 先进先出 队列后进先出 堆栈优先级队列 """先进先出 队列"& ...

  9. 网络编程 - socket通信/粘包/文件传输/udp - 总结

    socket通信 1.简单的套接字通信 import socket phone = socket.socket(socket.AF_INET,socket.SOCK_STREAM) phone.bin ...

  10. Pandas 如何通过获取双(多)重索引获取指定行DataFrame数据

    图片看不清楚的话,可以右键选择:“在新标签页中打开图片(I)” 参数 df.loc[(a,b),c]中第一个参数元组为索引内容,a为level0索引对应的内容,b为level1索引对应的内容 因为df ...