这是我的上传表单:
form id='picform' action='UploadImage' method='post' enctype='multipart/form-data'>
<input type='file' name='Photo' />
<input type='submit' />
</form>
和我的UploadServlet:
import java.io.*;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
@WebServlet("/uploadServlet")
@MultipartConfig(maxFileSize = 16177215)
public class UploadImage extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
InputStream inputStream = null;
Part filePart = request.getPart("Photo");
if (filePart != null) {
System.out.println(filePart.getName());
System.out.println(filePart.getSize());
System.out.println(filePart.getContentType());
inputStream = filePart.getInputStream();
}
String message = null;
try{
//loading drivers for mysql
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection
("jdbc:mysql://195.243.249.131:3306/****","****","****");
PreparedStatement ps=con.prepareStatement
("update users set Photo=? where email=?");
HttpSession session=request.getSession(false);
String email=(String)session.getAttribute("email");
ps.setString(1, email);
int i=ps.executeUpdate();
if (inputStream != null) {
// fetches input stream of the upload file for the blob column
ps.setBlob(3, inputStream);
}
int row = ps.executeUpdate();
if (row > 0) {
message = "Image is uploaded successfully into the Database";
}
}
catch(Exception se)
{
se.printStackTrace();
}
}
}
但我在尝试上传文件时遇到此异常:
java.lang.AbstractMethodError: org.apache.catalina.connector.RequestFacade.getPart(Ljava /郎/字符串;)Ljavax / servlet的/ HTTP /部件; UploadImage.doPost(UploadImage.java:21) javax.servlet.http.HttpServlet.service(HttpServlet.java:644) javax.servlet.http.HttpServlet.service(HttpServlet.java:725)