我一直在做一个使用jsp,servlets和tomcat上传文件的项目。
但是,我收到了以下错误:
type Exception report
message Servlet execution threw an exception
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Servlet execution threw an exception
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.NoClassDefFoundError: database/Db_Connection
controller.UploadFileController.doPost(UploadFileController.java:52)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.24 logs.
我的servlet代码是:
package controller;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
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.http.HttpServletResponse;
import javax.servlet.http.Part;
import database.Db_Connection;
import java.io.PrintWriter;
import java.util.Random;
import javax.servlet.RequestDispatcher;
@WebServlet("/uploadServlet")
@MultipartConfig(maxFileSize = 16177215)
public class UploadFileController extends HttpServlet
{
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
InputStream inputStream = null;
Random rand = new Random();
int n = rand.nextInt(9999) + 1;
String idTemp=(String.valueOf(n));
String title=(request.getParameter("title"));
Part filePart = request.getPart("file_uploaded");
if (filePart != null)
{
System.out.println(filePart.getName());
System.out.println(filePart.getSize());
System.out.println(filePart.getContentType());
inputStream = filePart.getInputStream();
}
try
{
Db_Connection dbconn=new Db_Connection();
Connection conn= dbconn.Connection();
String sql = "INSERT INTO files (id, title, file1) values (?, ?, ?)";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, idTemp);
statement.setString(2, title);
if (inputStream != null)
{
statement.setBinaryStream(3, inputStream, (int) filePart.getSize());
}
int row = statement.executeUpdate();
if (row > 0)
{
out.println("File uploaded!!!");
conn.close();
RequestDispatcher rs = request.getRequestDispatcher("upload_form.jsp");
rs.include(request, response);
}
else
{
out.println("Couldn't upload your file!!!");
conn.close();
RequestDispatcher rs = request.getRequestDispatcher("upload_form.jsp");
rs.include(request, response);
}
}catch(Exception e){e.printStackTrace();}
}
}
我们使用以下
在oracle 10g中创建了一个数据库 create table files(id number(10), title varchar(20),file1 varchar(20));
我们尝试上传的文件大小为5个字节。我也完成了数据库连接。你能帮我解决这个错误。
答案 0 :(得分:1)
在类路径
中包含包含database/Db_Connection
的jar文件
答案 1 :(得分:1)
“Servlet执行引发异常”是在出现异常时提供的一般错误消息,在这种情况下,我们需要查看识别异常的根本原因。在您的情况下,类路径中缺少DB_Connection类。
java.lang.NoClassDefFoundError: database/Db_Connection
controller.UploadFileController.doPost(UploadFileController.java:52)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
答案 2 :(得分:0)
您正在使用oracle数据库,是否包含了用于oracle连接的jar。尝试将其添加到我们的类路径。希望这能解决问题。