如何在JSP文件上激活@MultipartConfig注解?

ehxuflar  于 5个月前  发布在  其他
关注(0)|答案(2)|浏览(60)

所以在我的课上,我被命令上传图像到数据库,并把它们拿出来,并在网站上显示,当使用servlet或php时很简单,但我被要求只使用JSP文件来做这件事。
当我试图运行它时,问题就开始了,服务器要求@multipartconfig注解。我找不到一种方法将其添加到jsp代码中。

这是jsp:

<%@page import="javax.servlet.annotation.MultipartConfig"%>
<%@page import="java.sql.*"%>
<%@page import="java.io.InputStream"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%
    request.setCharacterEncoding("UTF-8");
    Part p = request.getPart("image");
    InputStream inputStream = null;
    if (p != null) {
        inputStream = p.getInputStream();
    }

    String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost:3306/test42";
    Class.forName(driver);
    Connection con = DriverManager.getConnection(url, "root", "1234");

    String sqlString = "INSERT INTO test42.images items(idimages) values(" + inputStream + ");";

    String msg = p.toString();

%>

字符串

上传表单如下:

<form method="post" action="mainPage.jsp" enctype="multipart/form-data">
                choose file :
                <input type="file" name="image" />

                <input type="submit" value="submit">
            </form>


这是来自服务器的消息:

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


我试着将它添加到web.xml中,但它不起作用....应用程序只是崩溃了一个构建错误;我从这里得到了这个解决方案:http://docs.oracle.com/javaee/6/tutorial/doc/gmhal.html

像这样:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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">

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

kmpatx3s

kmpatx3s1#

<multipart-config>标记需要放在<servlet>标记内部,并且您需要在<servlet>标记旁边沿着一个<servlet-mapping>

<servlet>   
    <servlet-name>uploadfile</servlet-name>
    <jsp-file>/mainPage.jsp</jsp-file>
    <multipart-config>
        <location>/temp</location>
        <max-file-size>20848820</max-file-size>
        <max-request-size>418018841</max-request-size>
        <file-size-threshold>1048576</file-size-threshold>
    </multipart-config>
</servlet>
<servlet-mapping>
    <servlet-name>uploadfile</servlet-name>
    <url-pattern>/mainPage.jsp</url-pattern>
</servlet-mapping>

字符串

58wvjzkj

58wvjzkj2#

实际上,每个jsp文件都将被转换为servlet,因为tomcat只能处理servlet,因此每个jsp文件都间接地是一个servlet,并具有它的所有功能

相关问题