zoukankan      html  css  js  c++  java
  • struts2 文件上传

    struts2上传图片的过程

    1、写一个上传的jsp页面upload_image.jsp,内容如下:

    <body>
    <center>
        <font color="red"><s:fielderror/></font>
            <s:form action="uploadOne" method="post" enctype="multipart/form-data">
                <s:file name="file" label="文件1"></s:file>
                <s:file name="file" label="文件2"></s:file>
                <s:file name="file" label="文件3"></s:file>
                <s:file name="file" label="文件4"></s:file>
                <s:submit label="上传"/>
            </s:form>    
    </center>
    </body>
    

      



    解析:A、 form里面的method必须是post,enctype="multipart/form-data"上传文件必须这样写
          B、<s:fielderror/>这个是图片格式或者大小出错的错误提示---要在struts.xml里面先配置
          C、name="file",批量上传name的值要一样


    2、创建一个action--我的包是com.upload.one

    public class UploadImageAction extends ActionSupport{
        private List<File> file;
        private List<String> fileFileName;
        private List<String> fileContentType;
        
        public String execute() throws IOException{
        //得到工程保存图片的路径
            String root = ServletActionContext.getRequest().getRealPath("/upload");
            
            //循环上传的文件
            for(int i = 0 ; i < file.size() ; i ++){
                InputStream is = new FileInputStream(file.get(i));
                
                //得到图片保存的位置(根据root来得到图片保存的路径在tomcat下的该工程里)
                File destFile = new File(root,this.getFileFileName().get(i));
                
                //把图片写入到上面设置的路径里
                OutputStream os = new FileOutputStream(destFile);
                byte[] buffer = new byte[400];
                int length  = 0 ;
                while((length = is.read(buffer))>0){
                    os.write(buffer, 0, length);
                }
                is.close();
                os.close();
            }
            return SUCCESS;
        }
    }
    

      


    解析:在这个action里面做了几个测试
         要在WebRoot下面新建一个文件夹--upload
        A、一个一个的打印,明白每个变量到底是干嘛的,到底得到些什么值
        B、我图片存放的路径改到了自己想要的路径下面,结果在页面显示的时候,决绝路径无法显示
           可能在显示的时候哪里写错了
            C、刚开始以后要把得到的图片的名称一个一个得手动添加到fileFileName,结果用
           fileFileName.add(图片名称)后,fileFileName的length比添加之前多了一倍
               于是明白,在命名的时候要遵循一个规律,这个样的话fileFileName自动的把图片名称
           一个一个的添加到里面,不用自己添加


    3、配置struts.xml文件

    <struts>
        <!-- 指定国际化资源文件的baseName为messageResource -->
         <constant name="struts.custom.i18n.resources" value="messageResource"/>
        
        <!-- 设置该应用使用的解码集 -->
         <constant name="struts.i18n.encoding" value="utf-8"/>
     
         <!-- 上传的全部图片的最大限制-->
         <constant name="struts.multipart.maxSize" value="1024102400"/>
         
        <!-- 配置action->
        <package name="default" extends="struts-default">
            <action name="uploadOne" class="com.upload.one.UploadImageAction" >
            
                <!-- 限制图片的格式和图片的大小 -->
                <interceptor-ref name="fileUpload">
                    <param name="allowedTypes">
                        image/bmp,image/png,image/gif,image/jpeg,image/jpg
                    </param>
                    <param name="maximumSize">102400</param>
                </interceptor-ref>
                
                <!-- 默认的拦截器,必须要写 -->
                <interceptor-ref name="defaultStack" />
                
                <result name="success">/showImage.jsp</result>
                <result name="input">/upload_image.jsp</result>
            </action>
        </package>
    
        <constant name="struts.multipart.saveDir" value="d:/test"></constant>
    </struts>
    

      


    解析:
        A、因为在这里我限制了图片的格式和大小,如果不配置国际化资源文件,那么在页面引入<s:fielderror/>
           的时候,显示出的内容不友好,于是自己定义出错显示的内容

        B、在action配置里面,限制了单张图片的大小<param name="maximumSize">102400</param>
           如果不再package外面(<constant name="struts.multipart.maxSize" value="1024102400"/>)限制总的上传大小
                那么,当你上传的单个图片超过限定的大小,没事反应,但是后台会报错

        C、<constant name="struts.multipart.saveDir" value="d:/test"></constant>临时存放文件的路径
           如果不要这个路径,就会在控制台打印Unable to find 'struts.multipart.saveDir' property setting. Defaulting to        javax.servlet.context.tempdir




    4、messageResource_zh_CN.properties配置文件的内容

    #上传文件类型不允许的提示信息
    struts.messages.error.content.type.not.allowed=\u4E0A\u4F20\u7C7B\u578B\u9519\u8BEF
    
    #上传文件太大的提示信息
    struts.messages.error.file.too.large=\u4E0A\u4F20\u6587\u4EF6\u592A\u5927
    

      



    解析:当图片的格式不对,上传大小不对时,就会在页面显示相应的错误信息



    5、把上传的图片显示出来
     

    <body>
          <s:iterator value="fileFileName" status="length">
                  <img src='upload/<s:property value="fileFileName.get(#length.index)"/>'>
          </s:iterator>
      </body>
    

      

  • 相关阅读:
    Java实现 LeetCode 50 Pow(x,n)
    Java实现 LeetCode 50 Pow(x,n)
    Java实现 LeetCode 49 字母异位词分组
    Java实现 LeetCode 49 字母异位词分组
    Java实现 LeetCode 49 字母异位词分组
    Java实现 LeetCode 48 旋转图像
    Java实现 LeetCode 48 旋转图像
    Java实现 LeetCode 48 旋转图像
    Java实现 LeetCode 47 全排列 II(二)
    Java实现 LeetCode 47 全排列 II(二)
  • 原文地址:https://www.cnblogs.com/yongde/p/2826164.html
Copyright © 2011-2022 走看看