Ext+struts2如何实现文件下载

elia117 2010-07-16 10:47:13
我前台用的是Ext的Grid来展示文件列表, 服务端是用Struts2,现在想让用户选中一个文档后点下载按钮就能实现文档下载功能。数据库里放的是文档的名称和路径,文档的实际路径在c盘某个目录中,请问如何实现下载。

我再struts配置文件中
<action name="DownLoadDocument" class="DocumentAction" method="downLoadDocument">
<result type="stream">
<param name="inputName">inputStream</param>
<param name="contentType">application/x-msdownload;charset=utf-8</param>
<param name="contentDisposition">attachment;filename="hello"</param>
<param name="bufferSize">15097152</param>

</result>
</action>
可是返回时用firefox只能看到返回的文件流,并不能出现文件下载提示框。

请教各位高手是什么原因?
...全文
723 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
yxhshuaiman 2012-08-14
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 的回复:]
这个楼主就是SB 自己会了 就不说了
[/Quote]
下面是action 中一个方法
try {
HttpServletResponse response = ServletActionContext.getResponse();
// 读取文件并且设置相关参数
String filePath = "F:\\struts.xml";
String fileName = "struts.xml";
File file = new File(filePath);
fileName = new String(fileName.getBytes("gb2312"),"iso8859-1");//取消乱码
byte[] buf = new byte[1024];
int len = 0;
BufferedInputStream br = null;
OutputStream ut = null;
response.reset();//必须加,不然保存不了临时文件
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition","attachment; filename=" + fileName);

br = new BufferedInputStream(new FileInputStream(file));
ut = response.getOutputStream();
while((len=br.read(buf))!=-1){
ut.write(buf, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
}

然后jsp页面 window.open('写调用action就行了')
zastll1314 2012-05-15
  • 打赏
  • 举报
回复
我的也是只能看到返回的文件流,并不能出现文件下载提示框。
也不知道怎么回事 气死我了啊
zastll1314 2012-05-15
  • 打赏
  • 举报
回复
这个楼主就是SB 自己会了 就不说了
javawangjiaqi 2012-04-23
  • 打赏
  • 举报
回复
ext下载 能否看下代码实现
liweishine 2011-02-23
  • 打赏
  • 举报
回复
兄弟.能否看下你extjs前端代码.
mshcf0920 2010-11-07
  • 打赏
  • 举报
回复
aaaaaaaaaaaa
tweij 2010-08-26
  • 打赏
  • 举报
回复
楼主在不啊?着急啊
tweij 2010-08-26
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 elia117 的回复:]

非常感谢IBM_hoojo经过提示和实验总算是成功了!
[/Quote]

楼主,把你的配置文件和后台代码贴出来分享下;非常感谢!
tweij 2010-08-26
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 elia117 的回复:]

非常感谢IBM_hoojo经过提示和实验总算是成功了!
[/Quote]
楼主,把你的解决方法贴出来分享一下,好吗?
elia117 2010-07-16
  • 打赏
  • 举报
回复
能将action中的实现给个吗
elia117 2010-07-16
  • 打赏
  • 举报
回复
换过了也不行。
hoojo 2010-07-16
  • 打赏
  • 举报
回复

<!-- 下载文件的Action定义 -->
<action name="download" class="action.DownLoadAction">
<!-- 设置文件名参数,由页面上传入 -->
<param name="fileName"></param>
<result name="success" type="stream">
<!-- 下载文件类型定义 -->
<param name="contentType">text/plain</param>
<!-- 下载文件处理方法 -->
<param name="contentDisposition">
attachment;filename="${downloadChineseFileName}"
</param>
<!-- 下载文件输出流定义 -->
<param name="inputName">downloadFile</param>
</result>
</action>
将content-type换下看看<param name="contentType">text/plain</param>
elia117 2010-07-16
  • 打赏
  • 举报
回复
非常感谢IBM_hoojo经过提示和实验总算是成功了!
elia117 2010-07-16
  • 打赏
  • 举报
回复
我试过了直接在地址栏输入DownLoadDocument.action&fid=2是可以的,但通过Ext页面的按钮调用就是不行。
hoojo 2010-07-16
  • 打赏
  • 举报
回复

<!------------文件名:DownLoadAction.java ------------------>
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class DownLoadAction extends ActionSupport {
//下载文件原始存放路径
private final static String DOWNLOADFILEPATH="/upload/";
//文件名参数变量
private String fileName;

public String getFileName() {
return fileName;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

//从下载文件原始存放路径读取得到文件输出流
public InputStream getDownloadFile() {
return
ServletActionContext.getServletContext().getResourceAsStream(DOWNLOADFILEPATH+fileName);
}
//如果下载文件名为中文,进行字符编码转换
public String getDownloadChineseFileName() {
String downloadChineseFileName = fileName;

try {
downloadChineseFileName = new String(downloadChineseFileName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

return downloadChineseFileName;
}

public String execute() {
return SUCCESS;
}
}

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧