Servlet从服务器发送图像并保存在客户端 [英] Servlet send image from server and save in client

查看:194
本文介绍了Servlet从服务器发送图像并保存在客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开发J2EE。
我正在修改一个现有的应用程序(一个OpenSource项目)。
我需要在服务器发送的客户端上保存图像,但是我不知道如何。
此活动必须以透明的方式进行,而不影响应用程序的现有操作。



从测试完成后,我收到此错误:
java.lang.IllegalStateException:getWriter()已被调用此响应。



根据您的意见,应该如何执行此任务?
如何在客户端本地保存图像?



更新



感谢您的答案。
我的问题是:


  1. 图像是在服务器上生成的,但不是直接的客户端请求(没有链接点击网页),图片由互联网上的其他服务组成。

  2. 重建服务器上的图像。

  3. 此图片必须发送到客户端以保存在本地。

  4. 所以我希望它出现一个窗口,您分配目标图像

  5. 加上这个应用程序的其余部分不受此活动的影响。

  6. 该应用程序尚未生效。

非常感谢你的回复。

解决方案


我得到这个错误:java.lang.IllegalStateException:getWriter()已被调用此响应。


换句话说,你试图将图像的二进制数据与HTML的字符数据混合在一起或者你试图在JSP而不是Servlet中执行此操作。这的确不行。您需要专门发送图像 HTML页面,以回应完全独立的请求。



在您的JSP / HTML页面只能连接到图像,如下所示:

 < a href =imageservlet / filename.gif >点击下载图片< / a> 

然后,在一个servlet中收听 url-pattern / imageservlet / * ,您只需从某个数据源(例如从本地磁盘)获取图像为 InputStream 文件系统作为 FileInputStream),然后将其写入通常的Java IO方式的响应的 OutputStream



您只需要至少将 Content-Disposition 响应标题设置为附件以确保客户端获得另一个弹出对话框,否则将直接在浏览器中显示。设置内容类型 Content-Length 也很重要,以便浏览器知道服务器发送和可以预测下载可能需要多长时间。

  response.setHeader(Content-Type,getServletContext()。getMimeType的getName())); 
response.setHeader(Content-Length,String.valueOf(file.length()));
response.setHeader(Content-Disposition,attachment; filename = \+ file.getName()+\);

您可以在本文



注意:您无法控制客户端将保存的位置形象,这将是一个安全漏洞。这样网站就能够在客户端的磁盘上写入恶意文件。






更新:根据您的更新,有两个选项:


  1. 您需要让客户端本身触发两个HTTP请求(我在您的后续问题


  2. 创建客户端应用程序,直接在客户端执行所有任务,然后将其嵌入到您的网页中,例如 Java Applet 。使用小程序,您可以完全控制客户端环境。您可以执行几乎所有要执行的Java代码,您可以直接将文件写入磁盘,而不要求客户端保存位置。您只需要由第三方公司签署小程序,或者客户需要在运行前确认安全警告。



I'm new and just developing on J2EE. I am modifying an existing application (an OpenSource project). I need to save an image on a client sent by the server, but I do not know how. This activity must be done in a transparent manner without affecting the existing operation of the application.

From the tests done I get this error: java.lang.IllegalStateException: getWriter () has Already Been Called for this response.

How should carry out this task, according to your own opinion? How do I save on the client, locally, the image?

Update:

Thanks for the answers. My problem is that:

  1. the image is generated on the server, but not for direct client request (there is no link to click on web page), the picture is composed using other services on the Internet.
  2. reconstruct the image on the server.
  3. This image must be sent to the client to be saved locally.
  4. so I'd like it to appear a window where you assign the destination image
  5. plus I'd like the rest of the application were not affected by this activity.
  6. The application is yet on production.

Thank you very much for your response.

解决方案

From the tests done I get this error: java.lang.IllegalStateException: getWriter () has Already Been Called for this response.

In other words, you were trying to mix the binary data of the image with the character data of the HTML output, or you were trying to do this in a JSP instead of a Servlet. This is indeed not going to work. You need to send either the image or the HTML page exclusively in response to fully separate requests.

In your JSP/HTML page just have a link to the image, like so:

<a href="imageservlet/filename.gif">click to download image</a>

Then, in a servlet listening on an url-pattern of /imageservlet/*, you just get the image as InputStream from some datasource (e.g. from local disk file system as FileInputStream) and then write it to the OutputStream of the response the usual Java IO way.

You only need to set at least the Content-Disposition response header to attachment to make sure that the client get a Save As popup dialogue, else it will be displayed straight in the browser. Setting the Content-Type and Content-Length are also important so that the browser knows what the server is sending and can predict how long the download may take.

response.setHeader("Content-Type", getServletContext().getMimeType(file.getName()));
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Disposition", "attachment;filename=\"" + file.getName() + "\"");

You can find complete basic servlet example in this article.

Note: you cannot control where the client would save the image, this would be a security hole. This way websites would be able to write malicious files on client's disk unaskingly.


Update: as per your update, there are two options:

  1. You need to let the client itself fire two HTTP requests (I've answered this in your subsequent question)

  2. Create a client side application which does all the task directly at the client side and then embed this in your webpage, for example a Java Applet. With an applet you have full control over the client environment. You can execute almost all Java code you'd like to execute and you can write files to disk directly without asking client for the location to save. You only need to sign the applet by a 3rd party company or the client needs to confirm a security warning before running.

这篇关于Servlet从服务器发送图像并保存在客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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