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

    文件上传
    第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar这两个文件可以从
    http://commons.apache.org/下载
    第二步:把form表的enctype设置为:"multipart/form-data",如下
    <form enctype="multipart/form-data" action="${pageContextrequest.contextPath}/xxx.action" method="post">
      <input type="file" name="uploadImage">
    </form>

    第三步:
    在Action类中添加以下属性,属性uploadImage对应于表单汇总文件字段的名称
    public class HelloWorldAction{
          private File uploadImage;//得到上传的文件
          private String uploadImageContentType;//得到文件的类型ContentType为固定格式
          private String uploadImageFileName;//得到文件的名称 FileName为固定格式
    }

    上传路径pageContext.request.contextPath


    jsp视图页面如下                                                                                                                             必须定义这个参数        以哪种方式提交
    <form action="${pageContext.request.contextPath}/[Action的路径+Action的名称+处理的方法名称]control/employee/list_execute.action"     enctype="multipart/form-data"  method="post">
       文件:<input type="file" name="image">
             <input type="submit" value="上传">
    </form>

    Action中如何写呢?


    package cn.itcast.action;

    import java.io.File;

    public class HelloWorldAction{

         private File image; ///File类型 名称要一样哦
         //如果要得到上传文件的上传的名称如何获取呢?
         private String imageFileName;//属性规则前面为上传字段的名称后面为FileName固定格式

         public String getImageFileName(){
              return imageFileName;
         }
       

         public void setImageFileName(String imageFileName){
                    this.imageFileName=imageFileName
         }

         public File getImage(){
                return image;
         }

         public void setImage(File image){
                 this.image=image;
         }

         public String execute() throws Exception{
               //在这个方法中对文件进行保存  用到工具类 FileUtils 在commons-io-1.3.2.jar包下
                 //得到image的真实路径
                 String realpath=ServletActionContext.getServletContext().getRealPath("/image");//相对于站点的路径
                 System.out.print(realpath);//打印路径
                 if(image != null)
                 {
                     File savefile=new File(new File(realpath),imageFileName);
                     if(!savefile.getParentFile().exists())//如果不存在创建
                     {
                         savefile.getParentFile().mkdirs();//创建
                     }
                         FileUtils.copyFile(image,realpath[目标文件]);
                         ActionContext.getContext().put("message","上传成功");
                 }
                 return "success";
         }
    }

    设置常量
    <!--上传文件的大小限制-->
    <constantname="struts.multipart.maxSize" value="false"/> value可以设置大小 几百兆的文件上传有专门应用程序插件上传

    在输出界面输出的就是EL表达式 ${message}

    =================================================================================

    多文件上传
    Action中的属性的类型全部定义为数组类型

    public String execute() throws Exception{
            String realpath=SeervletActionContext.getServletContext().getRealPath("/iamges");
            System.out.println(realpath);
            if(image != null)
            {
                  File savedir = new File(realpath)
                  if(!savedir.exists()) savedir.mkdirs();
                  for(int i = 0 ; i<image.length;i++)
                  {
                       File savefile = new File(savedir,imageFileName[i]);
                       FileUtils.copyFile(image[i],savefile);
                  }
                  ActionContext.getContext().put("message","上传成功");
            }
              return "success";
    }

    多文件上传总结:
    首先客户端上传字段文件名称要相同另外在Action定义的属性类型为数组类型或者是list类型

  • 相关阅读:
    【面试必备】CSS盒模型的点点滴滴
    2013年终总结——求评点求指导
    【面试必备】javascript的原型和继承
    支持断点续传的文件上传插件——Huploadify-V2.0来了
    【面试必备】javascript操作DOM元素
    走进AngularJs(八) ng的路由机制
    走进AngularJs(七) 过滤器(filter)
    走进AngularJs(六) 服务
    HTML5+flash打造兼容各浏览器的文件上传方案
    免费的HTML5版uploadify送上
  • 原文地址:https://www.cnblogs.com/wuhuisheng/p/2099551.html
Copyright © 2011-2022 走看看