读书人

struts2 下传 验证图片大小 长宽

发布时间: 2012-12-26 14:39:28 作者: rapoo

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;    }

读书人网 >编程

热点推荐