Java 如何使用jsp和servlet将图像上载到数据库,使用多部分/表单数据类型

Java 如何使用jsp和servlet将图像上载到数据库,使用多部分/表单数据类型,java,forms,jsp,servlets,Java,Forms,Jsp,Servlets,我正在处理一个项目,我试图存储jsp表单中的图像,该表单根据需要具有enctype=“multipart/form data”,它调用提交一个用@Multipartconfig注释的servlet。 请提前帮助和感谢 堆栈跟踪是 HTTP Status 500 - org.apache.tomcat.util.http.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a mu

我正在处理一个项目,我试图存储jsp表单中的图像,该表单根据需要具有enctype=“multipart/form data”,它调用提交一个用@Multipartconfig注释的servlet。 请提前帮助和感谢

堆栈跟踪是

  HTTP Status 500 - org.apache.tomcat.util.http.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded

type Exception report

message org.apache.tomcat.util.http.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: org.apache.tomcat.util.http.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded
    org.apache.catalina.connector.Request.parseParts(Request.java:2704)
    org.apache.catalina.connector.Request.getParts(Request.java:2552)
    org.apache.catalina.connector.Request.getPart(Request.java:2728)
    org.apache.catalina.connector.RequestFacade.getPart(RequestFacade.java:1089)
    servlet.account.ProfileProcessServlet.doPost(ProfileProcessServlet.java:56)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

org.apache.tomcat.util.http.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded
    org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:800)
    org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:256)
    org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:280)
    org.apache.catalina.connector.Request.parseParts(Request.java:2640)
    org.apache.catalina.connector.Request.getParts(Request.java:2552)
    org.apache.catalina.connector.Request.getPart(Request.java:2728)
    org.apache.catalina.connector.RequestFacade.getPart(RequestFacade.java:1089)
    servlet.account.ProfileProcessServlet.doPost(ProfileProcessServlet.java:56)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

表单是使用ajax post还是表单提交按钮提交的?@NickVasic否,只是简单的jsp和servletNow我在语句中遇到了问题;它给出了一个java.lang.AbstractMethodError:oracle.jdbc.driver.T4CPreparedStatement.setBinaryStream(ILjava/io/InputStream;)V异常。你能帮忙吗?对不起,我问了…但是我已经解决了我在评论中提到的错误。。我刚刚用ojdbc6.jar替换了我的ojdbc14.jar…因为它使用的是jdbc2.1,但我需要JDBC4。。谢谢。
<form action="ProfileProcessServlet" enctype=" multipart/form-data" method="post">
        <div id="header"> MY ACCOUNT - update profile</div>
            <%
            UserRegBean currentUser = (UserRegBean)(session.getAttribute("currentSessionUser"));
        %>
        Welcome <%= currentUser.getFirstName() + " " + currentUser.getLastName() %>

            <p class="image"><label for="image">Profile Photo</label></p>
            <input id="photo" name="photo" type="file" required />
            <br />
 @WebServlet("/profileServlet")
@MultipartConfig
public class ProfileProcessServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    static Connection currentCon = null;

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

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

            // 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();
            }

            // Connect to Oracle            
            currentCon = ConnectionManager.getConnection();

            // constructs SQL statement
            PreparedStatement statement = null;

            //Block for putting photo into mentorprofilepic database


                String sql = "INSERT INTO mentorprofilepic (username, photo_id, photo) values (?, photo_seq.NEXTVAL, ?)";

                statement = currentCon.prepareStatement(sql);
                statement.setString(1,username);


               statement.setBinaryStream(3, inputStream, (int)filePart.getSize());


             // sends the statement to the database server
                int row = statement.executeUpdate();
                if (row > 0) {
                    System.out.println("File uploaded and saved into student database \n");
                }
                currentCon.commit();
            currentCon.close();
    }
}