zoukankan      html  css  js  c++  java
  • java 文件上传 struts2.0实现

    1. web.xml  写全文拦截器

    WEBROOT--- WEB-INF ---- WEB.XML

    <filter>
    <filter-name>Struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>Struts2</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

    2.写个文件上传页面

    WEBROOT--E文件夹下----- upload.jsp

      <body>
              <form action="${pageContext.request.contextPath }/fileuploadaction" method="post" enctype="multipart/form-data">
              用户名:<input type="text" name="username"><br/>
              文件:<input type="file" name="file1"><br/>
              <input type="submit" value="上传">
          </form>
      </body>

    3. 项目文件下     struts.xml

    <struts>
    
        <!-- 二、总配置文件:引入其他所有配置文件 -->
        
    
        <include file="cn/itcast/e_fileupload/upload.xml"></include>
        
    </struts>

    4.  项目src下   建立包: cn.itcast.e_fileupload      下 新建  upload.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>
    <package name="upload_" namespace="/" extends="struts-default"   abstract="false">
    <action name="fileuploadaction"  class="cn.itcast.e_fileupload.fileupload"  >
    <result name="success">/e/success.jsp</result>
    </action>
    </package>
    </struts>

    5. cn.itcast.e_fileupload 下新建 fileupload.java

    package cn.itcast.e_fileupload;
    
    import java.io.File;
    
    import org.apache.commons.io.FileUtils;
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class fileupload extends ActionSupport{
        private File file1;
        private  String file1FileName;          //该变量 固定格式   "file1" + FileName(注意大小写) 
        private String file1ContextType;        //该变量 固定格式   
        public void setFile1(File file1) {
            this.file1 = file1;
        }
        public void setFile1filename(String file1FileName) {
            this.file1FileName = file1FileName;
        }
        public void setFile1contexttype(String file1ContextType) {
            this.file1ContextType = file1ContextType;
        }
        @Override
        public String execute() throws Exception {
            String path=ServletActionContext.getServletContext().getRealPath("/upload");
            File desfile=new File(path,file1FileName);
            FileUtils.copyFile(file1, desfile);
                 return SUCCESS;
        }
    
    }
  • 相关阅读:
    获取JVM的dump文件
    jmeter正则表达式提取器提取特定字符串后的全部内容
    mysql数据库开启慢查询日志
    正则中需要转义的特殊字符
    LoadRunner 调用Dll完成加密解密
    压缩十进制数据的一次实践
    记Judith此人和我对美国教育的感触
    在 sql server 中,不允许用户查看到所有数据库
    在 asp.net core 中使用 HttpContext.Current
    nginx 配置将某站点所有页面指向一个路径
  • 原文地址:https://www.cnblogs.com/yimian/p/7514659.html
Copyright © 2011-2022 走看看