如何使用multipart / form-data enctype使用jsp和servlet将图像上传到数据库

时间:2015-02-15 12:24:16

标签: 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 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)

jsp表单的代码是

<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 />

Servlet代码是

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

0 个答案:

没有答案