Struts2中处理文件上传

1.文件上传的Action,FileUploadAction

这里演示的是可以一次上传多个文件的示例,数量没有限制


package org.liky.struts.action;

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

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**

 *
 * 注意按照FileUploadInterceptor属性的定义要求这里有三个属性是固定的 1.File file 2.[File
 * Name]ContentType 3.[File Name]FileName
 *
 * File Name等同于<input type="file" name="File Name">中的File Name 例如<input
 * type="file" name="file">那么属性相应的就是 1.File file 2.String fileContentType
 * 3.String fileFileName
 *
 * @author Abu
 *
 */

public class FileUploadAction extends ActionSupport {

    private static final long serialVersionUID = -7025671119418195167L;

    private String username;
    private String password;
    private int age;

    private File[] file;
    private String[] fileContentType;

    private String[] fileFileName;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public File[] getFile() {
        return file;
    }

    public void setFile(File[] file) {
        this.file = file;
    }

    public String[] getFileContentType() {
        return fileContentType;
    }

    public void setFileContentType(String[] fileContentType) {
        this.fileContentType = fileContentType;
    }

    public String[] getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String[] fileFileName) {
        this.fileFileName = fileFileName;
    }

    @Override
    public String execute() throws Exception {

        String path = ServletActionContext.getRequest().getRealPath("/upload");

        for (int i=0;i<file.length; i++) {

            // 构造输入字节流
            InputStream is = new FileInputStream(file[i]);

            // 输出文件,fileFileName由struts2自动注入
            File destFile = new File(path, this.getFileFileName()[i]);
            OutputStream os = new FileOutputStream(destFile);

            int length = 0;
            byte[] buffer = new byte[1024];

            while ((length = is.read(buffer)) > 0) {
                os.write(buffer, 0, length);
            }

            os.close();
            is.close();
        }

        return ActionSupport.SUCCESS;
    }

}

 

2.文件上传的jsp页面,fileupload.jsp

注意红色部分,这里必须制定,表单数据的提交方式为post,而且格式为multipart/form-data

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%request.setCharacterEncoding("UTF-8");%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head> 
    <title>测试文件上传</title> 
  </head>
 
  <body>
    <h2>测试文件上传</h2>
    <form action="fileUpload.action" name="UploadForm" method="post" enctype="multipart/form-data">
        姓名:<input type="text" name="username"><br>
        密码:<input type="password" name="password"><br>
        年龄:<input type="text" name="age"><br>
        文件:<input type="file" name="file"><br>
        文件:<input type="file" name="file"><br>
        文件:<input type="file" name="file"><br>
        <input type="submit" value="提交">
    </form>
  </body>
</html>

 

3.后期表单处理的jsp页面,showUser.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>  
    <title>Login result</title>
  </head>
 
  <body>
    <h3 align="center" style="color:red;">After handle the file upload, show user information</h3>
    <hr>
    Your username:${requestScope.username }<br>
    Your password:${requestScope.password }<br>
    Your age:${requestScope.age }<br>
  </body>
</html>

 

4.配置文件,struts.xml

注意你只需要配置红色部分的action就可以了,因为懒的原因所以其他的interceptor和action就没有去删除了

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <package name="struts2" extends="struts-default">
        <interceptors>
            <interceptor name="sampleInterceptor"
                class="org.liky.struts.interceptor.SampleInterceptor">
                <param name="param">liky green</param>
            </interceptor>
            <interceptor name="anotherInterceptor"
                class="org.liky.struts.interceptor.AnotherInterceptor">
            </interceptor>
            <interceptor-stack name="sampleInterceptorStack">
                <interceptor-ref name="sampleInterceptor"></interceptor-ref>
                <interceptor-ref name="defaultStack"></interceptor-ref>
                <interceptor-ref name="anotherInterceptor"></interceptor-ref>
            </interceptor-stack>
            <interceptor name="myMethodInterceptor"
                class="org.liky.struts.interceptor.MyMethodInterceptor">
            </interceptor>
            <interceptor name="authInterceptor"
                class="org.liky.struts.interceptor.AuthInterceptor">
            </interceptor>
        </interceptors>

        <default-interceptor-ref name="sampleInterceptorStack">
        </default-interceptor-ref>

        <!-- Global Results -->
        <global-results>
            <result name="login">/login.jsp</result>
        </global-results>
        <action name="login"
            class="org.liky.struts.action.LoginAction">
            <result name="success">/register.jsp</result>
            <result name="input">login.jsp</result>
        </action>

        <action name="convert"
            class="org.liky.struts.action.ConvertAction">
            <result name="success">/output.jsp</result>
            <result name="input">/converter.jsp</result>
        </action>

        <action name="convertList"
            class="org.liky.struts.action.ConvertListAction">
            <result name="success">/output.jsp</result>
            <result name="input">/converter.jsp</result>
        </action>

        <action name="register"
            class="org.liky.struts.action.RegisterAction" method="register">
            <interceptor-ref name="sampleInterceptorStack"></interceptor-ref>
            <interceptor-ref name="authInterceptor"></interceptor-ref>
            <interceptor-ref name="myMethodInterceptor">
                <param name="excludeMethods">execute</param>
                <param name="includeMethods">register</param>
            </interceptor-ref>
            <result name="success">/success.jsp</result>
            <result name="input">/register.jsp</result>
        </action>


        <action name="fileUpload"
            class="org.liky.struts.action.FileUploadAction">
            <result name="success">/showUser.jsp</result>
            <result name="input">/fileupload.jsp</result>
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <interceptor-ref name="fileUpload">
                <!-- 如果需要制定单个上传文件的大小,和文件类型,请使用这两个参数来指定
                    <param name="maximumSize">102400</param>
                   
                    <param name="allowedTypes">application/vnd.ms-word</param>
                -->

            </interceptor-ref>
        </action>

    </package>
</struts>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值