Erlo

bootstrap fileinput +springmvc图片上传-krajee

2018-10-18 00:54:55 发布   458 浏览  
页面报错/反馈
收藏 点赞

引入的文件

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">-->
 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代码

后台代码

 @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) {
                 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;
    }

 

登录查看全部

参与评论

评论留言

还没有评论留言,赶紧来抢楼吧~~

手机查看

返回顶部

给这篇文章打个标签吧~

棒极了 糟糕透顶 好文章 PHP JAVA JS 小程序 Python SEO MySql 确认