欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

bootstrap fileinput +springmvc图片上传-krajee

程序员文章站 2022-07-04 23:02:37
引入的文件 首先创建一个div javascript代码 后台代码 ......

引入的文件

<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;
    }