PDA

Просмотр полной версии : JSP вывод бинарного файла(картинки)



shipr
04.02.2007, 09:50
Есть jsp страница

<%@ page contentType="image/JPEG" %><%
pageContext.setAttribute("Content-Length", request.getAttribute("length"));
byte[] PictureBytes = (byte[]) request.getAttribute("picture");
java.io.DataOutputStream dout = new java.io.DataOutputStream (new java.io.BufferedOutputStream(response.getOutputStr eam()));
dout.write(PictureBytes);
dout.close();
%>

выкидывает ошибку, но работает


StandardWrapperValve[action]: Servlet.service() for servlet action threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response

Если выводить данные по правильному, т.е. через out.write или out.print - такое чувство, что тип данных байт переводится в чар с потерей старшего бита (
Кто сталкивался с подобной проблемой - поделитесь решением :)
PS JDK 1.4.2

shipr
04.02.2007, 10:36
когда-то я подобное делал путём переписывания javax.servlet.jsp.JspWriter (класс объекта out). Ни у кого нет этого переписанного класса?

ЗЫ проверил на jdk 1.5 и 1.6 та же лажа (

shipr
05.02.2007, 21:31
вот наиболее (на мой взгляд) правильное решение этой проблемы
источник http://www.mooreds.com/wordpress/archives/000018

The JSP is taking my stream before my code has a chance. Therefore, I get an “getOutputStream() has already been called for this response” error. The weird bit is that this doesn’t seem to happen on Tomcat 4.1.24 on Windows (same version of struts).

So, what do you do? You write a servlet instead. That way you have utter control over the output stream:

—————


import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;

public class BinaryStreamServlet extends HttpServlet {

public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String contentType =
(String)req.getAttribute("contentType");
if (contentType == null || "".equals(contentType)) {
contentType = "image/png"; // default
}
res.reset();
res.setContentType(contentType);
OutputStream sos = res.getOutputStream();
ByteArrayOutputStream baos = (ByteArrayOutputStream)req.getAttribute("baos");
baos.writeTo(sos);
}
}
—————

I set up my action classes to cache the ByteArrayOutputStream in the request, with the name “baos.” I added these lines to my web.xml:

—————

<servlet>
<servlet-name>binaryFileServlet</servlet-name>
<servlet-class>BinaryStreamServlet</servlet-class>
</servlet>
....snip...
<servlet-mapping>
<servlet-name>binaryFileServlet</servlet-name>
<url-pattern>/binaryFile</url-pattern>
</servlet-mapping>
—————

and this to my struts-config.xml for any actions that needed to be able to send binary data:

—————
<forward name="success" path="/binaryFile"/>
—————

Works like a charm. Leave the JSPs to the character data, and use this simple servlet for binary delivery.


ЗЫ зачем, однако, тему открывал :rzhu_nimagu: ?
ЗЫЫ как закрыть тему?
ЗЫЫЫ никому джава програмер не нужен?
ЗЫЫЫЫ всёравно я делал как-то по другому (склероз) :shine: