web项目上传图片。

上传一张图片

//Controller代码
 try {
            MultipartHttpServletRequest request = (MultipartHttpServletRequest) this.getRequest();
            MultipartFile shop_logo = request.getFile("file_shop_logo");
            if (!shop_logo.isEmpty()) {
                String img_shop_logo = WeiitUtil.uploadFile(shop_logo);
                formMap.put("shop_logo",img_shop_logo);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception("出错了");
        }
//jsp代码
 

建议尺寸:245 * 149 像素

上传多张图片

//Controller代码
 try {
            MultipartHttpServletRequest request = (MultipartHttpServletRequest) this.getRequest();
            String[] imgArr = this.getRequest().getParameterValues("imagesList");
            if (imgArr!=null) {
                List arrList= Arrays.asList(imgArr);
                imagesList= new ArrayList(arrList);
            }
            Map fileMap = request.getFileMap();
            if (fileMap!=null && !fileMap.isEmpty()) {
                for (Iterator iterator=fileMap.keySet().iterator();iterator.hasNext();) {
                    String key = iterator.next().toString();
                    MultipartFile mFile = fileMap.get(key);
                    if (mFile != null) {
                        String img_url = WeiitUtil.uploadFile(mFile);
                        imagesList.add(mg_url);
                    }
                }
            }
            formMap.put("imagesList", imagesList);
        }catch (Exception e){
            e.printStackTrace();
            throw new Exception("出错了");
        }
        String product_img="";
        if (imagesList != null && imagesList.size()>0) {
            product_img=imagesList.get(0).toString();
        }
         formMap.put("product_img", product_img);
//jsp代码
 建议尺寸:600*600 像素。

你可能感兴趣的:(web项目上传图片。)