(Struts2学习篇) Struts2文件处理之文件上传

符号 阅读:590 2021-03-31 23:05:01 评论:0

在webroot文件夹下创建fileupload文件夹:

单文件上传

上传页面:

<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%> 
<%@ taglib uri="/struts-tags"   prefix="s"%> 
<html> 
  <head> 
    <title>My JSP 'index.jsp' starting page</title> 
    </head> 
  <body> 
    <form action="${pageContext.request.contextPath}/upload/uploadAction_saveFile.action"   
          name="form1"  method="post"  enctype="multipart/form-data" > 
              
             上传文件名称:<input type="file" name="uploadImage"> 
           <input type="submit" value="上传"> 
    </form> 
  </body> 
</html>


 

创建对应的uploadAction类

import java.io.File; 
import java.io.IOException; 
 
import javax.servlet.ServletContext; 
 
import org.apache.commons.io.FileUtils; 
import org.apache.struts2.ServletActionContext; 
 
import com.opensymphony.xwork2.ActionSupport; 
 
@SuppressWarnings("serial") 
public class UploadAction extends ActionSupport { 
	 
	/* 
	 * 上传文件的存储的临时文件: 
	 * apache-tomcat-6.0.35\\work\\Catalina\\localhost\\itcast1105_struts\\upload__5fee1dc7_13ad3d1835b__8000_00000000.tmp 
	 */ 
	private File uploadImage; 
	 
	//上传文件的类型:text/plain 
	private String uploadImageContentType; 
	 
	//上传文件的真是名称 
	private String uploadImageFileName; 
	 
	public File getUploadImage() { 
		return uploadImage; 
	} 
 
	public void setUploadImage(File uploadImage) { 
		this.uploadImage = uploadImage; 
	} 
 
	public String getUploadImageContentType() { 
		return uploadImageContentType; 
	} 
 
	public void setUploadImageContentType(String uploadImageContentType) { 
		this.uploadImageContentType = uploadImageContentType; 
	} 
 
	public String getUploadImageFileName() { 
		return uploadImageFileName; 
	} 
 
	public void setUploadImageFileName(String uploadImageFileName) { 
		this.uploadImageFileName = uploadImageFileName; 
	} 
 
	public String saveFile(){ 
		ServletContext sc = ServletActionContext.getServletContext(); 
		String path = sc.getRealPath("/fileupload"); 
		//创建文件用于接收上传上来的林十年文件 
		File file = new File(path, uploadImageFileName);	 
		try {		 
			FileUtils.copyFile(uploadImage, file);		 
		} catch (IOException e) { 
			e.printStackTrace(); 
		} 
		//将临时文件清空 
		uploadImage.delete(); 
		return "success"; 
	} 
	 
}


在struts.xml文件中配置文件上传的总大小
    <!-- 配置文件上传的总大小 -->
    <constant name="struts.multipart.maxSize" value="2097152000"></constant>

配置相应的拦截器参数

<action name="uploadAction_saveFile" class="UploadAction" method="saveFile"> 
			<result name="success">/upload/success.jsp</result> 
			<result name="input">/upload/error.jsp</result> 
			 
			<!-- 配置拦截器的参数,这里是文件上传拦截器 --> 
			<interceptor-ref name="defaultStack"> 
              	<!--  
              		配置文件上传拦截器的参数 
              			* 与定义参数的顺序无关 
              			* 允许的类型(allowedTypes)和允许的扩展名(allowedExtensions)必须保持一致 
              	 --> 
              	<!--  
              		* 配置上传文件的大小 
              			* struts.xml文件中配置的是上传文件的总大小 
              			* 这里配置的是上传文件的单个大小 
              	 --> 
              	<param name="fileUpload.maximumSize">20971520</param> 
              	<!-- 配置上传文件允许的类型,如果配置多个值的话,用","隔开 --> 
              	<param name="fileUpload.allowedTypes">text/plain,application/msword</param> 
              	<!-- 配置上传文件的扩展名,如果配置多个值的话,用","隔开 --> 
              	<param name="fileUpload.allowedExtensions">.txt</param> 
            </interceptor-ref> 
			 
</action>


 

多文件上传:


页面:

<%@ page language="java" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%> 
<%@ taglib uri="/struts-tags"   prefix="s"%> 
<html> 
  <head> 
    <title>My JSP 'index.jsp' starting page</title> 
    </head> 
  <body> 
    <form action="${pageContext.request.contextPath}/upload/uploadsAction_saveFiles.action"   
          name="form1"  method="post"  enctype="multipart/form-data" > 
             上传文件名称:<input type="file" name="uploadImages"><br> 
             上传文件名称:<input type="file" name="uploadImages"><br> 
             上传文件名称:<input type="file" name="uploadImages"><br> 
                  
           <input type="submit" value="上传"> 
    </form> 
  </body> 
</html>


 

action中的处理方法

        private File [] uploadImages; 
	 
	private String [] uploadImagesContentType; 
	 
	private String [] uploadImagesFileName; 
	 
	public String saveFiles(){ 
		System.out.println("UploadsAction ******** saveFiles()"); 
		 
		ServletContext sc = ServletActionContext.getServletContext(); 
		 
		String path = sc.getRealPath("/fileupload"); 
		 
		for(int i=0;i<uploadImages.length;i++){ 
			File file = new File(path, uploadImagesFileName[i]); 
			 
			try { 
				FileUtils.copyFile(uploadImages[i], file); 
			} catch (IOException e) { 
				e.printStackTrace(); 
			} 
			 
			uploadImages[i].delete(); 
			 
		} 
		 
		return "success"; 
	}


 

 

标签:Struts2
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号