一、 搭建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实现文件上传功能实例解析,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
# struts
# 文件上传
# Struts2实现单文件或多文件上传功能
# struts2实现多文件上传的示例代码
# java中struts2实现简单的文件上传与下载
# java中struts2实现文件上传下载功能
# JavaWeb中struts2实现文件上传下载功能实例解析
# JavaEE中struts2实现文件上传下载功能实例解析
# Struts2 文件上传进度条的实现实例代码
# 上传文件
# 上传
# 小编
# 拦截器
# 给大家
# 不超过
# 如需
# 太大了
# 所述
# 如要
# 给我留言
# 配置文件
# 右击
# 有任何
# Action
# import
# uploadFileName
# package
# yf
相关文章:
Python路径拼接规范_跨平台处理说明【指导】
制作公司内部网站有哪些,内网如何建网站?
建站之星CMS建站配置指南:模板选择与SEO优化技巧
手机网站制作与建设方案,手机网站如何建设?
建站之星与建站宝盒如何选择最佳方案?
娃派WAP自助建站:免费模板+移动优化,快速打造专业网站
建站之星价格显示格式升级,你的预算足够吗?
平台云上自主建站:模板化设计与智能工具打造高效网站
广州美橙建站如何快速搭建多端合一网站?
济南网站建设制作公司,室内设计网站一般都有哪些功能?
小视频制作网站有哪些,有什么看国内小视频的网站,求推荐?
广州营销型建站服务商推荐:技术优势与SEO优化解析
javascript中的try catch异常捕获机制用法分析
如何彻底卸载建站之星软件?
如何快速生成凡客建站的专业级图册?
保定网站制作方案定制,保定招聘的渠道有哪些?找工作的人一般都去哪里看招聘信息?
如何在阿里云ECS服务器部署织梦CMS网站?
山东云建站价格为何差异显著?
南京网站制作费用,南京远驱官方网站?
制作网站软件推荐手机版,如何制作属于自己的手机网站app应用?
移民网站制作流程,怎么看加拿大移民官网?
Bpmn 2.0的XML文件怎么画流程图
深圳企业网站制作设计,在深圳如何网上全流程注册公司?
巅云智能建站系统:可视化拖拽+多端适配+免费模板一键生成
赚钱网站制作软件,建一个网站怎样才能赚钱?是如何盈利的?
如何解决ASP生成WAP建站中文乱码问题?
如何破解联通资金短缺导致的基站建设难题?
网站制作模板下载什么软件,ppt模板免费下载网站?
整蛊网站制作软件,手机不停的收到各种网站的验证码短信,是手机病毒还是人为恶搞?有这种手机病毒吗?
香港服务器租用费用高吗?如何避免常见误区?
制作网站的软件下载免费,今日头条开宝箱老是需要下载怎么回事?
广州网站制作公司哪家好一点,广州欧莱雅百库网络科技有限公司官网?
,怎么用自己头像做动态表情包?
如何用IIS7快速搭建并优化网站站点?
如何通过西部建站助手安装IIS服务器?
如何在Windows虚拟主机上快速搭建网站?
宝塔面板创建网站无法访问?如何快速排查修复?
海南网站制作公司有哪些,海口网是哪家的?
广德云建站网站建设方案与建站流程优化指南
宝盒自助建站智能生成技巧:SEO优化与关键词设置指南
如何打造高效商业网站?建站目的决定转化率
建站之星如何助力企业快速打造五合一网站?
佛山企业网站制作公司有哪些,沟通100网上服务官网?
简单实现Android文件上传
北京营销型网站制作公司,可以用python做一个营销推广网站吗?
Dapper的Execute方法的返回值是什么意思 Dapper Execute返回值详解
网站制作大概要多少钱一个,做一个平台网站大概多少钱?
单页制作网站有哪些,朋友给我发了一个单页网站,我应该怎么修改才能把他变成自己的呢,请求高手指点迷津?
c++ stringstream用法详解_c++字符串与数字转换利器
公司网站设计制作厂家,怎么创建自己的一个网站?
*请认真填写需求信息,我们会在24小时内与您取得联系。