Struts2实现上传单个文件功能
1. 准备工作
在Struts2中实现文件上传功能,需要添加struts2-fileupload-plugin依赖包。可以在项目的pom.xml文件中加入以下代码:
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.22</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
2. 配置struts.xml文件
在Struts2项目中,需要在struts.xml配置文件中添加action映射,定义对应的Action类。
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="fileUploadAction" class="com.example.FileUploadAction">
<result name="success">success.jsp</result>
<result name="input">index.jsp</result>
</action>
</package>
</struts>
命名空间为根目录/,Action类为FileUploadAction。
3. 编写Action类
编写一个Action类,用于处理上传文件的相关操作,以下是一个示例:
package com.example;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport {
// 接收上传文件的参数
private File file;
private String fileContentType;
private String fileFileName;
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
// 处理文件上传
public String execute() throws Exception {
// 获取当前Web应用的上传目录
String uploadPath = ServletActionContext.getServletContext().getRealPath("/upload");
// 将上传的文件复制到目标目录
FileUtils.copyFile(file, new File(uploadPath, fileFileName));
return SUCCESS;
}
}
4. 编写上传页面
在Web应用中编写一个上传的页面文件,示例如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Single File Upload</title>
</head>
<body>
<form action="fileUploadAction" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
</body>
</html>
在表单中创建一个文件输入框,在表单中添加一个enctype="multipart/form-data"属性。
5. 测试
运行Tomcat服务器,访问上传页面,随意选择一个文件上传,上传成功后会跳转到成功的页面。
示例一
上传文件,并在上传目录下展示所有上传文件的信息
- struts.xml配置文件:
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="fileUploadAction" class="com.example.FileUploadAction">
<result name="success">success.jsp</result>
<result name="input">index.jsp</result>
</action>
<action name="showFilesAction" class="com.example.ShowFilesAction">
<result name="success">list.jsp</result>
<result name="input">list.jsp</result>
</action>
</package>
</struts>
- FileUploadAction类:
package com.example;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport {
private File file;
private String fileContentType;
private String fileFileName;
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String execute() {
String uploadPath = ServletActionContext.getServletContext().getRealPath("/upload");
try {
FileUtils.copyFile(file, new File(uploadPath, fileFileName));
} catch (IOException e) {
e.printStackTrace();
}
return SUCCESS;
}
}
- ShowFilesAction类:
package com.example;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ShowFilesAction extends ActionSupport {
private List<File> fileList = new ArrayList<File>();
public List<File> getFileList() {
return fileList;
}
public void setFileList(List<File> fileList) {
this.fileList = fileList;
}
public String execute() {
String uploadPath = ServletActionContext.getServletContext().getRealPath("/upload");
File uploadDir = new File(uploadPath);
if (!uploadDir.exists()) {
uploadDir.mkdirs();
}
File[] files = uploadDir.listFiles();
for (File file : files) {
fileList.add(file);
}
return SUCCESS;
}
}
- success.jsp文件:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Success</title>
</head>
<body>
<p>Upload Success</p>
<p><a href="showFilesAction.action">Show Files</a></p>
</body>
</html>
- list.jsp文件:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>List</title>
</head>
<body>
<h1>File List</h1>
<ul>
<s:iterator value="fileList">
<li><s:property value="name"/></li>
</s:iterator>
</ul>
</body>
</html>
其中,success.jsp为上传成功后显示的页面,提供了“查看文件”按钮。该按钮会跳转到ShowFilesAction类,该类读取上传目录中的所有文件,保存到一个fileList集合中,返回到list.jsp页面,展示出所有已上传的文件名。
示例二
上传文件后返回文件名及文件大小
- FileUploadAction类:
package com.example;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport {
private File file;
private String fileContentType;
private String fileFileName;
private long fileSize;
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public long getFileSize() {
return fileSize;
}
public void setFileSize(long fileSize) {
this.fileSize = fileSize;
}
public String execute() throws Exception {
String uploadPath = ServletActionContext.getServletContext().getRealPath("/upload");
FileUtils.copyFile(file, new File(uploadPath, fileFileName));
this.setFileSize(file.length());
return SUCCESS;
}
}
- success.jsp文件:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Success</title>
</head>
<body>
<p>Upload Success</p>
<p>File Name: <s:property value="fileFileName"/></p>
<p>File Size: <s:property value="fileSize"/></p>
</body>
</html>
在此示例中,在FileUploadAction类中新加了一个fileSize属性,上传成功后返回该文件的大小。
以上就是使用Struts2实现上传单个文件功能的完整攻略。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Struts2实现上传单个文件功能 - Python技术站