Edward-Chai 2017-11-23 16:29 采纳率: 100%
浏览 1492
已采纳

Struts2 使用redirectAction结果类型传递参数时,会跳转到错误的路径

我希望通过点击下载按钮来跳转到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>&nbsp;
                <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;
    }
}
  • 写回答

2条回答

查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算