KKB : springBoot 文件(图片)上传 的 前后台 处理

项目 图片文件上传

注意重复文件名的处理

页面使用基于bootstrap的图片上传插件 fileinput

 

步骤一:

引入相关的依赖的插件

<script src="/assets/js/bootstrap.min.js"></script> 
<link href="/js/fileinput/css/fileinput.css" media="all" rel="stylesheet" type="text/css" />
<script src="/js/fileinput/js/fileinput.js" type="text/javascript"></script> 
<script src="/js/fileinput/js/fileinput_locale_zh.js" type="text/javascript"></script>

步骤二:

方法一:在文件域中设置 class = "file"

<input id="rimageFile" name="rimageFile" class="file" type="file">

注意HTML的input属性 multiple,决定fileinput是否接收多图片上传

方法二:使用js把文件域转化成fileinput对象

<input id="rimageFile" name="rimageFile" type="file" > 
<script >
    $(function(){ 
    $("#rimageFile").fileinput({ 
            
    }); 
    }) 
</script>

 

 

后端处理前端发送的图片文件的处理

 

    @RequestMapping("/doadd")
    public String doAdd(Route route, @RequestParam("rimageFile") MultipartFile rimageFile, HttpServletRequest request) throws IOException {
        //项目部署目录 + img/prduct/rimage/
        String savePath = request.getServletContext().getRealPath("img/product/rimage/");
        //处理文件名重复
        String fileName = UUID.randomUUID().toString().replaceAll("-", "") + "." + FilenameUtils.getExtension(rimageFile.getOriginalFilename());

        //上传目录不存在,先创建
        File savePathDir = new File(savePath);
        if (!savePathDir.exists()) {
            savePathDir.mkdirs();
        }
        //保存文件
        rimageFile.transferTo(new File(savePathDir, fileName));
        //设置route的rimage属性等于图片的相对令
        route.setRimage("img/product/rimage/" + fileName);
        routeService.add(route);
        return "redirect:/admin/route/page";
    }

 

String savePath = request.getServletContext().getRealPath("img/product/rimage/");

获取项目的部署路径,即获取项目存储文件的位置,括号里的路径,表示的是增加 存储的项目路径(不写的话就是直接使用项目的部署路径,这里加上目录可以分层次显示)

String fileName = UUID.randomUUID().toString().replaceAll("-", "") + "." + FilenameUtils.getExtension(rimageFile.getOriginalFilename());

获取到文件名,上传的文件名可能重复,于是通过UUID重新编写文件名,加上文件的扩展名

File savePathDir = new File(savePath);

根据保存路径,创建文件存储路径(目前是空文件)

if (!savePathDir.exists()) {
    savePathDir.mkdirs();
}

文件路径不存在,则创建,此时再本地电脑上创建好了文件存储目录,是空文件

rimageFile.transferTo(new File(savePathDir, fileName));

使用transferTo方法上传文件,在savePath目录下可以看到上传的文件


 

图片数据在前端中回显

                         <!--回显缩略图-->
                        <script th:inline="javascript">
                            $(function () {
                                var rimage = '/' + [[${route.rimage}]];
                                $("#rimageFile").fileinput({
                                    initialPreview: ["<img src='" + rimage + "' class='file-preview-image' >"],
                                    overwriteInitial: true
                                });
                            }) 
                        </script>

 

 

处理线路的详细图

先删除原图,再上传新图,为方法添加事务机制

 

 

controller 层:

多文件的上传保存:

就是多了一个遍历而已,大图上传到大图文件夹,小图上传到小图文件夹

@RequestMapping("/doimage")
    public String doImage(Integer rid,
                          @RequestParam("bigPicFile") MultipartFile[] bigPicFile,
                          @RequestParam("smallPicFile") MultipartFile[] smallPicFile,
                          HttpServletRequest request) throws Exception {
        List<String> bigPic = new ArrayList<>();
        List<String> smallPic = new ArrayList<>();
        String path = request.getServletContext().getRealPath("/");
        for (MultipartFile f : bigPicFile) {
            File bigPath = new File(path + "img\\product\\big-pic\\");
            if (!bigPath.exists()) {
                bigPath.mkdirs();
            }
            String fileName = UUID.randomUUID().toString().replace("-", "") + "." + FilenameUtils.getExtension(f.getOriginalFilename());
            f.transferTo(new File(bigPath, fileName));
            bigPic.add("img/product/big-pic/" + fileName);
        }
        for (MultipartFile f : smallPicFile) {
            File smallPath = new File(path + "img\\product\\small-pic\\");
            if (!smallPath.exists()) {
                smallPath.mkdirs();
            }
            String fileName = UUID.randomUUID().toString().replace("-", "") + "." + FilenameUtils.getExtension(f.getOriginalFilename());
            f.transferTo(new File(smallPath, fileName));
            smallPic.add("img/product/small-pic/" + fileName);
        }
        //要添加的图片列表
        List<RouteImg> ris = new ArrayList<>();
        for (int i = 0; i < bigPic.size(); i++) {
            RouteImg img = new RouteImg();
            img.setRid(rid);
            img.setBigpic(bigPic.get(i));
            img.setSmallpic(smallPic.get(i));
            ris.add(img);
        }
        imgService.saveImg(rid, ris);
        return "redirect:/admin/route/page";
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在安装Hadoop环境时,需要将Hadoop压缩包分发到指定路径。根据引用中的内容,需要将Hadoop压缩包分别放置在以下两个路径下:/kkb/install/hadoop-2.6.0-cdh5.14.2/hadoop-hdfs-project/hadoop-hdfs-httpfs/downloads和/kkb/install/hadoop-2.6.0-cdh5.14.2/hadoop-common-project/hadoop-kms/downloads。 另外,在分发Hadoop环境时,还需要进行一些配置。根据引用和引用[3]中的内容,可以按照以下步骤进行Hadoop环境变量的分发: 1. 在第一台机器上执行命令,进入Hadoop配置文件所在目录: ``` cd /kkb/install/hadoop-2.6.0-cdh5.14.2/etc/hadoop ``` 2. 修改core-site.xml文件,可使用文本编辑器打开该文件: ``` vim core-site.xml ``` 3. 在core-site.xml文件中添加以下配置信息: ``` <configuration> <property> <name>fs.defaultFS</name> <value>hdfs://node01:8020</value> </property> <property> <name>hadoop.tmp.dir</name> <value>/kkb/install/hadoop-2.6.0-cdh5.14.2/hadoopDatas/tempDatas</value> </property> <!-- 缓冲区大小,实际工作中根据服务器性能动态调整 --> <property> <name>io.file.buffer.size</name> <value>4096</value> </property> <!-- 开启hdfs的垃圾桶机制,删除掉的数据可以从垃圾桶中回收,单位分钟 --> <property> <name>fs.trash.interval</name> <value>10080</value> </property> </configuration> ``` 4. 修改hdfs-site.xml文件,可使用文本编辑器打开该文件: ``` vim hdfs-site.xml ``` 5. 根据引用中的内容,在node01机器上创建所需的目录: ``` mkdir -p /kkb/install/hadoop-2.6.0-cdh5.14.2/hadoopDatas/tempDatas
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你在狗叫什么、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值