【问题标题】:Using JSP to store images in Mysql and upload the images online使用JSP在Mysql中存储图片并在线上传图片
【发布时间】:2015-06-24 19:30:43
【问题描述】:

我仍然想知道我做错了什么,我的问题是我不知道是我的整个示例代码有错误还是我与数据库的连接有问题,就我所在的位置而言我不确定是什么让我收到此错误。 (如下所示)。我试图在这里查看堆栈溢出,但我得到的只是使用 JSP 加载图像,但没有将它们添加到数据库中。如果我们有一个我无法追踪,请帮助我提供链接。我已经包含了所有需要的库,除了这个输出之外,我的代码没有错误。我来到这里是因为我被困住了,需要简要回顾一下您的专业人士认为我的代码有什么问题,如下所示。我将非常感谢在截止日期前提供的任何帮助。 我的数据库名称是

应用数据库 我想知道我应该使用表的名称吗?插入?这是 '联系人'

错误信息

HTTP 状态 404 - /UploadImageOnWeb/uploadServlet 类型状态报告 消息 /UploadImageOnWeb/uploadServlet 描述 请求的
资源不可用。 Apache Tomcat/8.0.23

谢谢

Java Servlet 代码

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

/**
 * Servlet implementation class FileUploadDBServlet
 */
@MultipartConfig(maxFileSize = 16177215)
@WebServlet("/FileUploadDBServlet")
public class FileUploadDBServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    // database connection settings
    /*private String dbURL = "jdbc:mysql://localhost/AppDB";
    private String dbUser = "root";
    private String dbPass = "mypassword";*/

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");

        InputStream inputStream = null; 


        Part filePart = request.getPart("photo");
        if (filePart != null) {

            System.out.println(filePart.getName());
            System.out.println(filePart.getSize());
            System.out.println(filePart.getContentType());


            inputStream = filePart.getInputStream();
        }

        Connection conn = null;
        String message = null;  

        try {
            // connects to the database
            /*DriverManager.registerDriver("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(dbURL, dbUser, dbPass);*/

           Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost/AppDB","root","mypassword");




            // constructs SQL statement
            String sql = "INSERT INTO contacts (first_name, last_name, photo) values (?, ?, ?)";
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1, firstName);
            statement.setString(2, lastName);

            if (inputStream != null) {
                // fetches input stream of the upload file for the blob column
                statement.setBlob(3, inputStream);
            }


            int row = statement.executeUpdate();
            if (row > 0) {
                message = "File uploaded and saved into database";
            }
        } catch (SQLException ex) {
            message = "ERROR: " + ex.getMessage();
            ex.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (conn != null) {
                // closes the database connection
                try {
                    conn.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            // sets the message in request scope
            request.setAttribute("Message", message);

            // forwards to the message page
            getServletContext().getRequestDispatcher("/Message.jsp").forward(request, response);
        }
    }
}

我的 .Jsp 类

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>File Upload to Database Demo</title>
</head>
<body>
    <center>
        <h1>File Upload to Database Demo</h1>
        <form method="post" action="uploadServlet" enctype="multipart/form-data">
            <table border="0">
                <tr>
                    <td>Enter First Name: </td>
                    <td><input type="text" name="firstName" size="20"/></td>
                </tr>
                <tr>
                    <td>Enter Last Name: </td>
                    <td><input type="text" name="lastName" size="20"/></td>
                </tr>
                <tr>
                    <td>Portrait Photo: </td>
                    <td><input type="file" name="photo" size="20"/></td>
                </tr>
                <tr>
                    <td colspan="2">
                        <input type="submit" value="Save">
                    </td>
                </tr>
            </table>
        </form>
    </center>
</body>
</html>

显示Jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Message</title>
</head>
<body>
    <center>
        <h3><%=request.getAttribute("Message")%></h3>
    </center>
</body>
</html>

【问题讨论】:

    标签: java mysql jsp servlets


    【解决方案1】:

    404 错误表明表单甚至没有到达 servlet。

    您将表单中的action 属性定义为uploadServlet,这似乎不是您的应用程序的有效URL。

    servlet 的 URL 在 @WebServlet 注释中定义为 FileUploadDBServlet

    因此您可以通过更改表单中的操作或更改 servlet 的 URL 来修复它。

    【讨论】:

    • 谢谢我改变了,但现在我得到了空,这很有帮助。为什么你认为我是空的?
    • 照片 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 967605 image/jpeg 得到了什么。
    • 听起来 JDBC 驱动程序 jar 文件不在您的类路径中。如果您有更多问题,最好发布一个新问题,以便其他人有机会看到它。
    • 不,它只是这个,所以它应该把罐子放在哪里?我在我的网页内容 src 中有它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-30
    • 2021-12-23
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 2012-12-16
    • 2019-08-08
    相关资源
    最近更新 更多