fighter1945 2009-01-06 15:21 采纳率: 0%
浏览 245
已采纳

[Struts] struts1.x下载文件完成后跳转到成功画面

看了好多网上用struts1.x实现文件下载功能的介绍了
action的写法大致有三 1, 用输入流,输出流
2, 用dispatcher
3, 用downloadAction
可是,怎么没看见个下载完成后跳转到成功画面的写法呢
用了第一和第二两种写法,最后都只能return null,如果写成return mapping.findForward("success"),则都会因为response已经提交而报exception
莫非都是下载完了就什么也不管了?
很疑惑,不知道怎么办好?有解决方法么,或者非正规的解决方法也好

[b]问题补充:[/b]

这段代码也是我从论坛上看来的,下载没有问题,画面forward不可

[code="java"]
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {

        FileDownLoadForm fdform =(FileDownLoadForm)form;
        String path = fdform.getFilePath();

        BufferedInputStream   bis = null;
        BufferedOutputStream  bos = null;
        InputStream fis = null;
        OutputStream fos = null;

        try{

            File uploadFile = new File(path);


            fis = new FileInputStream(uploadFile);
            bis = new BufferedInputStream(fis);
            fos = response.getOutputStream();
            bos = new BufferedOutputStream(fos);



            response.reset();
            response.setHeader("Content-disposition", "attachment;filename =" + URLEncoder.encode(path, "utf-8"));


            int bytesRead = 0;
            byte[] buffer = new byte[4096];
            while((bytesRead = bis.read(buffer,0,4096)) != -1){
                bos.write(buffer, 0, bytesRead);
            }
            bos.flush();
            fis.close();
            bis.close();
            fos.close();
            bos.close();
            //return mapping.findForward("success");

            return null;
        } catch(Exception e){
            e.printStackTrace();
            System.out.println(path);
            ActionMessages downErrors = new ActionMessages();
            ActionMessage downError =  new ActionMessage("errors.download", path);
            downErrors.add(ActionMessages.GLOBAL_MESSAGE, downError);
            saveMessages(request, downErrors);
            return mapping.findForward("error");
        }

    }

[/code]
[b]问题补充:[/b]

对应第二种方法的
也是从论坛看来的

[code="java"]

public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)  {
        FileDownLoadForm fdform =(FileDownLoadForm)form;
        String path = fdform.getFilePath();


        response.setContentType("application/x-download");
        String filenamedownload = path;
        String filenamedisplay = path.substring(path.lastIndexOf("/")+1);


        try {
            filenamedisplay = URLEncoder.encode(filenamedisplay,"UTF-8");
            response.addHeader("Content-Disposition","attachment;filename=" + filenamedisplay);
            RequestDispatcher dispatcher = request.getRequestDispatcher(filenamedownload);
            if(dispatcher != null)
            {
                dispatcher.forward(request,response);
            }
            response.flushBuffer();

           return null ;
           //  return mapping.findForward("success");
        }
        catch(Exception e) {
            e.printStackTrace();
            ActionMessages downErrors = new ActionMessages();
            ActionMessage downError =  new ActionMessage("errors.download", path);
            downErrors.add(ActionMessages.GLOBAL_MESSAGE, downError);
            saveMessages(request, downErrors);
            return mapping.findForward("error");

        }

    }

[/code]

  • 写回答

5条回答 默认 最新

  • buaalijie 2009-01-06 16:48
    关注

    可以分两步,第一步处理下载之前的事务,第二步利用弹出窗口做下载。在弹出窗口上面做文章控制父页面的消息显示。
    想直接一个请求做是不行的,因为response已经返回了的缘故。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面