Struts2 文件下载
使用Struts2做一个简单的文件下载。
首先,导包,写配置文件就不说了。
进入主题。
文件下载操作类:FileDownload.java
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileDownload extends ActionSupport{
private InputStream inputStream;
private String fileName;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public InputStream getInputStream() {
fileName="144003645679.jpg";
return ServletActionContext.getServletContext().getResourceAsStream("/upload/144003645679.jpg");
}
public String execute() {
return SUCCESS;
}
}
Struts.xml文件
<package name="fileaction" namespace="/" extends="struts-default">
<action name="filedownload" class="com.dkx.action.FileDownload" >
<result name="success" type="stream">
<!-- contentType 指定下载文件的文件类型 —— application/octet-stream 表示无限制 -->
<param name="contentType">text/plain</param>
<!-- contentDisposition 使用经过转码的文件名作为下载文件名 ——
默认格式是attachment;filename="${fileName}",
将调用该Action中的getFileName方法。 -->
<param name="contentDisposition">attachment;fileName="${fileName}"</param>
<!-- inputName 流对象名 ——
比如这里写inputStream,它就会自动去找Action中的getInputStream方法。 -->
<param name="inputName">inputStream</param>
<!-- 指定下载文件的缓冲大小 -->
<param name="bufferSize">50000000</param>
</result>
</action>
</package>
Jsp页面文件:
<a href="${basePath }filedownload.action">下载</a>
至此大功告成。点击下载。。
Struts2 文件下载的更多相关文章
- Struts2文件下载浅析
Struts2极大的简化了文件上传和下载,本文将主要介绍一下Struts2文件下载的实现1.功能主要是,在下载页面点击下载后,则下载相应的文件 2.代码部分jsp页面downloadPage:< ...
- struts2文件下载 <result type="stream">
<!--struts.xml配置--> <action name="download" class="com.unmi.action.DownloadA ...
- struts2文件下载及 <param name="inputName">inputStream</param>的理解
转自:http://blog.csdn.net/wnczwl369/article/details/7483290 转自:http://hi.baidu.com/c2_sun/item/934a542 ...
- struts2文件下载相关信息
struts.xml文件配置: <span style="font-size:16px;"><?xml version="1.0" encod ...
- Struts2文件下载
1). Struts2 中使用 type="stream" 的 result 进行下载 2). 可以为 stream 的 result 设定如下参数 contentType: 结果 ...
- struts2文件下载出现Can not find a java.io.InputStream with the name的错误
今天在用struts2就行文件下载时出现如下错误: Servlet.service() for servlet default threw exception java.lang.IllegalArg ...
- Struts2 文件下载(中文处理方法以及控制下载文件名称和扩展名)
Struts2的框架提供了现成的文件下载方式,大大简化了开发下载功能的便利性.网上的例子有很多,我把一些大家普遍比较关注的点,集中一下,给出一个整体方案. 一般我们照着书本或者网上的列子写出了一个De ...
- struts2文件下载,动态设置资源地址
转自:http://blog.csdn.net/ctrl_shift_del/article/details/6277340 ServletActionContext.getServletContex ...
- struts2文件下载 出现Can not find a java.io.InputStream with the name的错误
成功代码: 前台界面jsp: <a style="text-decoration:none;" href="<%=path %>/main/frontN ...
随机推荐
- ubuntu matplotlib 安装
sudo apt-get install python-numpy //必须 sudo apt-get install python-matplotlib //必须
- 关于RESTful
http://www.ruanyifeng.com/blog/2011/09/restful.html (1)每一个URI代表一种资源: (2)客户端和服务器之间,传递这种资源的某种表现层: (3)客 ...
- Chrome extension
PageSpeed Tincr SpriteMe JSONView FireMobileSimulator for Google Chrome™
- 对LockWindowUpdate与GetDCEx的理解(以前不知道还可以锁住刷新)
MSDN如是说:The LockWindowUpdate function disables or enables drawing in the specified window. Only one ...
- 读undo问题
SQL> drop table test1 purge; Table dropped. SQL> create table test1 as select * from dba_objec ...
- 【HDOJ】1348 Wall
计算几何-凸包模板题目,Graham算法解. /* 1348 */ #include <iostream> #include <cstdio> #include <cst ...
- POJ1321 棋盘问题(dfs)
题目链接. 分析: 用 dfs 一行一行的搜索,col记录当前列是否已经放置. AC代码如下: #include <iostream> #include <cstdio> #i ...
- 199bit总结的影响最大的十个算法
1. 归并排序(MERGE SORT).快速排序(QUICK SORT)和堆积排序(HEAP SORT) 哪个排序算法效率最高?这要看情况.这也就是我把3种算法放在一起讲的原因,可能你更常用其中一种, ...
- 将使用netTcp绑定的WCF服务寄宿到IIS7上全记录 (这文章也不错)
原文地址:http://www.cnblogs.com/wengyuli/archive/2010/11/22/wcf-tcp-host-to-iis.html 摘要 在项目开发中,我们可能会适时的选 ...
- HDU5409---CRB and Graph 2015多校 双联通分量缩点
题意:一个联通的无向图, 对于每一条边, 若删除该边后存在两点不可达,则输出这两个点, 如果存在多个则输出第一个点尽可能大,第二个点尽可能小的. 不存在输出0 0 首先 若删除某一条边后存在多个联通分 ...