Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在jsf中包含jsp会导致输入长度为1的异常_Jsp_Exception_Jsf 2_Blob - Fatal编程技术网

在jsf中包含jsp会导致输入长度为1的异常

在jsf中包含jsp会导致输入长度为1的异常,jsp,exception,jsf-2,blob,Jsp,Exception,Jsf 2,Blob,我有以下jsp代码 myDisplayImage.jsp 编辑1 我还使用getBinaryStream尝试了新代码,但仍然存在相同的错误。代码是 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title&g

我有以下jsp代码

myDisplayImage.jsp
编辑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>

JSP页面
你好,世界!

您不能在XHTML中包含输出文本以外内容的JSP

问题是JSP将图像的输出与整个请求混合,将内容类型设置为
“image/jpeg”
(或在出现错误时输出
“text/html”
)。您将无法按原样将其包含在另一个请求中

在您的例子中,您可能只想删除所有HTML标记,让JSP只输出图像。然后,您将能够使用纯HTML正确显示图像,如下所示:

<img src="myDisplayImage.jsp" />

您不能在XHTML中包含输出文本以外内容的JSP

问题是JSP将图像的输出与整个请求混合,将内容类型设置为
“image/jpeg”
(或在出现错误时输出
“text/html”
)。您将无法按原样将其包含在另一个请求中

在您的例子中,您可能只想删除所有HTML标记,让JSP只输出图像。然后,您将能够使用纯HTML正确显示图像,如下所示:

<img src="myDisplayImage.jsp" />


< /代码>不应在响应请求的中间更改内容类型。在您的情况下,您希望使此(或其他)JSP文件专用于输出具有适当内容类型的图像。在响应请求时,不应更改内容类型。在您的情况下,您希望将此(或其他)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()) {
                    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>
<img src="myDisplayImage.jsp" />