如何从一个servlet发送一个文件的“正确”办法? [英] How to send a file from a servlet the "right" way?
问题描述
我试图从一个http servlet发送一个文件给用户。
servlet运行一些鉴别测试(在请求上),然后发送给客户端一个文件。
这个通用工作,但现在我打开我的TOMCAT服务器重定向到https,当我尝试访问该servlet并从IE6或IE8下载文件失败,我得到这个例外:
java.lang.IllegalStateException:提交响应后无法转发
(在localhost.log中)
和
ClientAbortException:java.net.SocketException:连接重置通过peer:socket写入错误
(在servlet日志中)
发送的代码(简体):
private void sendFile(HttpServletResponse response,byte []文件,字符串clientFileName)
{
ServletOutputStream op = null;
setContentType(response);
response.setHeader(Content-Disposition,attachment; filename = \+ clientFileName +\);
//发送字节数组到输出缓冲区。
op = response.getOutputStream();
//完成所有处理后,内容长度必须设置。
response.setContentLength((int)file.length);
op.write(file);
$ b 这是从servlet发送文件的正确方法吗?最好的方法是什么?
感谢!!
更新
在此链接中使用@BalusC artice中的代码:
http://balusc.blogspot.com/2007/07/fileservlet.html
这使它工作。
在Gmail中使用IE6-IE8时仍然不起作用,因为gmail会在这些浏览器中添加下载过滤功能。
更新2
这个问题似乎与Gmail + Internet Explorer 6-8有关。
我假设Gmail正在做一个重定向(这实际上很明显,如果你点击邮件中的链接后,在页面上的网址)。
客户端拉技术是我唯一的解决方案吗?解决方案
技术。
通过在头文件中添加 Refresh
值,我们让浏览器询问文件。
是唯一可以解决的问题,克服了gmail在按下电子邮件链接时使用重定向的问题。
在代码中,我这样做了: / p>
response.setHeader(Refresh,3; URL = \+ url.toString()+\ );
forwardToJSP(request,response,waitForBrowserRefreshPage.jsp);
含义 - 3秒后询问用户指定的url,然后将文件传递到文件给客户。
forwardToJSP
方法显示您将很快转发,这里是一个链接,如果失败的消息。
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 generally works but now I turned on my TOMCAT server redirects to https and when I try to access the servlet and download the file from either IE6 or IE8 it fails and I get this exception:
java.lang.IllegalStateException: Cannot forward after response has been committed
(on the localhost.log)
and
ClientAbortException: java.net.SocketException: Connection reset by peer: socket write error
(in the servlet log)
The code that does the sending (Simplified):
private void sendFile(HttpServletResponse response, byte[] file, String clientFileName)
{
ServletOutputStream op = null;
setContentType(response);
response.setHeader("Content-Disposition", "attachment; filename=\"" + clientFileName + "\"");
// send byte array to output buffer.
op = response.getOutputStream();
// Content Length must be set after all processing done.
response.setContentLength((int) file.length);
op.write(file);
}
Is this the correct way to send a file from a servlet? What's the best way?
Thanks!!
UPDATE
Used the code from @BalusC artice in this link: http://balusc.blogspot.com/2007/07/fileservlet.html
This made it work.
Still doesn't work in IE6-IE8 when used from Gmail because of a filtering stage gmail adds to downloads in these browsers.
UPDATE 2
The problem seems to be with Gmail + Internet Explorer 6-8. I'm Assuming that gmail is doing a redirect (That's actually pretty obvious if you look at the url on the page after you click the link in the mail). Is Client-Pull technique my only solution?
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.
这篇关于如何从一个servlet发送一个文件的“正确”办法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!