• struts2中实现文件的上传


    文件上传的action,同时过滤上传的文件格式只对满足要求的格式支持上传

    package com.inspur.action;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;

    import org.apache.struts2.ServletActionContext;

    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;

    public class UploadAction extends ActionSupport {
    private String title;
    private File upload;
    private String uploadFileName;
    private String uploadContentType;
    private String savePath;
    private String allowTypes;

    public String getAllowTypes() {
    return allowTypes;
    }
    public void setAllowTypes(String allowTypes) {
    this.allowTypes = allowTypes;
    }
    public String getTitle() {
    return title;
    }
    public void setTitle(String title) {
    this.title = title;
    }
    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;
    }
    public String getSavePath() {
    return ServletActionContext.getRequest().getRealPath(savePath);
    }
    public void setSavePath(String savePath) {
    this.savePath = savePath;
    }
    public String filterType(String[]types){
    String fileType=this.getUploadContentType();
    for(String type:types){
    if(type.equals(fileType)){
    return null;
    }

    }
    return INPUT;
    }
    public String upload()throws Exception{
    if(this.getAllowTypes()==null){
    System.out.println("lzhq");
    }


    String filterResult=this.filterType(this.getAllowTypes().split(","));
    if(filterResult!=null){
    ActionContext.getContext().put("typeError", "this file you want to upload is error");
    return filterResult;
    }
    FileOutputStream fos=new FileOutputStream(this.getSavePath()+"\\"+this.getUploadFileName());
    FileInputStream fis=new FileInputStream(this.getUpload());
    byte[]buffer=new byte[1024];
    int len=0;
    while((len=fis.read(buffer))>0){
    fos.write(buffer,0,len);
    }
    return SUCCESS;
    }

    }

    在struts.xml文件中提供上传文件保存路径的动态配置和允许的文件上传格式的动态配置

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
    <struts>
    <constant name="struts.custom.i18n.resources" value="message"></constant>
    <constant name="struts.i18n.encoding" value="gbk"></constant>
    <package name="upload" extends="struts-default" namespace="/">
    <action name="upload" class="com.inspur.action.UploadAction" method="upload">
    <param name="allowTypes">image/bmp,image/png,image/gif,image/jpeg</param>
    <param name="savePath">/upload</param>
    <result name="success">/success.jsp</result>
    <result name="input">/upload.jsp</result>
    </action>
    </package>

    </struts>

    上传成功的转向页面:

    <body>
    上传的文件标题是:<s:property value="title"/><br/>
    上传的图片是: <img alt="upload" src="<s:property value="'upload/'+uploadFileName"/>"/>
    </body>

    文件上传页面jsp文件:

    <body>
    ${requestScope.typeError }
    <form action="upload.action" method="post" name="uploadForm" enctype="multipart/form-data">
    文件标题:<input type="text" name="title"/><br/>
    选择文件:<input type="file" name="upload"/><br/>
    <input type="submit" value="upload"/>
    </form>

    </body>

    在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>*.action</url-pattern>
    </filter-mapping>
    <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.jsp</url-pattern>
    </filter-mapping>
    <filter>
    <filter-name>struts-cleanup</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>struts-cleanup</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

    其中org.apache.struts2.dispatcher.ActionContextCleanUp类与文件的上传无关,但是在文件上传的应用中会出现无法预测的异常,添加该过滤器后异常消失。本身org.apache.struts2.dispatcher.ActionContextCleanUp类就是org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter类的辅助类,此处只是出于系统稳定性的考虑,与应用的文件上传功能没有直接的关系。

    注意:struts2的文件上传默认使用的是common-fileupload和common-io文件上传 框架,所以需要导入对应的jar包,另外保存上传文件的文件夹应该是在webRoot目录下,而不是在src目录下,经过编译后部署到tomcat中的程序只是webRoot中的经过编译的class文件和所有的jsp,jar,properties,xml等文件而不会包含src文件,所以应该将upload文件夹建在webRoot目录下。

  • 相关阅读:
    Java 获取代码运行时间
    CentOS7 配置阿里yum源
    MySQL优化服务器设置(MySQL优化配置文件)
    Mysql查看状态,连接数,线程数以及Mysql性能监控工具doDBA的使用以及优化
    SpringBoot专栏(四) -- SpringBoot+MyBatis集成Druid连接池
    SpringBoot专栏(三) -- SpingBoot集成MyBatis框架
    利用MyBatis生成器自动生成实体类、DAO接口和Mapping映射文件
    在Linux上搭建Jmeter测试环境
    MySQL 修改最大连接数(max_connections)失效,上限214问题
    javase基础
  • 原文地址:https://www.cnblogs.com/moonfans/p/3025186.html
Copyright © 2020-2023  润新知