博客
关于我
Struts2文件上传
阅读量:443 次
发布时间:2019-03-06

本文共 4400 字,大约阅读时间需要 14 分钟。

=======================上传(过滤大小及类型)下载文件(弹出框以流的方式下载)=======================

1)上传jsp:

Struts2文件上传示例

用户名:
年龄:
照片:   

 

注:1) enctype="multipart/form-data" 表单中如果要上传附件那么这里要加这个属性,作用是表单是的数据以二进制形式提交

2) method="post" 有附件上传那么提交方式必然是post方式
2) 上传Action:

/** 必须继承ActionSupport类,则否fileUpload拦截器无效*/public class FileUpLoadAction extends ActionSupport {private static final long serialVersionUID = 1L;private File image2; // 变量名必须与jsp页面中的file控件的name相同private String image2FileName; // 必须--File变量的名字+ "FileName"private List
listNames;public File getImage2() {return image2;}public void setImage2(File image2) {this.image2 = image2;}public String getImage2FileName() {return image2FileName;}public void setImage2FileName(String image2FileName) {this.image2FileName = image2FileName;}public List
getListNames() {return listNames;}public void setListNames(List
listNames) {this.listNames = listNames;}public String execute() throws Exception {// 放图片的路径:E:\apache-tomcat-6.0.18\webapps\StrutsFileUpDown\imagesString realpath = ServletActionContext.getServletContext().getRealPath("/images");//不存在则创建,代码没写if (image2 != null) {// 放图片的路径+图片的名称File savefile = new File(realpath + "/" + image2FileName);// FileUtils.copyFile(file1,file2);file1,file2都是文件类型File;把file1拷贝到file2FileUtils.copyFile(image2, savefile);}// 接收用户名和年龄String userName = ServletActionContext.getRequest().getParameter("uname");String age = ServletActionContext.getRequest().getParameter("age");/*** 读取文件名列表*///读取图片的名称返回一个list列表listNames = findFileNames(realpath );return "success";}/*** 读取文件名的列表* * @param path* 放图片的路径* @return 把路径中的图片名取出来存在List里*/private List
findFileNames(String path) {List
listNames = new ArrayList
();File file = new File(path);File[] files = file.listFiles();for (File f : files) {if (f.isFile()) {// 得到图片的名称 123.jpgString fileName = f.getName();listNames.add(fileName);}}return listNames;}}

 

3)下载Action

public class DownloadAction {private String fileName; public String getFileName() {return fileName;}public void setFileName(String fileName) {this.fileName = fileName;}public InputStream getInputStream() {System.out.println("getFileName()="+getFileName());InputStream ins = ServletActionContext.getServletContext().getResourceAsStream("/images/" + fileName);return ins;//查找具有给定名称的资源。返回 inputstream//相当于取得File文件后,再new InputStream(file)一样的结果//以'/'开头时默认是从此类所在的包下取资源,以'/'开头则是从ClassPath(Src根目录)根下获取。//E:\apache-tomcat-6.0.18\webapps\StrutsFileUpDown\images 其实E:\apache-tomcat-6.0.18\webapps \StrutsFileUpDown\就是根}public String execute(){return "success";//下载不用写返回页面}}

 

注:execute()必须走,走 execute(),在走getInputStream --------->把附件以流的方式写入浏览器中,以弹出框的形式下 载

4)struts.xml

index.jsp
error.jsp
image/bmp,image/png,image/gif
102400
application/octet-stream
inputStream
attachment;fileName=${fileName}
4096

5)错误页面 error.jsp

<%@ taglib uri="/struts-tags" prefix="s"%>

返回图片上传页

 

出错信息在:

传输文件时的信息解释:(struts核心包下/org.apache.struts2/struts-messages.properties文件里)

struts.messages.error.content.type.not.allowed=不支持上传该类型的文件

struts.messages.error.file.too.large=上传图片失败:图片太大
struts.messages.error.uploading=上传文件时发生错误

 

===========================页面用struts标签==========================================

1)如果页面用struts标签。
2)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>
那么访问JSP时就报错,找不到struts标签,因为配置时用的是*.action,只有.action形式才走struts框架,那么这时我们就可以把配置改下加
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>

这样我们就可以在访问jsp时,让它走struts框架。

web.xml完整代码:

struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
*.action
struts2
*.jsp
index.jsp

 

3)还有另外一种方法是把*.action改成/*

转载地址:http://rgxyz.baihongyu.com/

你可能感兴趣的文章
mysql 的存储引擎介绍
查看>>
MySQL 的存储引擎有哪些?为什么常用InnoDB?
查看>>
Mysql 知识回顾总结-索引
查看>>
Mysql 笔记
查看>>
MySQL 精选 60 道面试题(含答案)
查看>>
mysql 索引
查看>>
MySQL 索引失效的 15 种场景!
查看>>
MySQL 索引深入解析及优化策略
查看>>
MySQL 索引的面试题总结
查看>>
mysql 索引类型以及创建
查看>>
MySQL 索引连环问题,你能答对几个?
查看>>
Mysql 索引问题集锦
查看>>
Mysql 纵表转换为横表
查看>>
mysql 编译安装 window篇
查看>>
mysql 网络目录_联机目录数据库
查看>>
MySQL 聚簇索引&&二级索引&&辅助索引
查看>>
Mysql 脏页 脏读 脏数据
查看>>
mysql 自增id和UUID做主键性能分析,及最优方案
查看>>
Mysql 自定义函数
查看>>
mysql 行转列 列转行
查看>>