博客
关于我
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高级查询之多条件的过滤查询
查看>>
MySQL高级配置
查看>>
MySQL高频面试题
查看>>
MySQL高频面试题的灵魂拷问
查看>>
MySQL(1)的使用 | SQL
查看>>
MySQL(2)DDL详解
查看>>
MySQL(3)DML详解
查看>>
MySQL(4)运算符 | 关联查询详解
查看>>
MySQL(5)条件查询 | 单行函数 | 事务详解
查看>>
Mysql,group by分组查询、order by排序查询、join连接查询、union联合查询
查看>>
Mysql,sql文件导入和导出
查看>>
MYSQL:int类型升级到bigint,对PHP开发语言影响
查看>>
Mysql:mysql 5.X 报错 ERROR 1193 (HY000): Unknown system variable ‘validate_password_length‘
查看>>
MySQL:MySQL执行一条SQL查询语句的执行过程
查看>>
Mysql:SQL性能分析
查看>>
mysql:SQL按时间查询方法总结
查看>>