⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uploadedfileoper.java

📁 tbuy1.1.5是在netbeans环境下用JSF技术编写的一个论坛tbuy1.1.5是在netbeans环境下用JSF技术编写的一个论坛
💻 JAVA
字号:
/* * 作者: 胡李青 * qq: 31703299 * Copyright (c) 2007 huliqing * 主页 http://www.tbuy.biz/ * 你可以免费使用该软件,未经许可请勿作用于任何商业目的,如有技术问题请与本人联系! * * 该类主要用于简化操作上传文件,依赖于 * org.apache.myfaces.custom.fileupload.UploadedFile */package biz.tbuy.common;import biz.tbuy.common.logs.Elog;import java.awt.Image;import java.util.Date;import java.io.InputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.util.Date;import java.util.List;import javax.imageio.ImageIO;import org.apache.myfaces.custom.fileupload.UploadedFile;/** * @version 1.0 * @author huliqing * <p><b>qq:</b>31703299 * <p><b>E-mail:</b><a href="mailto:huliqing.cn@gmail.com">huliqing.cn@gmail.com</a> * <p><b>Homepage:</b><a href="http://www.tbuy.biz/">http://www.tbuy.biz/</a> */public class UploadedFileOper {    private UploadedFile _file;    private String _name;        // 文件名,包含后缀    private String _newName;     // 文件名(新) 由时间秒 + 原后缀组成    private String _type;        // 文件类型,如:application,image    private String _suffix;      // 文件后缀,如:jpg,gif,    private String _contentType; // contentType类型    private int _size;           // 文件大小(字节数)    private int _imageWidth;     // 图片文件的宽度(如果是图片)    private int _imageHeight;    // 图片文件的高度(如果是图片)                /** 关于图片类型的文件 *****************************************************/    private List<String> _suffixAllow; // 上传文件所允许的类型    private int _maxWidthAllow;      // 上传文件允许的最大宽度(如果是图片)    private int _maxHeightAllow;     // 上传文件允许的最大高度(如果是图片)    private int _maxSizeAllow;       // 上传文件允许的最大Size(以K为单位)                public UploadedFileOper(UploadedFile file) {        _file = file;        init();    }    public void setFile(UploadedFile file) {        _file = file;    }        public UploadedFile getFile() {        return _file;    }        /**     * 设置允许的文件后缀名     * @param suffixAllow list集合,包含String类型     */     public void setSuffixAllow(List<String> suffixAllow) {        _suffixAllow = suffixAllow;    }        /**     * 获取允许的文件后缀类型     * @return suffixAllow list集合,包含String类型     */     public List<String> getSuffixAllow() {        return _suffixAllow;    }        /**     * 设置允许的图片类型的宽度     * @param maxWidthAllow 允许的宽度(int)     */     public void setMaxWidthAllow(int maxWidthAllow) {        _maxWidthAllow = maxWidthAllow;    }        /**     * 获取所允许的图片类型的宽度     * @return maxWidthAllow     */     public int getMaxWidthAllow() {        return _maxWidthAllow;    }        /**     * 设置允许的图片类型的高度     * @param maxHeightAllow 允许的高度(int)     */     public void setMaxHeightAllow(int maxHeightAllow) {        _maxHeightAllow = maxHeightAllow;    }        /**     * 获取所允许的图片类型的高度     * @return maxHeightAllow(int)     */     public int getMaxHeightAllow() {        return _maxHeightAllow;    }        /**     * 设置允许的文件的大小(int类型/单位K)     * @param maxSizeAllow 允许的文件大小     */     public void setMaxSizeAllow(int maxSizeAllow) {        _maxSizeAllow = maxSizeAllow;    }        /**     * 获取所允许的文件大小(int类型/单位K)     * @return maxSizeAllow 允许的文件大小     */     public int getMaxSizeAllow() {        return _maxSizeAllow;    }        /** method *****************************************************************         /**     * 返回文件的类型,如:image/application等,该类型来自于     * ContentType的类型     * @return type 文件的类型     */    public String getType() {        return _type;    }        /**     * 获取文件的后缀名,如:jpg,gif,rar,zip等     * @return jpg gif rar zip ...     */    public String getSuffix() {        return _suffix;    }        /**     * 获取文件的字节数(bytes),为long类型     * @return size 文件的字节数(long)     */    public long getSize() {        return _size;    }        /**     * 获取文件的ContentType     * @return ContentType     */    public String getContentType() {        return _contentType;    }    /**     * 获取文件的输入流     * @return InputStream 或者返回null     * @throws IOException     */    public InputStream getInputStream() throws IOException {        try {            return _file.getInputStream();        } catch (IOException ioe) {            return null;        }    }        /**     * 获取文件名     * @return String     */    public String getName() {        return _name;    }        /**     * 获取文件的字节数组byte[]     * @return byte[]     * @throws IOException     */    public byte[] getBytes() throws IOException {        return _file.getBytes();    }    /**     * 该方法可获得文件的新名称,由     * 当前的时间与1970.0.0的秒差数 + "." + 文件的原类型组成新的文件名     * @return newName     */    public String getNewName() {        return _newName;    }        /**     * 该方法可用于将文件保存至本地路径     * @param path 本地路径,即需要保存文件的路径     * @return true 如果保存成功,否则返回false     */    public boolean saveTo(String path) {        if (_file == null) return false;        try {            InputStream is = _file.getInputStream();            FileOutputStream fos = new FileOutputStream(path);            byte[] buff = new byte[1024];            int len;            while ((len = is.read(buff)) != -1) {                fos.write(buff, 0, len);            }            fos.flush();            fos.close();            is.close();            return true;        } catch (IOException ioe) {            Elog.log("UploadedFileOper:saveTo:" + ioe.getMessage());        }        return false;    }        /**     * 该文件写入指定路径,使用当前平台的默认字符集     * @param path 写入的路径     */     public boolean writeTo(String path) {       return writeTo(path, null);    }        /**     * 使用指定的路径,并用指定的字符集     * @param path 写入的路径     * @param charset 所使用的字符集     * @return true 如果写入成功 否则false     */     public boolean writeTo(String path, String charset) {        if (_file == null) return false;        try {            InputStream is = _file.getInputStream();            InputStreamReader isr = new InputStreamReader(is);            FileOutputStream fos = new FileOutputStream(path);            OutputStreamWriter osw;            if (charset != null) {                 osw = new OutputStreamWriter(fos, charset);            } else {                osw = new OutputStreamWriter(fos);            }            char[] buff = new char[1024];            int len;            while ((len = isr.read(buff, 0, buff.length)) != -1) {                osw.write(buff, 0, len);            }            isr.close();            osw.close();            return true;        } catch (IOException ioe) {            Elog.log("UploadedFileOper:writeTo:" + ioe.getMessage());        }        return false;    }    /** ***********************************************************************     * 获取文件的宽度,如果该文件为图片     * @return width 图片文件的宽度,非图片则返回0     */    public int getImageWidth() {        return _imageWidth;    }    /**     * 获取文件的宽度,如果该文件为图片     * @return height 图片文件的高度,非图片则返回0     */    public int getImageHeight() {        return _imageHeight;    }        /**     * 关于检查文件是否被允许,如果设置了     * typeAllow;     * maxWidthAllow;     * maxHeightAllow;     * maxSizeAllow;     * 则可通过该方法判断文件是否被允许,如果有一项不被允许,     * 则该方法返回相应int数字标识,否则返回-1,或者不设置上面的限制,     * 该方法也会返回-1     * @return      * -1.文件正常通过检查     * 1.文件大小超过限制     * 2.文件后缀不被允许     * 3.文件宽度超过限制(如果是图片文件)     * 4.文件高度超过限制(如果是图片文件)     */     public int checkAllow() {        int ok = -1;        // 检查文件大小是否被允许        if (_maxSizeAllow != 0 && (_size / 1024) > _maxSizeAllow) {            return 1;        }        // 检查文件类型是否被允许        if (_suffixAllow != null) {            if (!isInside(_suffix.toLowerCase(), _suffixAllow)) {                return 2;            }        }        if (_type.equals("image")) {            // 宽度不被允许            if (_maxWidthAllow != 0 && _imageWidth > _maxWidthAllow) {                return 3;            }            // 高度不被允许            if (_maxHeightAllow != 0 && _imageHeight > _maxHeightAllow) {                return 4;            }        }        return ok;    }        /**     * 检查一个字符串是否存在于另一个List类型的集合中     * @param type 需要判断的字符串,如果为空则返回false     * @param types 已有的字符串集合     * @return true 如果type存在于types中,否则返回false;     */     private boolean isInside(String type, List<String> types) {        if (type.equals("")) return false;        return types.contains(type) ? true : false;    }        /**     * 初始化UploadedFileOper     */     private void init() {        if (_file == null) return;        _name = _file.getName();        _suffix = _name.substring(_name.lastIndexOf(".") + 1);        _newName = new Date().getTime() + "." + _suffix;        _contentType = _file.getContentType();        _type = _contentType.substring(0, _contentType.indexOf("/")).trim();        _size = Math.round(_file.getSize());        if (_type.equals("image")) {            try {                Image img = ImageIO.read(_file.getInputStream());                _imageHeight = img.getHeight(null);                _imageWidth = img.getWidth(null);                img.flush();            } catch (IOException ioe) {                Elog.log("UploadedFileOper:init:/" + ioe.getMessage());            }        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -