1

jspに以下のコードがあります

myDisplayImage.jsp

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <%@ page import="java.sql.*"%>
        <%@ page import="java.io.*"%>
        <%
                Blob image = null;
                Connection con = null;
                byte[] imgData = null;
                Statement stmt = null;
                ResultSet rs = null;
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    con = DriverManager.getConnection("jdbc:mysql://localhost:8889/mySacDB", "root", "root");
                    stmt = con.createStatement();
                    rs = stmt.executeQuery("select photo from personalInfo where userid = 5");
                    if (rs.next()) {
                        image = rs.getBlob(1);
                        imgData = image.getBytes(1, (int) image.length());
                    }
                    // display the image
                    response.setContentType("image/jpeg");
                    OutputStream o = response.getOutputStream();
                    o.write(imgData);
                    o.flush();
                    o.close();
                } catch (Exception e) {
                    out.println("Unable To Display image<br /><br />");
                    out.println("Image Display Error=" + e.getMessage());
                    return;
                } finally {
                    try {
                        rs.close();
                        stmt.close();
                        con.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
        %>
    </body>
</html>

これを使うと、画像がきちんと表示されるようになります。

ただし、JSF 2.0 ページに同じページを含めると、例外が発生します。

<o:resourceInclude path="myDisplayImage.jsp"/>

ノート :o = xmlns:o="http://omnifaces.org/ui"

私が得る例外はInput length = 1. スタックトレースは

 java.nio.charset.MalformedInputException: Input length = 1
at java.nio.charset.CoderResult.throwException(CoderResult.java:260)
at java.nio.charset.CharsetDecoder.decode(CharsetDecoder.java:781)
at com.sun.faces.application.ByteArrayWebOutputStream.writeTo(ByteArrayWebOutputStream.java:112)
at com.sun.faces.application.ViewHandlerResponseWrapper.flushToWriter(ViewHandlerResponseWrapper.java:162)
at com.sun.faces.application.view.JspViewHandlingStrategy.renderView(JspViewHandlingStrategy.java:264)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:809)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:671)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:626)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:556)
at org.omnifaces.component.output.ResourceInclude.encodeBegin(ResourceInclude.java:79)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1755)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:349)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at com.sac.filter.MyFilter.doFilter(MyFilter.java:48)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:680)

編集 1

getBinaryStreamまた、同じエラーを使用して新しいコードを試しました。コードは

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <%@ page import="java.sql.*"%>
        <%@ page import="java.io.*"%>
        <%
            Blob image = null;
            Connection con = null;
            byte[] imgData = null;
            Statement stmt = null;
            ResultSet rs = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                con = DriverManager.getConnection("jdbc:mysql://localhost:8889/mySacDB", "root", "root");
                stmt = con.createStatement();
                rs = stmt.executeQuery("select photo from personalInfo where userid = 5");
                if (rs.next()) {
                    String imgLen = rs.getString(1);
                    System.out.println(imgLen.length());
                    int len = imgLen.length();
                    byte[] rb = new byte[len];
                    InputStream readImg = rs.getBinaryStream(1);
                    int index = readImg.read(rb, 0, len);
                    System.out.println("index----------------" + index);
                    response.reset();
                    response.setHeader("Content-Length", String.valueOf(len));
                    String tempString = "image/jpeg";
                    String tempExtenson = tempString.substring(tempString.lastIndexOf("/") + 1, tempString.length());
                    response.setHeader("Content-disposition", "inline;filename=/file." + tempExtenson);
                    response.setContentType(tempString);
                    response.getOutputStream().write(rb, 0, len);
                    response.getOutputStream().flush();
                }
                stmt.close();
                // display the image
            } catch (Exception e) {
                out.println("Unable To Display image<br /><br />");
                out.println("Image Display Error=" + e.getMessage());
                return;
            } finally {
                try {
                    rs.close();
                    stmt.close();
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        %>
    </body>
</html>
4

1 に答える 1

1

テキスト以外のものを出力する JSP を XHTML に含めることはできません。

問題は、JSP がイメージの出力をリクエスト全体に混ぜて、コンテンツ タイプを"image/jpeg"(または"text/html"何らかのエラーが発生したときに出力する) に設定していることです。そのまま別のリクエストに含めることはできません。

あなたの場合、おそらくすべての HTML タグを削除して、JSP が画像を出力するようにしたいでしょう。次に、次のように、プレーンな HTML を使用して画像を適切に表示できます。

<img src="myDisplayImage.jsp" />
于 2012-07-26T13:10:23.447 に答える