struts2文件下载

Struts2文件下载
目录:
[img]http://dl2.iteye.com/upload/attachment/0090/3458/4cfcc747-3789-3ffa-8e99-fb0f71ddeee3.jpg[/img]

struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="down" namespace="/downLoad" extends="struts-default">
<action name="liuDownLoad" class="liu.chao.action.LiuDownLoad">
<result name="success" type="stream">
<!-- 下载的文件类型 -->
<param name="contentType">application/octet-stream</param>
<!-- 下载的文件处理方式 -->
<param name="contentDisposition">attachment;fileName="${fileName}"</param>
<!-- 下载文件的输出流对应于Action里面的getInputStream()方法 -->
<param name="inputName">inputStream</param>
</result>
</action>
</package>
</struts>

Action:
package liu.chao.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class LiuDownLoad extends ActionSupport {

private String filePath;//要下载文件的目录 如:images
private String fileName;//要下载的文件名称 如:003.png
public String getFilePath() {
return filePath;
}

public void setFilePath(String filePath) {
this.filePath = filePath;
}

public String getFileName() {
return fileName;
}

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


public InputStream getInputStream()throws Exception{

String path=ServletActionContext.getServletContext().getRealPath(filePath);
File file=new File(path,fileName);
InputStream input=new FileInputStream(file);
return input;
}



public String execute(){

return SUCCESS;
}


}

前台代码:
    <form action="downLoad/liuDownLoad.action" name="form" method="post">
文件要下载目录(eg:images):<input type="text" name="filePath" id="003"/></br>
文件名称(eg:003.png):<input type="text" name="fileName" id="004"/>
<input type="submit" value="下载"/>
</form>

运行效果:
[img]http://dl2.iteye.com/upload/attachment/0090/3460/5b83b77f-6067-34ca-9f87-f0233b76882e.png[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值