前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >多文件上传公共方法

多文件上传公共方法

作者头像
用户5899361
发布2020-12-07 11:51:52
8660
发布2020-12-07 11:51:52
举报
文章被收录于专栏:学习java的小白

Map接受

代码语言:javascript
复制
 /**
     * 通过;spring 的方法解析所有方法
     * @param multipartRequest  请求
     * @param keys  获取参数key 对应的文件 key的值必须和前台页面的name值一样
     * @param systemPath    系统路径(活路径)
     * @param path  自己目录的路径
     * @return
     */
    public static String[]  getFileURL(MultipartHttpServletRequest multipartRequest, String[] keys,String systemPath,String path){
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        String[] vales=new String[keys.length];
        for (int i=0;i<keys.length;i++) {
            vales[i]=getPathValue(fileMap.get(keys[i]),systemPath,path);
        }
        return vales;
    }
代码语言:javascript
复制
  protected static String getPathValue(MultipartFile file,String systemPath,String path){
        if (!file.isEmpty()) {
            try {
                String fileName=file.getOriginalFilename();
                if(StringUtils.isNotBlank(fileName)){
                    String[] str=fileName.split("\\.");
                    if(str.length==1){
                        path=path+new Date().getTime()+str[0]+".JPEG";
                    }else{
                        path=path+new Date().getTime()+"."+str[str.length-1];
                    }

                }
                // 文件保存路径
                File fl=new File(systemPath+path);
                if(!fl.getParentFile().exists()){
                    fl.getParentFile().mkdirs();
                }
                // 转存文件
                file.transferTo(fl);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else{
            return null;
        }
        return  path;
    }

多文件一个文件名

代码语言:javascript
复制
 MultipartHttpServletRequest mr=(MultipartHttpServletRequest) request;

        Iterator<String> ite=mr.getFileNames();
        while (ite.hasNext()) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            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);

                }
            }
        }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档