首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

struts2 下传 验证图片大小 长宽

2012-12-26 
struts2 上传 验证图片大小 长宽/*** 验证图片大小 限制在一M内* @param file* @return*/public HashMapS

struts2 上传 验证图片大小 长宽

/**     * 验证图片大小 限制在一M内     * @param file     * @return     */    public HashMap<String, Object> checkSize(File file){        HashMap<String, Object> map=new HashMap<String, Object>();        FileInputStream ins;        try{            ins = new FileInputStream(file);            //判断文件是否超过一M            if (ins.available() > 1024 * 1024 *1) {                file.delete();                map.put("success", false);                map.put("msg", "上传文件不得超过1M");            }else{                map.put("success", true);                map.put("msg", "");            }        }catch (FileNotFoundException e){            log.error(e.getMessage());            map.put("success", false);            map.put("msg", "系统读取图片时出现异常,请联系管理员");        }catch (IOException e){            log.error(e.getMessage());            map.put("success", false);            map.put("msg", "系统读取图片时出现异常,请联系管理员");        }        return map;    }    /**     * 检测文件长度是否符合     * @param height     * @return     */    public HashMap<String, Object> checkImgSize(File file,String width,String height){        HashMap<String, Object> map=new HashMap<String, Object>();        Image src;        try{            src = javax.imageio.ImageIO.read(file);            int w = src.getWidth(null);              int h = src.getHeight(null);            if(width.equals(w+"") && height.equals(h+"")){                map.put("success", true);                map.put("msg", "true");            }else{                map.put("success", false);                map.put("msg", "文件上传失败,请上传图片宽:"+width+" 高:"+height);            }        }catch (IOException e){            log.error(e.getMessage());            e.printStackTrace();            map.put("success", false);            map.put("msg", "系统读取图片大小出现异常,请联系管理或调整图片");        }        return map;    }

热点排行