1、文件上传
首先要导入要用到的struts2的jar包
2、在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>/*</url-pattern> </filter-mapping>
3、在src目录下配置struts.xml
<?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"> <!-- START SNIPPET: xworkSample --> <struts> <include file="com/xinzhi/file/input.xml"></include> </struts> <!-- END SNIPPET: xworkSample -->
4、修改上传文件大小常量(这个是为了练习采写,默认大小为2M)在src目录下配置constant.xml
<?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"> <!-- START SNIPPET: xworkSample --> <struts> <constant name="struts.multipart.maxSize" value="31457280"></constant> </struts>
5、写一个上传的jsp页面input.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>文件上传</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="inputFileAction" method="post" enctype="multipart/form-data"> 上传文件:<input type="file" name="file1"/> <input type="submit" value="提交"> </form> </body> </html>
6、在FileUploadAction.java中接收文件并处理
package com.xinzhi.file; import java.io.File; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.Action; public class FileUploadAction implements Action { private File file1;// 上传的文件 private String file1FileName;// 上传的文件名,格式固定(文件+FileName) public void setFile1(File file1) { this.file1 = file1; } public void setFile1FileName(String file1FileName) { this.file1FileName = file1FileName; } public String execute() throws Exception { String realPath = ServletActionContext.getServletContext().getRealPath( "/load"); File destFile = new File(realPath, file1FileName);//目标文件 FileUtils.copyFile(file1, destFile); return "success"; } }
7、在当前包下配置action即input.xml(因为我在src下面写了一个总的配置,所以在这里写的要在总的配置中include一下)
<?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"> <!-- START SNIPPET: xworkSample --> <struts> <package name="inputpackage" extends="struts-default" > <action name="inputFileAction" class="com.xinzhi.file.FileUploadAction" method="execute"> <result name="success">/index.jsp</result> <!-- 限制的拓展名extensions-默认堆-Stack --> <interceptor-ref name="defaultStack" > <param name="fileUpload.allowedExtensions">txt,jpg,</param> </interceptor-ref> </action> </package> </struts>
至此文件上传的代码就完成了