package com.tcf.action; import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream; import com.opensymphony.xwork2.ActionSupport; public class DownLoadAction extends ActionSupport {
private String inputPath;
private String fileName;
private InputStream inputStream;
private String contentType; public String getInputPath() {
return inputPath;
} public void setInputPath(String inputPath) {
this.inputPath = inputPath;
} public String getFileName() {
return fileName;
} public void setFileName(String fileName) {
this.fileName = fileName;
} public String getContentType() {
return contentType;
} public InputStream getInputStream() {
return inputStream;
} public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
} public void setContentType(String contentType) {
this.contentType = contentType;
} public String execute(){
try {
this.inputStream = new BufferedInputStream(new FileInputStream(inputPath+"/"+fileName));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SUCCESS;
}
}
struts.xml
<action name="download" class="com.tcf.action.DownLoadAction">
<param name="inputPath">E:/temp</param>
<param name="fileName">back.gif</param>
<result type="stream">
<param name="contentType">image/gif</param>
<param name="inputName">inputStream</param>
<!-- 附件 -->
<param name="contentDisposition">
attachment;filename="${fileName}"
</param>
<param name="bufferSize">4096</param>
</result>
</action>
download.jsp
<a href="download.action" >下载</a>