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

2020-12-07 11:52:08 浏览数 (1)

适用于意见反馈

代码语言:javascript复制
 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;
    }
app

0 人点赞