HttpServletRequest 类型未定义方法 getPart(String)
The method getPart(String) is undefined for the type HttpServletRequest
我一直致力于 Java 网络项目,我需要通过 JSP 页面上传图像并将其存储到 MySQL 数据库中。
我正在输入可用于 Servlet 3.0 或更高版本的多部分图像。
我一直在使用 getPart("image") 逐个获取图像,但它向我显示以下错误:
"The method getPart(String) is undefined for the type HttpServletRequest"
- 我正在使用的服务器:Tomcat 7
- 我正在使用的IDE:Eclipse Luna
代码片段如下:
ImageUploadServlet:
package com.nagarro.imagemanagementutility.controller;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import com.nagarro.imagemanagementutility.util.Constants;
/**
* Servlet implementation class ImageUploadServlet
*/
@MultipartConfig(maxFileSize=Constants.MAX_FILE_SIZE) // Upload image file size upto 16 MB
public class ImageUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ImageUploadServlet() {
super();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
InputStream inputStream = null;
// obtains the upload image file in this multipart request
Part imagePart = request.getPart("photo");
}
}
UserImageUtility.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Image Management Utility</title>
</head>
<body>
<table border="1" align="center" width="900px">
<tr>
<td>
<div align="center">
<h3>Image Management Utility</h3>
</div>
</td>
</tr>
<tr>
<td>
<div>Please select an image file to upload (Max Size 1 MB)</div>
<form action="ImageUploadServlet" method="post"
enctype="multipart/form-data">
<input type="file" name="image" size="50" />
<div align="right">
<input type="submit" name="sbtnSubmit" value="Submit" />
<input type="button" name="btnCancel" value="Cancel" />
</div>
</form>
</td>
</tr>
</table>
</body>
</html>
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.nagarro</groupId>
<artifactId>Assignment_4_ImageManagementUtility</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Assignment_4_ImageManagementUtility Maven Webapp</name>
<url>http://maven.apache.org</url>
<build>
<finalName>Assignment_4_ImageManagementUtility</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.nagarro.imagemanagementutility.controller.ApplicationLauncher</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
<finalName></finalName>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.8.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.8.Final</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
</dependency>
<!-- Hibernate uses slf4j for logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
</project>
lib中的jar列表:
方法
Part getPart(java.lang.String name) throws java.io.IOException, ServletException
在 HttpServletRequest Java EE 6, but not in HttpServletRequest Java EE 5 可用。
确保您使用的是 Java EE 6。
您的 class 路径上有多个 Servlet API jar。 JSTL 太老了,在你的 pom.xml 中有一堆 JSTL 工件拉入旧的 servlet 版本。从您的 pom.xml 中删除 javax.servlet:jstl
、jstl:jstl
和 javax.servlet.jsp.jstl:jstl-api
,然后试试这个:
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jstl-impl</artifactId>
<version>1.2</version>
<exclusions>
<exclusion>
<groupId>java.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
</exclusion>
</exclusions>
</dependency>
P.S。 Eclipse 将 Tomcat 的 jars 添加到 class 路径,因此您可以根据需要从 pom.xml 中删除 java.servlet:servlet-api
。如果你保留它,添加<scope>provided</scope>
或Tomcat会抱怨。提供 servlet classes 是 servlet 容器的责任,所以你不应该将它们包含在你的 WAR 中,尽管 Tomcat 足够聪明以防止它成为问题。
我一直致力于 Java 网络项目,我需要通过 JSP 页面上传图像并将其存储到 MySQL 数据库中。 我正在输入可用于 Servlet 3.0 或更高版本的多部分图像。
我一直在使用 getPart("image") 逐个获取图像,但它向我显示以下错误: "The method getPart(String) is undefined for the type HttpServletRequest"
- 我正在使用的服务器:Tomcat 7
- 我正在使用的IDE:Eclipse Luna
代码片段如下:
ImageUploadServlet:
package com.nagarro.imagemanagementutility.controller;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import com.nagarro.imagemanagementutility.util.Constants;
/**
* Servlet implementation class ImageUploadServlet
*/
@MultipartConfig(maxFileSize=Constants.MAX_FILE_SIZE) // Upload image file size upto 16 MB
public class ImageUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ImageUploadServlet() {
super();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
InputStream inputStream = null;
// obtains the upload image file in this multipart request
Part imagePart = request.getPart("photo");
}
}
UserImageUtility.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Image Management Utility</title>
</head>
<body>
<table border="1" align="center" width="900px">
<tr>
<td>
<div align="center">
<h3>Image Management Utility</h3>
</div>
</td>
</tr>
<tr>
<td>
<div>Please select an image file to upload (Max Size 1 MB)</div>
<form action="ImageUploadServlet" method="post"
enctype="multipart/form-data">
<input type="file" name="image" size="50" />
<div align="right">
<input type="submit" name="sbtnSubmit" value="Submit" />
<input type="button" name="btnCancel" value="Cancel" />
</div>
</form>
</td>
</tr>
</table>
</body>
</html>
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.nagarro</groupId>
<artifactId>Assignment_4_ImageManagementUtility</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Assignment_4_ImageManagementUtility Maven Webapp</name>
<url>http://maven.apache.org</url>
<build>
<finalName>Assignment_4_ImageManagementUtility</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.nagarro.imagemanagementutility.controller.ApplicationLauncher</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
<finalName></finalName>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.8.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.8.Final</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
</dependency>
<!-- Hibernate uses slf4j for logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>
</project>
lib中的jar列表:
方法
Part getPart(java.lang.String name) throws java.io.IOException, ServletException
在 HttpServletRequest Java EE 6, but not in HttpServletRequest Java EE 5 可用。
确保您使用的是 Java EE 6。
您的 class 路径上有多个 Servlet API jar。 JSTL 太老了,在你的 pom.xml 中有一堆 JSTL 工件拉入旧的 servlet 版本。从您的 pom.xml 中删除 javax.servlet:jstl
、jstl:jstl
和 javax.servlet.jsp.jstl:jstl-api
,然后试试这个:
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jstl-impl</artifactId>
<version>1.2</version>
<exclusions>
<exclusion>
<groupId>java.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
</exclusion>
</exclusions>
</dependency>
P.S。 Eclipse 将 Tomcat 的 jars 添加到 class 路径,因此您可以根据需要从 pom.xml 中删除 java.servlet:servlet-api
。如果你保留它,添加<scope>provided</scope>
或Tomcat会抱怨。提供 servlet classes 是 servlet 容器的责任,所以你不应该将它们包含在你的 WAR 中,尽管 Tomcat 足够聪明以防止它成为问题。