首页 > 代码库 > struts2学习笔记之十:文件上传

struts2学习笔记之十:文件上传

Struts2的上传
1、Struts2默认采用了apache commons-fileupload
2、Struts2支持三种类型的上传组件
3、需要引入commons-fileupload相关依赖包
* commons-io-1.3.2.jar
* commons-fileupload-1.2.1.jar
4、表单中需要采用POST提交方式,编码类型需要使用:multipart/form-data
5、Struts2的Action
取得文件名称->>规则:输入域的名称+固定字符串FileName
取得文件数据->>规则:File 输入域的名称
取得内容类型->>规则:输入域的名称+固定字符串ContentType
6、得到输入流,采用输出流写文件
 
Action类
package com.djoker.struts2;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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.Action;

public class uploadAction {

    private String myFileFileName;
    
    private File myFile;
    
    private String descContextType;

    public String getMyFileFileName() {
        return myFileFileName;
    }

    public void setMyFileFileName(String myFileFileName) {
        this.myFileFileName = myFileFileName;
    }

    public File getMyFile() {
        return myFile;
    }

    public void setMyFile(File myFile) {
        this.myFile = myFile;
    }

    public String getDescContextType() {
        return descContextType;
    }

    public void setDescContextType(String descContextType) {
        this.descContextType = descContextType;
    }
    
    public String execute() throws Exception {
        System.out.println(myFileFileName);
        InputStream is = null;
        OutputStream os = null;
        try{
        is = new BufferedInputStream(new FileInputStream(myFile));
        os = new BufferedOutputStream(new FileOutputStream(ServletActionContext.getServletContext().getRealPath("upload") + "/" + myFileFileName));
        byte[] ByteBuffer = new byte[1024];
        int len = 0;
        while((len = is.read(ByteBuffer)) > 0){
            os.write(ByteBuffer, 0, len);
        }
        } finally {
            if(is != null){
                is.close();
            }
            if(os != null){
                os.close();
            }
        }
        return Action.SUCCESS;
    }
}

 

upload.jsp文件
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<form action="uploadAction.action" method="post" enctype="multipart/form-data">
    选择文件:<input type="file" name="myFile"><br>
    文件描述:<input type="text" name="desc"><br>
    <input type="submit" value="上传">
</form>
</body>
</html>
 
 
struts.xml配置中,配置最大文件上传限制
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">

<struts>
    
    <!-- 当struts.xml配置文件发生修改,会立刻加载,在生产环境下最好不要配置  -->
    <constant name="struts.configuration.xml.reload" value="true"></constant>
    <!-- 提供更加友好的提示信息 -->
    <constant name="struts.devMode" value="true"></constant>
    <!-- 对字符集的设置 -->
    <constant name="struts.i18n.encoding" value="GB18030"/>
    <!-- 配置文件上传最大限制 -->
    <constant name="struts.multipart.maxSize" value="9999999999"></constant>
    <package name="struts2" extends="struts-default" >
        <global-results>
            <result>/success.jsp</result>
            <result name="error">/error.jsp</result>
        </global-results>
        <action name="login" class="com.djoker.struts2.LoginAction">
            <result>/success.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
        <action name="uploadAction" class="com.djoker.struts2.uploadAction">
            <result>/success.jsp</result>
        </action>
    </package>
    
    <include file="struts-user.xml"></include>
</struts>

 

struts2学习笔记之十:文件上传