struts2上传文件

本文介绍如何使用Struts2框架实现文件上传功能,包括JSP页面设计、Action处理逻辑及配置方法。演示了文件上传过程中的关键步骤,如设置上传路径、检查文件是否存在等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!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>用struts来上传文件</title> </head> <body> <s:form action="UploadAction" namespace="/chapter12" enctype="multipart/form-data"> <s:file label="上传文件" name="upload" /> <s:textfield label="新文件名" name="filename" /> <s:submit value="上传" /> </s:form> </body> </html>

以上是jsp的代码

下面看action的代码

package chapter12.action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; private File upload; private String uploadContentType; private String uploadFileName; private String filename; private String uploadPath; //上传文件保存在服务器的路径,通过参数设置 private String result; public String getResult() { return result; } public void setResult(String result) { this.result = result; } public File getUpload() { return upload; } public void setUpload(File upload) { this.upload = upload; } public String getUploadContentType() { return uploadContentType; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; } public String getUploadFileName() { return uploadFileName; } public void setUploadFileName(String uploadFileName) { this.uploadFileName = uploadFileName; } public String getFilename() { return filename; } public void setFilename(String filename) { this.filename = filename; } public String getUploadPath() { return uploadPath; } public void setUploadPath(String uploadPath) { this.uploadPath = uploadPath; } public String execute() throws Exception{ String fn = ""; //如果新文件名未输入,则使用上传文件的文件名作为服务器保存的文件名 if(filename.equals("")){ fn = uploadPath + uploadFileName; //获得上传的文件名 }else{ fn = uploadPath + filename; } System.out.println(uploadContentType); //如果服务器存在同名的文件,则输出提示信息 if(new File(fn).exists()){ result = "该文件已经存在,请为其指定一个新的文件名!"; }else{ FileOutputStream fos = new FileOutputStream(fn); InputStream is = new FileInputStream(upload); byte[] buffer = new byte[8192]; int count = 0; while((count = is.read(buffer))>0){ fos.write(buffer, 0, count); } fos.close(); is.close(); result = "文件上传成功"; } return "result"; } }

这是action的代码,其余的都不用解释,但是有几个命名要解释一下。

主要是这个uploadFileName和uploadContentType是怎样接收到值的,

在struts中:xxxFileName: 该属性 封装了文件域中的文件名,为String类型。其中xxx表示上传域的name的属性值,在本例中xxxFileName属性值就是uploadFileName

对于xxxContentType的原理也是一样的

至于action的配置很简单

<!-- struts2上传文件 --> <action name="UploadAction" class="chapter12.action.UploadAction"> <result name="result">/chapter12/result.jsp</result> <param name="uploadPath">D:/upload/</param> </action>

result.jsp的代码更是简单

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!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>上传结果</title> </head> <body> <s:property value="result"/> </body> </html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值