我希望通过点击下载按钮来跳转到DownloadCount.action并传递参数filename来在数据库中进行一次下载计数,再让filename参数通过redirectAction方式传参给downfile.action来进行下载,但是DownloadCount.action执行完成后会跳转到http://downfile.action/?filename=1511398398834.xlsx,问下各位为什么会出现这种情况以及如何解决,谢谢!
private.jsp
<html>
<head>
<title>Title</title>
</head>
<body>
<p><a href='<s:url value="upload.jsp"></s:url>'>上传文件</a></p>
<table>
<tr><td>文件id</td><td>文件名</td><td>文件类型</td><td>文件大小</td><td>上传者</td><td>最后下载时间</td><td>下载次数</td><td>最后下载用户</td><td>文件操作</td></tr>
<s:iterator value="filelist" var="file" id="file" >
<tr>
<td><s:property value="#file.fileid"/></td>
<td><s:property value="#file.filename"/></td>
<td><s:property value="#file.filetype"/></td>
<td><s:property value="#file.filesize"/></td>
<td><s:property value="#file.uploader"/></td>
<td><s:property value="#file.lastdtime"/></td>
<td><s:property value="#file.downtimes"/></td>
<td><s:property value="#file.lastdownloader"/></td>
<td><a href='<s:url value="DownloadCount.action"><s:param name="filename" value="#file.filename"></s:param></s:url>'>下载</a>
<a href='<s:url value="FileDelete.action"><s:param name="filename" value="#file.filename"></s:param><s:param name="uname" value="uname"></s:param></s:url>'>删除</a>
</td>
</tr>
</s:iterator>
</table>
<s:debug></s:debug>
</body>
</html>
DownloadCountAction.java
package com.action;
import com.opensymphony.xwork2.ActionSupport;
import com.util.DBTools;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
@Namespace("/")
@ParentPackage("struts-default")
public class DownloadCountAction extends ActionSupport {
private String filename;
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
@Action(value = "DownloadCount",results = {
@Result(name = "success",location = "/downfile.action?filename=${filename}",type = "redirectAction")
})
public String DownloadCount() throws Exception{
DBTools db=new DBTools();
db.FileDownload(filename);
return SUCCESS;
}
}
struts.xml中关于downfile.action的配置
<action name="downfile" class="com.action.DownFileAction" method="downloadFile">
<result name="success" type="stream">
<param name="bufferSize">4096</param>
<param name="contentDisposition">
attachment;filename="${filename}"
</param>
<param name="inputName">downFile</param>
</result>
</action>
DownFileAction.java
package com.action;
import com.opensymphony.xwork2.ActionSupport;
import com.util.DBTools;
import org.apache.struts2.ServletActionContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
public class DownFileAction extends ActionSupport{
private String filename;
public String getFilename() {
return filename;
}
public void setFilename(String filename) throws Exception {
this.filename = new String(filename.getBytes("ISO8859-1"),"utf-8");
}
public String downloadFile() throws Exception {
return SUCCESS;
}
public InputStream getDownFile()throws Exception{
String path=ServletActionContext.getServletContext().getRealPath("/WEB-INF/uploadlist");
File file=new File(path+"/"+this.getFilename());
InputStream inputStream=new FileInputStream(file);
return inputStream;
}
}