博客
关于我
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 整形列的字节与存储范围
查看>>
mysql 断电数据损坏,无法启动
查看>>
MySQL 日期时间类型的选择
查看>>
Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
查看>>
MySQL 是如何加锁的?
查看>>
MySQL 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询数据库所有表的字段信息
查看>>
【Java基础】什么是面向对象?
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>