• Struts2图片文件上传,判断图片格式和图片大小


    1. 配置Struts2能够上传的最大文件大小

    使用Struts2进行文件上传的时候,Struts2默认文件大小最大为2MB,如果要传大一点的文件,就需要修改struts.xml配置文件,重新设置能够上传的最大的文件大小。配置文件如下:

    <struts>
        <!-- Struts2全局配置 -->
        <constant name="struts.action.extension" value="whtml"/>
        <constant name="struts.locale" value="zh_CN" />
        <constant name="struts.custom.i18n.resources" value="messages"/>
        <constant name="struts.i18n.encoding" value="UTF-8"/>
        <constant name="struts.objectFactory" value="spring"/>
        <constant name="struts.objectFactory.spring.autoWire" value="name"/>
        <constant name="struts.objectFactory.spring.useClassCache" value="true"/>
        <!-- 修改上传的最大文件大小:10MB -->
        <constant name="struts.multipart.maxSize" value="10485760"/>
    
        <!-- 他包都会继承my-json,返回JSON数据 -->
        <package name="my-json" namespace="/" extends="json-default">
            <!-- Struts2拦截器,用户身份认证 -->
            <interceptors>
                <interceptor name="sessionInterceptor" class="com.hanvon.iface.web.interceptor.SessionInterceptor"/>
                <interceptor-stack name="securityStack">
                    <interceptor-ref name="sessionInterceptor"/>
                    <interceptor-ref name="json"/>
                    <interceptor-ref name="defaultStack"/>
                </interceptor-stack>
            </interceptors>
    
            <!-- 设置默认的拦截器栈:拦截所有动作 -->
            <default-interceptor-ref name="securityStack"/>
    
            <!-- 全局results -->
            <global-results>
                <result name="input">/login.html</result>
                <result name="success">/index.html</result>
                <result name="notLoginError" type="redirect">/notLogin.whtml</result>
                <!-- 返回JSON数据类型 -->
                <result name="JSON_RESULT" type="json">
                    <!-- 指定将被Struts2序列化的属性,该属性在action中必须有对应的getter方法 -->
                    <param name="root">jsonResult</param>
                </result>
            </global-results>
        </package>
    
        <!-- 添加外部配置文件 -->
        <include file="struts/userAction.xml"/>
        <include file="struts/loginAction.xml"/>
    </struts>

    注意配置行:<constant name="struts.multipart.maxSize" value="10485760"/>  这个就是设置最大文件大小的,单位是:B(Byte)。

    2.为Action添加文件上传参数

    HTML或者JSP页面添加文件控件:<input type="file" name="imgFile" />

    这里控件命名为:imgFile

    结下来就是在Struts2的Action中添加对象的属性来接受文件:

    Struts2约定的属性名字有三个,如下:

    private File imgFile; //这个是控件的name,你自己命名

    private String imgFileFileName;  //这个是文件名,Struts2会自动处理,按照Struts2约定命名即可

    private String imgFileContentType; //这个是文件类型,如 image/jpg,按照Struts2约定命名即可

    对于“***FileName”和“***ContentType”两个字段,Struts2会自动传递并赋值,当然你可以不设置这2个字段。

    注意:通过imgFile.getFileName() 得到的文件名是 ****.tmp这样的临时文件,无法判断文件类型,因此,如果要判断文件类型,还是需要这两个字段的。

    下面给出一段判断图片类型的代码:

    /** 检测是否是图片文件 */
        private boolean isImageFile() {
            boolean isImage = false;
            String[] imgExts = {".gif", ".jpg", ".jpeg",".bmp", ".png"};
            for(String ext : imgExts) {
                if(imgFileFileName.toLowerCase().endsWith(ext)) {
                    isImage = true;
                }
            }
    
            return isImage;
        }

    判断文件大小:imgFile.length()获取到的就是文件的大小,单位是B(Byte)

    long length = imgFile.length();

    String msg = "您上传的文件大小为:" + (length / 1024)+ "KB";








  • 相关阅读:
    mongoDb学习以及spring管理 (包括百度云配置)
    Python循环嵌套
    Web应用功能测试测试点
    二维数组联通子数组
    二维数组最大子数组问题
    环一维数组最大子数组问题
    电梯调研
    最大子数组问题
    小学生题目
    3.13题目思路
  • 原文地址:https://www.cnblogs.com/riskyer/p/3315418.html
Copyright © 2020-2023  润新知