当前位置:网站首页>App向后台传不固定张数的图片

App向后台传不固定张数的图片

2020-12-07 13:01:24 用户5899361

适用于意见反馈

 public Map<String, Object> saveOpinion(HttpServletRequest request, String title, String situationLevel, String content,String userId) {
        Map<String, Object> map=new HashMap<String, Object>();
        AppOpinionEntity opinionEntity=new AppOpinionEntity();
        opinionEntity.setTitle(title);
        opinionEntity.setContent(content);
        opinionEntity.setSituationLevel(situationLevel);
        opinionEntity.setUserId(userId);
        opinionEntity.setCreateBy("1");
        opinionEntity.setCreateDate(DateTimeUtils.getDateTime());
        opinionEntity.setUpdateBy("1");
        opinionEntity.setUpdateDate(DateTimeUtils.getDateTime());
        opinionEntity.setDelTag("0");

        opinionDao.save(opinionEntity);
        MultipartHttpServletRequest mr=(MultipartHttpServletRequest) request;

        Iterator<String> ite=mr.getFileNames();
        while (ite.hasNext()) {
            String name = ite.next();
            List<MultipartFile> file = mr.getFiles(name);

            if (file != null || file.size() != 0) {
                for (MultipartFile files : file) {
                    AppOpinionImgEntity opinionImgEntity = new AppOpinionImgEntity();
                    opinionImgEntity.setCreateBy("1");
                    opinionImgEntity.setCreateDate(DateTimeUtils.getDateTime());
                    opinionImgEntity.setUpdateBy("1");
                    opinionImgEntity.setUpdateDate(DateTimeUtils.getDateTime());
                    opinionImgEntity.setDelTag("0");

                    String sysPath = request.getSession().getServletContext().getRealPath("/");
                    String path = "upload/opinion/";

                    try {
                        String fileName = files.getOriginalFilename();

                        opinionImgEntity.setImgName(fileName);
                        opinionImgEntity.setOpinionId(opinionEntity.getId());


                        if (StringUtils.isNotBlank(fileName)) {
                            String[] str = fileName.split("\\.");
                            if (str.length == 1) {
                                path = path + new Date().getTime() + str[0] + ".JPEG";//苹果不带后缀图片处理
                                opinionImgEntity.setImgType("JPEG");
                            } else {
                                path = path + new Date().getTime() + "." + str[str.length - 1];
                                opinionImgEntity.setImgType(fileName.substring(fileName.lastIndexOf(".")));//获取图片格式
                            }

                        }
                        // 文件保存路径
                        File fl = new File(sysPath + path);
                        if (!fl.getParentFile().exists()) {
                            fl.getParentFile().mkdirs();
                        }
                        // 转存文件
                        files.transferTo(fl);

                    } catch (Exception e) {
                        e.printStackTrace();
                        return new HashMap<String, Object>();
                    }
                    opinionImgEntity.setImgUrl(path);
                    opinionImgDao.save(opinionImgEntity);

                }
            }
        }
        map.put("type", "1");
        return map;
    }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

版权声明
本文为[用户5899361]所创,转载请带上原文链接,感谢
https://cloud.tencent.com/developer/article/1757175