struts2文件上传下载
来源:互联网 发布:遥感集市 免费数据 编辑:程序博客网 时间:2024/04/27 14:06
文件上传
1.struts2中文件上传介绍
struts2文件上传需要使用apache提供的文件上传组件(commons-fileupload.jar和commons-io.jar)。
struts2文件上传的核心是通过fileupload拦截器实现的。
2.如何实现文件上传
1>.添加commons-fileupload和commons-io包
2>.在jsp页面做如下配置
将form的method属性值设置为post
给form标记添加属性enctype="multipart/form-data"(让提交的表单数据,以流的形式提交)
3>.在action类中添加如下属性,并添加get|set方法
private File xxx;
private String xxxFileName;
private String xxxContentType;
注意:xxx指代<input type="file">文件域的name值
struts2的文件上传功能,是将客户端上传的文件保存到一个临时目录,我们要做的事情,就是将临时目录中的文件保存到指定目录。
文件下载
在struts2中如何实现文件下载
1.添加一个文件下载的action类
public class DownloadAction{
private InputStream logoStream;
public InputStream getLogoStream(){
try{
this.fileName = brand.getLogoUrl();
//获取被下载的文件的绝对路径
String filePath = ServletActionContext.getRequest().getServletContext().getRealPath(brand.getLogoUrl());
System.out.println(filePath);
//读取被下载的文件
logoStream = new FileInputStream(filePath);
return logoStream;
}catch(Exception ex){
ex.printStackTrace();
return null;
}
}
}
2.在该类中添加一个action方法,返回success
public class DownloadAction{
public String download(){
return "success";
}
}
3.在struts.xml中配置该action,并且result的结果类型为stream
<action name="download" class="...DownloadAction" method="download">
<result name="success" type="stream">
</result>
</action>
4.在result中设置参数inputName,该参数的值必须为action类中的一个数据类型为InputStream的属性。
<action name="download" class="...DownloadAction" method="download">
<result name="success" type="stream">
<param name="inputName">logoStream</param>
</result>
</action>
5.控制下载文件的文件名称:在result中添加参数:contentDisposition
<action name="download" class="...DownloadAction" method="download">
<result name="success" type="stream">
<param name="inputName">logoStream</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>
</result>
</action>
注意:${fileName} 中的fileName为action类中的属性
6.控制文件的类型:在result中添加参数:contentType
contentType:
word : application/msword
excel : application/vnd.ms-excel
ppt : application/vnd.ms-powerpoint
html : text/html
文本文件 : text/plain
可执行文件 : application/octet-stream
<action name="download" class="...DownloadAction" method="download">
<result name="success" type="stream">
<param name="inputName">logoStream</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="contentType">application/octet-stream</param>
</result>
</action>
- Struts2文件上传下载
- Struts2 多文件上传下载
- struts2文件上传下载
- struts2文件上传下载详解
- struts2 文件上传下载
- Struts2文件上传下载(一)
- Struts2文件上传下载(二)
- Struts2 上传下载文件
- struts2上传下载文件
- Struts2文件上传下载
- struts2文件上传下载
- struts2文件上传下载
- struts2的文件上传下载
- struts2实现文件上传下载
- 文件上传下载struts2
- Struts2文件上传下载
- struts2文件上传下载
- struts2实现文件上传下载
- 动漫5
- 数组-数组的概述
- 电商app开发架构设计优化购物车环节
- qml播放带进度条的视频
- 为React Native开发写的JS和React入门知识
- struts2文件上传下载
- Poj 3279 Fliptile
- OpenCV Python教程(2、图像元素的访问、通道分离与合并)
- PAT 1041
- 以太网和局域网的关系
- Request对象的
- 用java流 压缩文件与解压缩文件代码
- 排序算法之插入排序
- Android studio Module的使用 (一)