J2ee image uploading error: java.lang.IllegalStateException

J2ee image uploading error: java.lang.IllegalStateException

我正在尝试通过 jsp 表单将图像上传到数据库。但是有一个错误,

java.lang.IllegalStateException: Request.getPart is called without multipart configuration. Either add a @MultipartConfig to the servlet, or a multipart-config element to web.xml

我的servlet代码;

/* image */
            Part filePart = request.getPart("eImage"); //here is the error
            InputStream inputStream = null;

            if (filePart != null) {
                System.out.println(filePart.getName());
                System.out.println(filePart.getSize());
                System.out.println(filePart.getContentType());

                inputStream = filePart.getInputStream();
            }

但是当我添加时,

<multipart-config>
    <location>/tmp</location>
    <max-file-size>20848820</max-file-size>
    <max-request-size>418018841</max-request-size>
    <file-size-threshold>1048576</file-size-threshold>
</multipart-config>

作为子元素,根据https://docs.oracle.com/javaee/7/tutorial/servlets011.htm,但是也出现错误as;

Severe: Exception while deploying the app [events_handeling] : org.xml.sax.SAXParseException; lineNumber: 12; columnNumber: 23; Deployment descriptor file WEB-INF/web.xml in archive [web]. cvc-complex-type.2.4.a: Invalid content was found starting with element 'multipart-config'.

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <multipart-config>
        <location>/tmp</location>
        <max-file-size>20848820</max-file-size>
        <max-request-size>418018841</max-request-size>
        <file-size-threshold>1048576</file-size-threshold>
    </multipart-config>
</web-app>

这应该如何解决?

在标签内的JSP或HTML页面中写入(enctype=”multipart/form-data”)。

<form name="form1" method="post" enctype="multipart/form-data" action="insertimage.jsp">
<input type="file" name="ImageFile" id="ImageFile" />
<input type="submit" name="submit" value="submit" />
</form>

Java 从请求中读取文件的函数

   try {
            String ImageFile="", itemName = "";
            boolean isMultipart = ServletFileUpload.isMultipartContent(request);
            if (isMultipart) {
                FileItemFactory factory = new DiskFileItemFactory();
                ServletFileUpload upload = new ServletFileUpload(factory);
                List items = null;
                try {
                    items = upload.parseRequest(request);
                }
                catch (FileUploadException e) { 
                    e.getMessage();
                }

                Iterator itr = items.iterator();
                while (itr.hasNext()) {
                    FileItem item = (FileItem) itr.next();
                    if (item.isFormField()) {
                        String name = item.getFieldName();
                        String value = item.getString();
                        if(name.equals("ImageFile")) {
                            ImageFile=value;
                        }
                    }
                    else {
                        try {
                            itemName = item.getName();  
                            File savedFile = new File("config.getServletContext().getRealPath("/")+"Example\image-folder\"+itemName);
                            item.write(savedFile);
                        }
                        catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        catch (Exception e) {
            out.println(e.getMessage());
        }

从该代码读取文件并保存到数据库中。