博客
关于我
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/

你可能感兴趣的文章
Nash:轻量级、安全且可靠的脚本语言
查看>>
NAS、SAN和DAS的区别
查看>>
NAS个人云存储服务器搭建
查看>>
NAS服务器有哪些优势
查看>>
NAT PAT故障排除实战指南:从原理到技巧的深度探索
查看>>
nat 网卡间数据包转发_你是不是从来没有了解过光纤网卡,它跟普通网卡有什么区别?...
查看>>
NAT-DDNS内网穿透技术,快解析DDNS的优势
查看>>
NAT-DDNS内网穿透技术,快解析DDNS的优势
查看>>
NAT-DDNS内网穿透技术,解决动态域名解析难题
查看>>
natapp搭建外网服务器
查看>>
NativePHP:使用PHP构建跨平台桌面应用的新框架
查看>>
nativescript(angular2)——ListView组件
查看>>
NativeWindow_01
查看>>
Native方式运行Fabric(非Docker方式)
查看>>
Nature | 电子学“超构器件”, 从零基础到精通,收藏这篇就够了!
查看>>
Nature和Science同时报道,新疆出土四千年前遗骸完成DNA测序,证实并非移民而是土著...
查看>>
Nature封面:只低一毫米,时间也会变慢!叶军团队首次在毫米尺度验证广义相对论...
查看>>
Nat、端口映射、内网穿透有什么区别?
查看>>
Nat、端口映射、内网穿透有什么区别?
查看>>
nat打洞原理和实现
查看>>