当前位置:网站首页>Public method of multi file upload

Public method of multi file upload

2020-12-07 13:01:25 User 5899361

Map Accept

 /**
     *  adopt ;spring  Methods of parsing all methods 
     * @param multipartRequest   request 
     * @param keys   To obtain parameters key  Corresponding file  key Must be the same as the foreground page name Have the same value 
     * @param systemPath     System path ( Living path )
     * @param path   The path to your own directory 
     * @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;
    }
  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 save path 
                File fl=new File(systemPath+path);
                if(!fl.getParentFile().exists()){
                    fl.getParentFile().mkdirs();
                }
                //  File transfer 
                file.transferTo(fl);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else{
            return null;
        }
        return  path;
    }

Multiple files one file name

 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 save path 
                        File fl = new File(sysPath + path);
                        if (!fl.getParentFile().exists()) {
                            fl.getParentFile().mkdirs();
                        }
                        //  File transfer 
                        files.transferTo(fl);

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

                }
            }
        }

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[User 5899361]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201207125830423s.html