struts2文件上传下载
来源:互联网 发布:淘宝账号密码格式 编辑:程序博客网 时间:2024/06/08 03:15
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实现文件上传下载
- Java分支结构switch语句
- spring用的是单实例 ,但是支持多线程
- struts2自定义拦截器
- Java循环结构,while、for和break…
- 大数据解密:《人民的名义》是怎么火起来的?
- struts2文件上传下载
- myeclipse中通过oci连接oracle
- 第二次测试循环+一点数组
- 在MyEclipse2015中上传项目到github的步骤(转载)
- 微信小程序从入门到放弃(二)
- 求逆序对
- 什么是Struts2
- JAVA 自定义类 和 字段 的注解示例
- 将博客搬至CSDN