How to send a file from a servlet the “right” way?

后端 未结 1 1334
谎友^
谎友^ 2021-01-07 04:32

I\'m trying to send a file to a user from an http servlet. The servlet runs some identification tests (on the request) and then sends the client a file.

This general

相关标签:
1条回答
  • 2021-01-07 05:08

    The solution to the problem is the "Client-Pull" technique. By adding a Refresh value to the header we make the browser ask for the file.

    This is the only solution I could come up to that overcomes the fact the gmail will use redirection when pressing a link from within an email.

    In the code I did this:

    response.setHeader("Refresh", "3; URL=\"" + url.toString() + "\"");
    forwardToJSP(request, response, "waitForBrowserRefreshPage.jsp");
    

    Meaning - after 3 seconds ask the user for the specified url which will, in turn, deliver the file to the client. The forwardToJSP method displays the "You will be forwarded soon, here a link if it fails" message.

    0 讨论(0)
提交回复
热议问题