栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

ssm上传多张照片保存到数据库在别的页面显示

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ssm上传多张照片保存到数据库在别的页面显示

前端

上传多个照片并保存

上传图片:

效果

controller

 @RequestMapping(value = "/addImg")
    private String addImg(@RequestParam(value = "file", required = false) CommonsMultipartFile[] file,
                          HttpServletRequest request,Model model) throws Exception {
        //定义序号
        int count = 1;
        for (CommonsMultipartFile mf : file) {
            System.out.println(mf.toString());
            if (!mf.isEmpty()) {
                // 使用UUID给图片重命名,并去掉四个“-”
                String name = UUID.randomUUID().toString().replaceAll("-", "");
                // 获取文件的扩展名
                String ext = FilenameUtils.getExtension(mf
                        .getOriginalFilename());
                // 设置图片上传路径
                String url = request.getSession().getServletContext()
                        .getRealPath("/upload");
                System.out.println(url);
                // 以绝对路径保存重名命后的图片
                mf.transferTo(new File(url + "/" + name + "." + ext));
                // 把图片存储路径保存到数据库
                String path = "upload/" + name + "." + ext;
                if(count==1){
                    imginfo.AddImge(path);
                }else if(count==2){
                    imginfo.AddImge(path);
                }else if(count==3){
                    imginfo.AddImge(path);
                }
            }
            count++;
        }
        List imginfoList = imginfo.GetAllAddress();
        System.out.println(imginfoList);
        Object[] objects = imginfoList.toArray();
        System.out.println(objects);
        model.addAttribute("img",objects);
        return "hello";
    }

maven包


        
            commons-fileupload
            commons-fileupload
            1.3.1
        
        
            commons-io
            commons-io
            2.4
        

xml配置

 
    
        
        
        
        
        
    

展示页面

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%--  记得引入jquery  --%>

多个照片

效果

pojo、dao和sql就不写了!!!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/940563.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号