gpt4 book ai didi

mysql - 来自 MySQL 的图像上的问号

转载 作者:行者123 更新时间:2023-11-30 00:40:15 26 4
gpt4 key购买 nike

我有一个 MySQL 数据库,其中有以下列:名字姓氏灯泡

我有一个 servlet 和一个表单,可以将图片上传到我的 MySQL 数据库,并且成功。当我从数据库下载图片时,它会为我提供正确的图片。我现在想在浏览器中显示图片,因此我尝试制作一个表单和一个 servlet。当我要求提供图片时,我只得到一个问号。有谁知道这是为什么吗?

致以诚挚的问候麦兹

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Get Image</title>
</head>
<body>
<p>Here you can see the image</p>

<img src="ImageServlet">

</body>
</html>


import java.sql.*;
import java.io.*;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;


@WebServlet(urlPatterns = {"/ImageServlet"})

public class ImageServlet extends HttpServlet{

private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException{
//PrintWriter pw = response.getWriter();


String connectionURL = "jdbc:mysql://localhost/dblogin";
Connection connection = null;
response.setContentType("image/jpg");

try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL,"root","");
Statement st1 = connection.createStatement();


ResultSet rs1 = st1.executeQuery
("SELECT image FROM photo WHERE lastname='test'");

if(rs1.next()){
InputStream is = rs1.getBinaryStream("photo");
byte[] b = new byte[10240];
int n;
while((n = is.read(b)) >= 0) {
response.getOutputStream().write(b, 0, n);
System.out.println(n);
}
is.close();

st1.close();

}
}
catch (Exception e){
e.printStackTrace();
}
}
}

最佳答案

这是正确的代码:

package jsp;

import java.sql.*;
import java.io.*;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;


@WebServlet(urlPatterns = {"/ImageServlet"})

public class ImageServlet extends HttpServlet{

private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException{
//PrintWriter pw = response.getWriter();


String connectionURL = "jdbc:mysql://localhost/dblogin";
Connection connection = null;
response.setContentType("image/jpg");

try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL,"root","");
Statement st1 = connection.createStatement();


ResultSet rs1 = st1.executeQuery
("SELECT photo FROM login WHERE lastname='test'");

if(rs1.next()){
InputStream is = rs1.getBinaryStream("photo");
byte[] b = new byte[10240];
int n;
while((n = is.read(b)) >= 0) {
response.getOutputStream().write(b, 0, n);
System.out.println(n);
}
is.close();

st1.close();

}
}
catch (Exception e){
e.printStackTrace();
}
}
}

关于mysql - 来自 MySQL 的图像上的问号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21826539/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com