下载图像后,我想重定向另一个页面,但不能 [英] after download a image ,i want to redirect on another page, but not able to

查看:76
本文介绍了下载图像后,我想重定向另一个页面,但不能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HttpServletResponse response)抛出ServletException,IOException {

HttpServletResponse response) throws ServletException, IOException {

    URL url = new URL("http://www.avajava.com/images/avajavalogo.jpg");
    System.out.println(url.getFile().substring(url.getFile().lastIndexOf("/")+1, url.getFile().length()));
    URLConnection connection = url.openConnection();
    InputStream stream = connection.getInputStream();
    response.setHeader("Cache-Control", "no-cache");
    ServletContext context = getServletContext();
    String  mimeType = "application/octet-stream";
    response.setContentType(mimeType);
    response.setHeader("Content-Disposition", "attachment; filename=icon" + ".jpg");
    String headerKey = "Content-Disposition";
    String headerValue = String.format("attachment; filename=\"%s\"",url.getFile().substring(url.getFile().lastIndexOf("/")+1, url.getFile().length()));
    response.setHeader(headerKey, headerValue);
    OutputStream outStream = response.getOutputStream();
    byte[] buffer = new byte[4096];
    int bytesRead = -1;
      while ((bytesRead = stream.read(buffer)) != -1) {
       outStream.write(buffer, 0, bytesRead);
   }

// stream.close();
// outStream.close();

// stream.close(); // outStream.close();

      RequestDispatcher rd = getServletContext().getRequestDispatcher("/next.html");

   rd.include(request, response);


推荐答案

好的,假设用户点击链接或者下载按钮,应用程序应该下载图像,一旦完成,它应该导航到另一个页面。如果是这种情况,则以下解决方法有帮助。

Ok suppose in your case when the user clicks a link or download button the application should download the image and once that is complete it should navigate to another page. If that is the case the following workaround help.

您的解决方案无法工作的原因是您首先下载图像并将流写入响应对象的输出strem。这将提交流。因此,如果您在写入流后尝试重定向,则会收到IllegaStateException。这就是在这种情况下发生的事情,它不会被转发。

The reason why your solution didnt work is because you first download the image and write the stream to response objet's output strem. This will commits the stream. So if you try a redirect after writing to a stream you'll get an IllegaStateException. That is what happening in this case and it wont get forwarded.

我建议的解决方法是分别处理两个动作。假设我有一个下载图像的servlet和另一个导航到下一页的servlet。然后你可以使用jquery的文件下载插件等待下载发生,然后触发导航流程。

The workaround i'm proposing is to treat two actions separately. Say i've one servlet which downloads the image and another which navigates to next page. Then you can use jquery's file download plugin to wait for a download to happen and then trigger the navigation flow.

所以我的第一页就像。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-1.9.0.min.js"></script>
<script type="text/javascript" src="js/jquery.fileDownload.js"></script>
<script>
    $(document).on("submit", "form.fileDownloadForm", function(e) {

        $.fileDownload($(this).prop('action'), {
            successCallback : function(url) {
                $('form#nextPage').submit();
            },
            failCallback : function(responseHtml, url) {
                alert('File download failed!!!!');
            }
        });

        e.preventDefault();
    });
</script>
</head>
<body>
    First
    <form action="<%=request.getContextPath()%>/img" method="get"
        class="fileDownloadForm" id="imgForm">
        <input type="submit" value="Download Image">
    </form>
    <form action="<%=request.getContextPath()%>/test" method="get"
        id="nextPage"></form>
</body>
</html>

所以这里我有两种形式(因为我正在使用表单提交方法。你可以避免这种情况,如果你使用链接),imgForm和nextPage。因此,当我们点击imgForm上的下载按钮时,会调用jquery下载插件脚本。请注意e.preventDefault()方法。这将阻止页面提交,以便页面等待下载完成,并在成功完成后调用successCallback方法。因此,我使用下一个提交导航到下一页。如果您不想要第二个表单,您可以使用其他方法,例如设置window.location =

So here i have two forms (since i'm using form submit method. You can avoid this if you use links), imgForm and nextPage. So when we click the download button on imgForm the jquery download plugin script get invoked. Notice the e.preventDefault() method. This will prevents the page from submitting so the page will wait for the download to complete and on successful completion it invoked the successCallback method. So in that i use the next submit to navigate to next page. If you dont want a second form you could use another methods like setting window.location =

否则为servelts。我有两个servlet用于下载图像,第二个用于导航到下一页。

No for the servelts. I have two servlets one to download the image and second to navigate to next page.

图像Servlet

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ImageServlet
 */
public class ImageServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public ImageServlet() {
    super();
    // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        URL url = new URL("http://www.avajava.com/images/avajavalogo.jpg");
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.9.248.37", 18082));
        System.out.println(url.getFile().substring(url.getFile().lastIndexOf("/")+1, url.getFile().length()));
        URLConnection connection = url.openConnection(proxy);
        InputStream stream = connection.getInputStream();
        response.setHeader("Cache-Control", "no-cache");
        ServletContext context = getServletContext();
        String  mimeType = "application/octet-stream";
        response.setContentType(mimeType);
        response.setHeader("Content-Disposition", "attachment; filename=icon" + ".jpg");
        response.setHeader("Set-Cookie", "fileDownload=true; path=/");
        String headerKey = "Content-Disposition";
        String headerValue = String.format("attachment; filename=\"%s\"",url.getFile().substring(url.getFile().lastIndexOf("/")+1, url.getFile().length()));
        response.setHeader(headerKey, headerValue);
        OutputStream outStream = response.getOutputStream();
        byte[] buffer = new byte[4096];
        int bytesRead = -1;
        while ((bytesRead = stream.read(buffer)) != -1) {
           outStream.write(buffer, 0, bytesRead);
       }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}

导航Servlet

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Test
 */
public class Test extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Test() {
    super();
    // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        RequestDispatcher rd = getServletContext().getRequestDispatcher("/next.jsp");
        rd.include(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

}

希望很清楚。

请注意ImageServlet中的两个标题

Please note the two headers in ImageServlet

response.setHeader("Content-Disposition", "attachment; filename=icon" + ".jpg");
response.setHeader("Set-Cookie", "fileDownload=true; path=/");

jquery插件需要这些标头才能正常工作。您可以从 http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/

jquery plugin needs these headers to work porperly. You can get more details about jquery download plug in from http://johnculviner.com/jquery-file-download-plugin-for-ajax-like-feature-rich-file-downloads/

这篇关于下载图像后,我想重定向另一个页面,但不能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆