Struts11---文件上传

01.创建对应的上传页面

<body>
    <form action="user/upload" method="post" enctype="multipart/form-data">
      <input  type="file"  name="upload"/>
      <input  type="submit"  value="登录"/>
    </form>
  </body>

02.创建对应的struts.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

<!--设置开发模式  -->
 <constant name="struts.devMode" value="true"/>
 <!-- 设置文件上传的大小 -->
 <constant name="struts.multipart.maxSize" value="20971520000"/>
 
    <package name="default"  namespace="/user" extends="struts-default">
    <!-- 文件上传 -->
       <action name="upload" class="cn.bdqn.action.UploadAction" method="upload">
            <!--限制 文件上传类型  MIME  服务器中conf文件夹的web.xml  -->
            <interceptor-ref name="fileUpload">
              <param name="allowedTypes">image/jpeg</param>
            </interceptor-ref>
          <interceptor-ref name="defaultStack"/>
         <result name="input">/error.jsp</result>
         <result>/success.jsp</result>
       </action>
    </package>
</struts>

03.创建对应的Action

public class UploadAction extends ActionSupport {
    
    //上传的文件
    private  File upload;
    /**
     * 上传文件的名称
     * 当前系统的毫秒数+随机数+文件名
     */
    private  String  uploadFileName;
    //上传文件的类型
    private  String  uploadContentType;

    
    public  String  upload(){
        System.out.println("进入了 文件上传..........");
        //指定文件上传的位置
        String path = ServletActionContext.getServletContext().getRealPath("/upload");
        //文件的名称   File.separatorChar(系统的分隔符  区分Window 和  Linux)
        String name=path+File.separatorChar+uploadFileName;
        System.out.println("文件的名称是:"+name);
         try {
            FileUtils.copyFile(upload, new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            return INPUT;
        }
        return  SUCCESS;
    }


    public File getUpload() {
        return upload;
    }


    public void setUpload(File upload) {
        this.upload = upload;
    }


    public String getUploadFileName() {
        return uploadFileName;
    }


    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }


    public String getUploadContentType() {
        return uploadContentType;
    }


    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }
    
    
}

04.错误界面!省略!

 

 

05.多个文件上传  界面修改

 <body>
    <form action="user/upload" method="post" enctype="multipart/form-data">
      <input  type="file"  name="upload"/>
      <input  type="file"  name="upload"/>
      <input  type="file"  name="upload"/>
      <input  type="submit"  value="登录"/>
    </form>
  </body>

06.Action修改后的代码

public class UploadAction extends ActionSupport {
    
    //上传的文件
    private  File [] upload;
    /**
     * 上传文件的名称
     * 当前系统的毫秒数+随机数+文件名
     */
    private  String [] uploadFileName;
    //上传文件的类型
    private  String [] uploadContentType;

    
    
    //多文件上传
    public  String  upload(){
        System.out.println("进入了 多文件上传..........");
        //指定文件上传的位置
        String path = ServletActionContext.getServletContext().getRealPath("/upload");
        //文件的名称   File.separatorChar(系统的分隔符  区分Window 和  Linux)
        String name="";
        for (int i = 0; i <upload.length; i++) {
            name=path+File.separatorChar+uploadFileName[i];
            System.out.println("文件的名称是:"+name);
             try {
                FileUtils.copyFile(upload[i], new File(name)); //真正上传
            } catch (IOException e) {
                e.printStackTrace();
                return INPUT;
            }
        }
        return  SUCCESS;
    }


    public File[] getUpload() {
        return upload;
    }


    public void setUpload(File[] upload) {
        this.upload = upload;
    }


    public String[] getUploadFileName() {
        return uploadFileName;
    }


    public void setUploadFileName(String[] uploadFileName) {
        this.uploadFileName = uploadFileName;
    }


    public String[] getUploadContentType() {
        return uploadContentType;
    }


    public void setUploadContentType(String[] uploadContentType) {
        this.uploadContentType = uploadContentType;
    }


    
    
    
}

 

转载于:https://www.cnblogs.com/xtdxs/p/7097534.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值