struts2 如何通过连接下载服务器端文件
2个回答
展开全部
页面:<a href="${pageContext.request.contextPath }/file/downLoadAction.action?relativePath=${relativePath }">${fileFileName}</a>
struts.xml
这里主要注意的是,使用的result的类型是stream
<action name="downLoadAction" class="hwt.action.DownLoadAction">
<!
- - 设置result的类型为stream- - >
<result name="downLoad"
type="stream">
<param name="contentType">text/plain</param>
<param name="inputName">fileStream</param>
<param
name="contentDisposition">filename="${fileName}"</param>
<param name="bufferSize">1024</param>
</result>
</action>
Action中
继承actionsupport
public class DownLoadAction extends ActionSupport{
private String fileName;
private String relativePath ;
/*
* getFileStream()这个方法的名字和struts.xml的配置文件中的
* <param
name="inputName">fileStream</param>
*/
public InputStream getFileStream(){
relativePath = relativePath.replace("\\","/");
return ServletActionContext.getServletContext().getResourceAsStream("/"+FileUploadUtils.baseName+"/"+relativePath);
}
public String execute(){
return "downLoad";
}
public String getFileName() {
fileName = relativePath.substring(relativePath.lastIndexOf("/")+1);
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getRelativePath() {
return relativePath;
}
public void setRelativePath(String relativePath) {
this.relativePath = relativePath;
}
}
struts.xml
这里主要注意的是,使用的result的类型是stream
<action name="downLoadAction" class="hwt.action.DownLoadAction">
<!
- - 设置result的类型为stream- - >
<result name="downLoad"
type="stream">
<param name="contentType">text/plain</param>
<param name="inputName">fileStream</param>
<param
name="contentDisposition">filename="${fileName}"</param>
<param name="bufferSize">1024</param>
</result>
</action>
Action中
继承actionsupport
public class DownLoadAction extends ActionSupport{
private String fileName;
private String relativePath ;
/*
* getFileStream()这个方法的名字和struts.xml的配置文件中的
* <param
name="inputName">fileStream</param>
*/
public InputStream getFileStream(){
relativePath = relativePath.replace("\\","/");
return ServletActionContext.getServletContext().getResourceAsStream("/"+FileUploadUtils.baseName+"/"+relativePath);
}
public String execute(){
return "downLoad";
}
public String getFileName() {
fileName = relativePath.substring(relativePath.lastIndexOf("/")+1);
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getRelativePath() {
return relativePath;
}
public void setRelativePath(String relativePath) {
this.relativePath = relativePath;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询