文件下载
//文件下载
在标签里制定文件的路径:
<a href="download.action?fileName=16.1.txt">点击此处下载文档</a>
制定的文件是web工程中的
<action name="download" class="action.FileDownAction">
<param name="inputPath">/image</param>
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">
arrachment;filename="${fileName}"
</param>
<param name="bufferSize">4096</param>
</result>
</action>
创建真正文件下载类FileDownAction
package action;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileDownAction 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 InputStream getInputStream() throws Exception {
String path =ServletActionContext.getServletContext().getRealPath(inputPath);
return new BufferedInputStream(new FileInputStream(path+"\\"+fileName));
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
}