Struts2实现文件上传功能实例解析
作者:Giving_bestself 时间:2023-11-04 04:56:13
一、 搭建struts2环境
在myeclipse下,右击项目->MyEclipse->Project Facets->install Apache Struts2。
如要自己搭建,需下载struts2包,写struts.xml配置文件。
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>*.action</url-pattern>
</filter-mapping>
二、 文件上传
1.前台页面:
上传页面:
<body>
<form action="upload.action" method="post" enctype="multipart/form-data">
<input type="file" name="upload"/>
<input type="submit" value="提交"/>
<br>
${result}
</form>
</body>
input name属性与后台命名一致。
上传失败页面:
<body>
<h2>上传失败</h2>
<s:fielderror></s:fielderror>
</body>
需:
<%@ taglib uri="/struts-tags" prefix="s"%>
2.后台Action
主要属性upload,uploadContentType,uploadFileName。
package com.yf.action;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport{
private File upload;
private String uploadContentType;
private String uploadFileName;
private String result;
public File getUoload() {
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 getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
@Override
public String execute() throws Exception {
String path = ServletActionContext.getServletContext().getRealPath("/images");
File file = new File(path);
if(!file.exists()){
file.mkdir();
}
System.out.println(upload);
FileUtils.copyFile(upload, new File(file,uploadFileName));
result = "上传成功";
return SUCCESS;
}
}
3.struts.xml文件配置
配置action及配置 * 限制上传文件的类型及大小。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<constant name="struts.devMode" value="true"/>
<constant name="struts.multipart.saveDir" value="/tmp"/>
<constant name="struts.custom.i18n.resources" value="app"></constant>
<package name="default" namespace="/" extends="struts-default">
<action name="upload" class="com.yf.action.UploadAction">
<result>/index.jsp</result>
<result name="input">/error.jsp</result>
<!-- 配置 * 限制上传文件的类型及大小 -->
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/bmp,image/x-png,image/gif,image/jpeg</param>
<param name="maximumSize">2M</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts>
4.新建properties文件
文件上传失败信息显示到前台,处理显示出错信息。
文件内容如下:
struts.messages.error.file.too/large=\u4E0A\u4F20\u6587\u4EF6\u592A\u5927\u4E86\uFF01
struts.messages.error.content.type.not.allowed=\u4E0A\u4F20\u6587\u4EF6\u7C7B\u578B\u4E0D\u7B26\uFF01
即添加:
Name:struts.messages.error.file.too/large
value : 上传文件太大了!
Name : struts.messages.error.content.type.not.allowed
value: 上传文件类型不符!
运行结果如下:
选择jpg图片,大小不超过2M,运行后
选择非图片文件:
如需批量上传文件,将后台upload,uploadContentType,uploadFileName均改为List,循环读取上传文件保存到硬盘,前台加入input ,name属性一致。
以上所述是小编给大家介绍的Struts2实现文件上传功能实例解析,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
来源:http://blog.csdn.net/giving_bestself/article/details/54233172
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
.net的序列化与反序列化实例
Android基于TextView属性android:ellipsize实现跑马灯效果的方法
C++中String类型的逆序方式
IDEA配置maven环境的详细教程(Unable to import maven project报错问题的解决)
![](https://img.aspxhome.com/file/2023/0/71000_0s.jpg)
C#中使用HttpDownLoadHelper下载文件实例
Android11文件管理权限申请详细介绍
![](https://img.aspxhome.com/file/2023/6/137656_0s.png)
Android ViewPager实现无限循环轮播广告位Banner效果
![](https://img.aspxhome.com/file/2023/3/93253_0s.jpg)
Android开源组件小结
![](https://img.aspxhome.com/file/2023/1/138451_0s.jpg)
合并有序数组的实现(java与C语言)
Java操作hdfs文件系统过程
![](https://img.aspxhome.com/file/2023/7/70967_0s.png)
C# 定时器保活机制引起的内存泄露问题解决
Java权重随机的实现方法
Mybatisplus主键生成策略算法解析
SpringBoot深入浅出分析初始化器
浅谈java并发之计数器CountDownLatch
![](https://img.aspxhome.com/file/2023/2/66702_0s.jpg)
Java 栈与队列实战真题训练
![](https://img.aspxhome.com/file/2023/9/73089_0s.png)
SpringBoot+Hutool+thymeleaf完成导出Excel的实现方法
![](https://img.aspxhome.com/file/2023/2/71682_0s.png)
Android仿英语流利说取词放大控件的实现方法(附demo源码下载)
![](https://img.aspxhome.com/file/2023/5/88685_0s.jpg)