jsp代码上传jgp文件并保存在mysql db

时间:2015-12-19 11:42:15

标签: jsp-tags

这是我的servlet代码

package classes;

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;

@WebServlet("/FileUploadDBServlet")
@MultipartConfig(maxFileSize = 16177215)    // upload file's size up to 16MB
public class FileUploadDBServlet extends HttpServlet {

    // database connection settings
    private String dbURL = "jdbc:mysql://localhost:3306/studreg";
    private String dbUser = "root";
    private String dbPass = "";

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // gets values of text fields
        String matric = request.getParameter("matric");
        //String lastName = request.getParameter("lastName");

        InputStream inputStream = null; // input stream of the upload file

        // obtains the upload file part in this multipart request
        Part filePart = request.getPart("photo");

        if (filePart != null) {
            // prints out some information for debugging
            System.out.println(filePart.getName());
            System.out.println(filePart.getSize());
            System.out.println(filePart.getContentType());

            // obtains input stream of the upload file
            inputStream = filePart.getInputStream();
        }

        Connection conn = null; // connection to the database
        String message = null;  // message will be sent back to client

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

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

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

            // sends the statement to the database server
            int row = statement.executeUpdate();
            if (row > 0) {
                message = "File uploaded and saved for transcript processing";
            }
        } catch (SQLException ex) {
            message = "ERROR: " + ex.getMessage();
            ex.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 contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<%@ page import ="java.sql.*" %>
<%@page import="classes.DbConn2"%>
<%@page import="classes.FileUploadDBServlet"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <form method ="post" action ="receipt.jsp" name="form1">
                  <p>
                    <input type="hidden" name="matric" value="<%=matric %>">
             <td colspan="2"> <input name="invoiceno" type="hidden" value="<%=invoiceno%>" />
                    <input type="submit" value="Print Payment Receipt">
                                </p>
                  <p>&nbsp;  </p>
                </form>
        <left>
                <p>Click here to print <a target="_blank "href="http://www.collegesch.com/reg/receipt.jsp?matric=<%=matric%>&invoiceno=<%=invoiceno%>">Receipt</a>
            <h1 align="left">File Upload for Transcript Request</h1>
            <form method="post" action="FileUploadDBServlet" name="form2" enctype="multipart/form-data">
                <table width="759" border="0">
                     <td colspan="2"> <input name="matric" type="hidden" value="<%=matric%>" />                  
                          <tr>
                        <td><p>Picture( jpeg, 450X500): </p>
                          </td>
                        <td><input type="file" name="photo" size="10"/></td>
                <td><p>Result( jpeg, 450X500): </p>
                          </td>
                        <td><input type="file" name="photo1" size="10"/></td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <input type="submit" value="Save">
                        </td>
                    </tr>
                </table>
                <p>&nbsp;</p>
            </form>
        </center>
         <br /> </td>
        <td>&nbsp;</td>
        </tr>

        <tr>
        <td width="4">&nbsp;<br />
        &nbsp;<br />    </td>
        <td width="35">&nbsp;</td>
        <td width="505">&nbsp;</td>
        <td width="547">&nbsp;</td>
      </tr>
    </table>
    </body>
    </html>

但是在上传文件后,我收到了这个错误:

The requested URL /reg/FileUploadDBServlet was not found on this server.

此外,尝试使用404 Not Found处理请求时遇到ErrorDocument错误。

1 个答案:

答案 0 :(得分:0)

更改

  action="FileUploadDBServlet" 

  action="<%= request.getContextPath () %>/FileUploadDBServlet"

再试一次 - 在路径下可能找不到相对路径。