Struts2(13)_Struts2 的文件上传

本系列博客汇总在这里:Struts2 汇总


源码工程文件为:struts2_15

一、单文件上传

上传文件对表单的要求:

  • 表单要使用 post 方式提交;
  • 表单的 enctype 是 multipart/form-data;
  • 表单中要有 file 类型的 input 文本域。

Struts上传也是基于拦截器,底层还是使用 commons-fileupload 组件,Struts上传的步骤如下:

  • 建立表单
    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@ taglib uri="/struts-tags" prefix="s"%>
    <!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=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <s:fielderror></s:fielderror>
    <s:actionerror/>
    <form action="${pageContext.request.contextPath }/upload/weiyuxuan" method="post" enctype="multipart/form-data">
    	姓名:<input type="text" name="username"><br>
    	文件:<input type="file" name="upload"><br>
    	<input type="submit">
    </form>
    </body>
    </html>
    
  • 创建 Action
    注意:
    接收的文件属性:和表单中文本域 file 类型 input 的 name 一致!
    接收的文件名字:file 的 name+FileName 固定写法!
    接收的文件 MIME 类型:file 的 name+ContentType。
    package com.wyx.action;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    import javax.servlet.ServletContext;
    
    import org.apache.commons.io.FileUtils;
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    /**
     * action动作类充当模型对象
     */
    public class UploadAction extends ActionSupport
    {
    
    	private String username;
    
    	/**
    	 * 要接收的文件,命名需要和表单中的file类型的input的name一致
    	 */
    	private File upload;
    
    	/**
    	 * 文件名的接收 File属性名FileName:固定写法
    	 */
    	private String uploadFileName;
    
    	/**
    	 * 获得上传文件的MIME类型,File属性名字ContentType:固定写法
    	 */
    	private String uploadContentType;
    
    	public String getUsername()
    	{
    		return username;
    	}
    
    	public void setUsername(String username)
    	{
    		this.username = username;
    	}
    
    	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 upload() throws Exception
    	{ 
    		//获得servletContext 
    		ServletContext sc  = ServletActionContext.getServletContext(); 
    		//获得服务的绝对路径 
    		String realPath = sc.getRealPath("/"); 
    		realPath = realPath + "upload\\"+uploadFileName;
    		//定义输入输出流 
    		InputStream in = new FileInputStream(upload); 
    		OutputStream out = new FileOutputStream(realPath); 
    		int len = -1; 
    		byte[] bs = new byte[1024];
    		while((len = in.read(bs)) != -1)
    		{ 
    			out.write(bs, 0, len); 
    		} 
    		out.close();
    		in.close(); 
    		return super.SUCCESS; 
    	}*/
    	
    	public String upload() throws Exception
    	{
    		// 获得servletContext
    		ServletContext sc = ServletActionContext.getServletContext();
    		// 获得服务的绝对路径
    		String realPath = sc.getRealPath("/");
    		realPath = realPath + "upload\\" + uploadFileName;
    		// 定义输入输出流
    		FileUtils.copyFile(upload, new File(realPath));
    		return super.SUCCESS;
    	}
    
    }
    
    配置
    <?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>
    
    	<package name="upload" extends="struts-default" namespace="/upload">
    		<!-- 拦截器配置 -->
    		<action name="weiyuxuan" class="com.wyx.action.UploadAction" method="upload">			
    			<result name="success">/success.jsp</result>
    			<result name="input">/form.jsp</result>
    		</action>		
    	</package>
    
    </struts>
    
    在这里插入图片描述

二、文件上传类型的验证和大小

验证文件的类型:

<?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>
	
	<package name="upload" extends="struts-default" namespace="/upload">

		<!-- 拦截器配置 -->
		<action name="weiyuxuan" class="com.wyx.action.UploadAction" method="upload">
			<!-- 主动引用默认拦截器栈 -->
			<interceptor-ref name="defaultStack">
				<!--设置上传拦截器fileUpload.allowedExtensions, 不要使用allowedExtensionsSet-->
				<param name="fileUpload.allowedExtensions">.png,.txt</param>
			</interceptor-ref>
			<result name="success">/success.jsp</result>
			<result name="input">/form.jsp</result>
		</action>
	</package>

</struts>

在这里插入图片描述

验证文件的大小:

  • Struts2 默认的上传文件的配置在 default.properties 的文件中,由 struts.multipart.maxSize=2097152 控制。
  • 文件超过最大值的提示信息是在 struts_message.propertise 中,我们只要通过国际化文件覆盖它即可。
    在这里插入图片描述
    <!-- 设置文件上传的大小,以字节为单位 -->
    <constant name="struts.multipart.maxSize" value="1048576"></constant>
    
    在这里插入图片描述
    我们发现展示并不是中文,我们使用国际化来处理,创建国际化文件:
    在这里插入图片描述
    在国际化文件中 {0} 是去文件最大值,{1} 取我们上次的文件的大小。然后再 struts.xml 中加载资源文件:
    <!-- 国际化 -->
    <constant name="struts.custom.i18n.resources" value="com/wyx/action/resource/msg"></constant>
    
    在这里插入图片描述

三、多文件的上传

多文件上传 Action 中把 file 属性以及和 file 属性相关的上传文件都变成数组,在 execute 中循环上传即可。

package com.wyx.action;

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

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
 * action动作类充当模型对象
 */
public class UploadsAction extends ActionSupport
{

	private String username;

	/**
	 * 要接收的文件,命名需要和表单中的file类型的input的name一致
	 */
	private File[] upload;

	/**
	 * 文件名的接收 File属性名FileName:固定写法
	 */
	private String[] uploadFileName;

	/**
	 * 获得上传文件的MIME类型,File属性名字ContentType:固定写法
	 */
	private String[] uploadContentType;

	public String getUsername()
	{
		return username;
	}

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

	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 upload() throws Exception
	{
		// 获得servletContext
		ServletContext sc = ServletActionContext.getServletContext();
		// 获得服务的绝对路径

		for (int i = 0; i < upload.length; i++)
		{
			String realPath = sc.getRealPath("/");
			realPath = realPath + "upload\\" + uploadFileName[i];
			// 定义输入输出流
			FileUtils.copyFile(upload[i], new File(realPath));
		}
		return super.SUCCESS;
	}

}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:fielderror></s:fielderror>
<s:actionerror/>
<form action="${pageContext.request.contextPath }/upload/weiyuxuans" method="post" enctype="multipart/form-data">
	姓名:<input type="text" name="username"><br>
	文件1:<input type="file" name="upload"><br>
	文件2:<input type="file" name="upload"><br>
	文件3:<input type="file" name="upload"><br>
	<input type="submit">
</form>
</body>
</html>
<?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>
	<!-- 国际化 -->
	<constant name="struts.custom.i18n.resources" value="com/wyx/action/resource/msg"></constant>
	<!-- 设置文件上传的大小,以字节为单位 -->
	<constant name="struts.multipart.maxSize" value="1048576"></constant>

	<package name="upload" extends="struts-default" namespace="/upload">
		<!-- 多文件上传 -->
		<action name="weiyuxuans" class="com.wyx.action.UploadsAction" method="upload">
			<!-- 主动引用默认拦截器栈 -->
			<interceptor-ref name="defaultStack">
				<!-- 设置上传拦截器fileUpload.allowedExtensions, 不要使用allowedExtensionsSet -->
				<param name="fileUpload.allowedExtensions">.png,.txt</param>
			</interceptor-ref>
			<result name="success">/success.jsp</result>
			<result name="input">/form1.jsp</result>
		</action>
	</package>

</struts>

在这里插入图片描述
在这里插入图片描述

如有错误,欢迎指正!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值