一、配置上传解析器
首先要配置项目的框架,也就是倒导入"struts2-core-2.2.1.jar"库文件,找到org.apache.struts2包下的default.porperties资源文件。如下图;资源文件中给出了不同的strus2的默认配置,我们可看到struts2默认是jakarta作为其文件上传的解析器。
jakarta是Commo-FileUpload的框架。如果要使用Commo-FileUpload框架来上传文件,只需将"commons-fileupload-1.2.1.jar"和"commons-io-1.3.2.jar"两个jar复制到项目中的WEB-INF/lib目录下就可。
如果想要使用COS框架来上传文件,只需将“cos.jar”复制到项目中就可以,然后在修改struts.multipart.parser常量值。
修改常量值有两种方法,一是在"struts.xml"中修改,代码如下:
<constant name="struts.multipart.paeser" value="cos"></constant>
二是在struts.properties中修改,代码如下:
sruts.multipart.parser=cos
二、实现文件上传的Action
创建表单:upload.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>Struts2文件上传</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>
- <center>
- <h1>Struts 2完成上传</h1>
- <form action="upload.action" method="post" enctype="multipart/form-data">
- <table>
- <tr>
- <td>用户名:</td>
- <td><input type="text" name="username" ></td>
- </tr>
- <tr>
- <td>上传文件:</td>
- <td><input type="file" name="myFile"></td>
- </tr>
- <tr>
- <td><input type="submit" value="上传"></td>
- <td><input type="reset"></td>
- </tr>
- </table>
- </form>
- </center>
- </body>
- </html>
<% 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>Struts2文件上传</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> <center> <h1>Struts 2完成上传</h1> <form action="upload.action" method="post" enctype="multipart/form-data"> <table> <tr> <td>用户名:</td> <td><input type="text" name="username" ></td> </tr> <tr> <td>上传文件:</td> <td><input type="file" name="myFile"></td> </tr> <tr> <td><input type="submit" value="上传"></td> <td><input type="reset"></td> </tr> </table> </form> </center> </body> </html>
完成上传Action
package net.hncu.struts2.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;
- public class UploadAction extends ActionSupport {
- // username属性用来封装用户名
- private String username;
- // myFile属性用来封装上传的文件
- private File myFile;
- // myFileContentType属性用来封装上传文件的类型
- private String myFileContentType;
- // myFileFileName属性用来封装上传文件的文件名
- private String myFileFileName;
- //获得username值
- public String getUsername() {
- return username;
- }
- //设置username值
- public void setUsername(String username) {
- this.username = username;
- }
- //获得myFile值
- public File getMyFile() {
- return myFile;
- }
- //设置myFile值
- public void setMyFile(File myFile) {
- this.myFile = myFile;
- }
- //获得myFileContentType值
- public String getMyFileContentType() {
- return myFileContentType;
- }
- //设置myFileContentType值
- public void setMyFileContentType(String myFileContentType) {
- this.myFileContentType = myFileContentType;
- }
- //获得myFileFileName值
- public String getMyFileFileName() {
- return myFileFileName;
- }
- //设置myFileFileName值
- public void setMyFileFileName(String myFileFileName) {
- this.myFileFileName = myFileFileName;
- }
- public String execute() throws Exception {
- //基于myFile创建一个文件输入流
- InputStream is = new FileInputStream(myFile);
- // 设置上传文件目录
- String uploadPath = ServletActionContext.getServletContext()
- .getRealPath("/upload");
- // 设置目标文件
- File toFile = new File(uploadPath, this.getMyFileFileName());
- // 创建一个输出流
- OutputStream os = new FileOutputStream(toFile);
- //设置缓存
- byte[] buffer = new byte[1024];
- int length = 0;
- //读取myFile文件输出到toFile文件中
- while ((length = is.read(buffer)) > 0) {
- os.write(buffer, 0, length);
- }
- System.out.println("上传用户"+username);
- System.out.println("上传文件名"+myFileFileName);
- System.out.println("上传文件类型"+myFileContentType);
- //关闭输入流
- is.close();
- //关闭输出流
- os.close();
- return SUCCESS;
- }
- }
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; public class UploadAction extends ActionSupport { // username属性用来封装用户名 private String username; // myFile属性用来封装上传的文件 private File myFile; // myFileContentType属性用来封装上传文件的类型 private String myFileContentType; // myFileFileName属性用来封装上传文件的文件名 private String myFileFileName; //获得username值 public String getUsername() { return username; } //设置username值 public void setUsername(String username) { this.username = username; } //获得myFile值 public File getMyFile() { return myFile; } //设置myFile值 public void setMyFile(File myFile) { this.myFile = myFile; } //获得myFileContentType值 public String getMyFileContentType() { return myFileContentType; } //设置myFileContentType值 public void setMyFileContentType(String myFileContentType) { this.myFileContentType = myFileContentType; } //获得myFileFileName值 public String getMyFileFileName() { return myFileFileName; } //设置myFileFileName值 public void setMyFileFileName(String myFileFileName) { this.myFileFileName = myFileFileName; } public String execute() throws Exception { //基于myFile创建一个文件输入流 InputStream is = new FileInputStream(myFile); // 设置上传文件目录 String uploadPath = ServletActionContext.getServletContext() .getRealPath("/upload"); // 设置目标文件 File toFile = new File(uploadPath, this.getMyFileFileName()); // 创建一个输出流 OutputStream os = new FileOutputStream(toFile); //设置缓存 byte[] buffer = new byte[1024]; int length = 0; //读取myFile文件输出到toFile文件中 while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length); } System.out.println("上传用户"+username); System.out.println("上传文件名"+myFileFileName); System.out.println("上传文件类型"+myFileContentType); //关闭输入流 is.close(); //关闭输出流 os.close(); return SUCCESS; } }
配置上传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">
- <action name="upload" class="net.hncu.struts2.action.UploadAction">
- <result name="success">/result.jsp</result>
- <result name="input">/upload.jsp</result>
- </action>
- </package>
- <!-- Add packages here -->
- </struts>
<!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"> <action name="upload" class="net.hncu.struts2.action.UploadAction"> <result name="success">/result.jsp</result> <result name="input">/upload.jsp</result> </action> </package> <!-- Add packages here --> </struts>
测试页面: