引入的文件

<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/4.4.9/css/fileinput.min.css" media="all" rel="stylesheet" type="text/css" />

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css">
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/4.4.9/css/fileinput.min.css" media="all" rel="stylesheet" type="text/css" />
<!-- if using RTL (Right-To-Left) orientation, load the RTL CSS file after fileinput.css by uncommenting below -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/4.4.9/css/fileinput-rtl.min.css" media="all" rel="stylesheet" type="text/css"/>
<!-- optionally uncomment line below if using a theme or icon set like font awesome (note that default icons used are glyphicons and `fa` theme can override it) -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" media="all" rel="stylesheet" type="text/css" />
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<!-- piexif.min.js is needed for auto orienting image files OR when restoring exif data in resized images and when you
wish to resize images before upload. This must be loaded before fileinput.min.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/4.4.9/js/plugins/piexif.min.js" type="text/javascript"></script>
<!-- sortable.min.js is only needed if you wish to sort / rearrange files in initial preview.
This must be loaded before fileinput.min.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/4.4.9/js/plugins/sortable.min.js" type="text/javascript"></script>
<!-- purify.min.js is only needed if you wish to purify HTML content in your preview for
HTML files. This must be loaded before fileinput.min.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/4.4.9/js/plugins/purify.min.js" type="text/javascript"></script>
<!-- popper.min.js below is needed if you use bootstrap 4.x. You can also use the bootstrap js
3.3.x versions without popper.min.js. -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js"></script>
<!-- bootstrap.min.js below is needed if you wish to zoom and preview file content in a detail modal
dialog. bootstrap 4.x is supported. You can also use the bootstrap js 3.3.x versions. -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" type="text/javascript"></script> <!-- the main fileinput plugin file -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/4.4.9/js/fileinput.min.js"></script>
<!-- optionally uncomment line below for loading your theme assets for a theme like Font Awesome (`fa`) -->
<!-- script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-fileinput/4.4.9/themes/fa/theme.min.js"></script -->
<!-- optionally if you need translation for your language then include locale file as mentioned below -->
<script type="text/javascript" src="../../static/bootstrap/js/zh.js"></script> <!--这个可以去bootstrap cdn找-->

首先创建一个div

<div class="file-loading">
<input id="input-file-1" name="fileName" multiple type="file" accept="image/*" >
</div>

javascript代码

<script>
$("#input-file-1").fileinput({
language: "zh",
uploadUrl: "/goods/add",
autoOrientImage: true,
multiple:true,
maxFileCount:4,
uploadAsync:false,
uploadExtraData:{id:'kv-1',goodsName:"123"}, //额外添加的数据,后台有request.getPara取得
allowedFileExtensions: ["jpg", "jpeg", "gif", "png","bmp"],//单位为kb,如果为0表示不限制文件大小
layoutTemplates:{
// actionDelete: '',//设置为空字符串可以去掉对应的按钮
actionUpload:'',
},
browseClass: 'btn btn-primary'
}).on('fileuploaded', function(event, data) {
//上传成功
alert("成功");
})
.on("fileuploaderror",function (event,data,msg) {
//上传失败
alert("失败");
}) /* .on("filepreremove", function(jqXHR) {
var abort = false;
if (confirm("确定删除此图片?")) {
abort = true;
}
return abort; // 您还可以发送任何数据/对象,你可以接收` filecustomerror
});*/ </script>

后台代码

 @ResponseBody
@RequestMapping(value="/add",method = RequestMethod.POST)
public String insertGoods(@RequestParam("fileName") MultipartFile imageFile[], //同步上床 获取多张图片参数
/*Goods goods,*/
HttpServletRequest request){
System.out.println("hello world"); Goods goods = new Goods();
if(imageFile!=null){
String imgUrl="";
for(int k=0;k<imageFile.length;k++) {
imgUrl += saveImageFile(imageFile[k], request)+",";
} goods.setImgUrl(imgUrl);
}
Date date=new Date();
goods.setUploadTime(date);
GoodsEnum anEnum=goodsService.insertGoods(goods);
if(anEnum.equals(GoodsEnum.INSERT_GOODS_SUCCESS)){
return JSONUtil.toJSON("success");
}else{
return JSONUtil.toJSON("error");
}
}
private String saveImageFile(MultipartFile imageFile, HttpServletRequest request) {
//获取文件上传到服务器的路径
String uploadUrl=getRealPath(request)+"static/uploadImg/";
System.out.println("文件路径为:"+uploadUrl);
//获取从客户端传过来的文件名
String filename=imageFile.getOriginalFilename();
//判断文件上传的路径是否存在,若不存在,则需要创建它
File dir=new File(uploadUrl);
if(!dir.exists()){
dir.mkdirs();
}
//targetFile最终上传的文件,先判断文件是否存在
File targetFile=new File(uploadUrl+filename);
if(!targetFile.exists()){
//如果文件不存在,我们尝试创建它
try {
targetFile.createNewFile();
}catch (IOException e){
e.printStackTrace();
}
}
//使用MultipartFile的transferTo方法保存文件 try {
imageFile.transferTo(targetFile);
}catch (IllegalStateException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return "img/"+filename;
}

bootstrap fileinput +springmvc图片上传-krajee的更多相关文章

  1. Spring Boot+BootStrap fileInput 多图片上传

    一.依赖文件 <link rel="stylesheet" type="text/css" th:href="@{/js/bootstrap/c ...

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

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

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

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

  4. springmvc图片上传(兼容ie8以上,实时预览)

    html代码: <form id="uploadform" method="post" enctype="multipart/form-data ...

  5. SpringMVC 图片上传,检查图片大小

    使用SpringMVC+Spring 前端提交图片文件到Controller,检查上传图片大小是否符合要求 直接上代码了 1.校验图片大小 这里提供出验证的方法,用于在需要校验的地方调用 /** * ...

  6. springmvc图片上传、json

    springmvc的图片上传 1.导入相应的pom依赖 <dependency> <groupId>commons-fileupload</groupId> < ...

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

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

  8. SpringMVC图片上传与显示

    @RestController @Scope("prototype") @RequestMapping("/xxxx/xxx/main") public cla ...

  9. springmvc图片上传

    //-------------------------------------上传图片--------------------------------------------------- @Requ ...

随机推荐

  1. vue封装插件并发布到npm上

    vue封装插件并发布到npm上 项目初始化 首先,要创建项目,封装vue的插件用webpack-simple很合适,vue init webpack-simple 项目名称此命令创建我们的项目的目录, ...

  2. 《Implementing QuantLib》译后记

    目录 <Implementing QuantLib>译后记 初心 瞎忙 收获 彩蛋 展望 就在几天之前,经历了一年时间断断续续的坚持,<Implementing QuantLib&g ...

  3. 华南理工大学“三七互娱杯”程序设计竞赛 HRY and codefire(概率期望DP)

    https://ac.nowcoder.com/acm/contest/874/A 题目:有两个账号 , 一开始都为0级 , 求任意一个账号升级到N的期望 要求:如果当前账号嬴了 , 就继续沿用当前的 ...

  4. DA14580_583_DK_II开发板入门笔记

    本文链接:http://www.cnblogs.com/obarong/p/8521893.html 1.介绍 开发板资料 参考文件: DA1458XDK蓝牙开发板用户须知1.3.pdf DA1458 ...

  5. guava学习:guava集合类型-Bimap

    学习guava让我惊喜的第二个接口就是:Bimap BiMap是一种特殊的映射其保持映射,同时确保没有重复的值是存在于该映射和一个值可以安全地用于获取键背面的倒数映射. 最近开发过程中,经常会有这种根 ...

  6. JavaScript数据结构-3.List

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. java代理-cglib

    前面说到了java的动态代理,但是动态代理依赖于接口,这次来看看cglib来实现的代理... 假设有如下方法,这回没有说接口哦~ package proxy.cglibProxy; public cl ...

  8. MYSQL中数据类型介绍

    一.MySQL的数据类型 主要包括以下五大类: 主要包括以下五大类: 整数类型:bit.  int . bit int . small int . tiny int . medium int .boo ...

  9. [中英对照]Introduction to Remote Direct Memory Access (RDMA) | RDMA概述

    前言: 什么是RDMA? 简单来说,RDMA就是指不通过操作系统(OS)内核以及TCP/IP协议栈在网络上传输数据,因此延迟(latency)非常低,CPU消耗非常少. 下面给出一篇简单介绍RDMA的 ...

  10. sgu-203 Hyperhuffman(哈夫曼编码)

    Hyperhuffman You might have heard about Huffman encoding - that is the coding system that minimizes ...