struts2 文件下载

jsp文件

<td width="70%" style="padding:5px">
<%List<Accessory> accessoryList=(List<Accessory>)request.getAttribute("accessoryList");
for(Accessory accessory:accessoryList){
if(accessory!=null){
%>
<a href="downloadFileAction.action?fileName=<%=accessory.getFile_address()%>&trenName=<%=URLEncoder.encode(accessory.getFile_name(),"UTF-8")%>"><%=accessory.getFile_name()%></a><br>
<%
}
}
%>
</td>

下载action

package com.longyi.citicoa.domain.action;

import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class DownloadFileAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private String fileName; //下载文件名
private String trenName; //文件真实名称

//从下载文件原始存放路径读取得到文件输出流
public InputStream getDownloadFile(){
return ServletActionContext.getServletContext().getResourceAsStream(fileName);
}

public String execute(){
return SUCCESS;
}

//中文需要转码tomcat配过所以不要

//get and set method
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}

public String getTrenName() {
return trenName;
}

public void setTrenName(String trenName) {
this.trenName = trenName;
}


}


struts.xml配置
<!-- 文件下载 -->
<action name="downloadFileAction" class="com.longyi.citicoa.domain.action.DownloadFileAction">
<param name="fileName"></param>
<result name="success" type="stream">
<param name="contentType">text/plain</param>
<param name="contentDisposition">
attachment;fileName="${trenName}"
</param>
<param name="encode">true</param>
<param name="inputName">downloadFile</param>
</result>
</action>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值