我正在尝试在Tomcat 7中运行一个基于org.apache.commons.fileupload的servlet文件上传示例
我使用此命令在CMD(Win 7 32位)中编译了servlet类文件。
javac -classpath "c:\program files\apache software foundation\tomcat\7.0\lib\servlet-api.jar";"c:\program files\apache software foundation\tomcat 7.0\lib\commons-fileupload-1.3.jar";"C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\commons-io-2.4.jar"; regservlet.java
在cmd工作时,一切都很好。
编译后,我尝试了我的例子,我得到了一个错误
HTTP状态500 - Servlet执行引发异常
输入例外报告
消息Servlet执行引发异常
说明服务器遇到内部错误,导致无法完成此请求。
例外
javax.servlet.ServletException:Servlet执行引发异常根本原因
java.lang.NoClassDefFoundError:javax / servlet / http / HttpServletRequest
org.apache.commons.fileupload.servlet.ServletFileUpload.isMultipartContent(ServletFileUpload.java:68) regservlet.doPost(regservlet.java:40) javax.servlet.http.HttpServlet.service(HttpServlet.java:647) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) note备注根本原因的完整堆栈跟踪可在Apache Tomcat /7.0.42日志中。
然后最后谷歌搜索,找到了一些解决方案,我一个接一个地跟着他们。
在WEB-INF中创建了一个lib文件夹,并复制了commons-fileupload-1.2.2和commons-io-2.2。但没有运气。
在tomcat / lib中复制了commons-fileupload-1.2.2和commons-io-2.2。但没有运气。
将commons-fileupload-1.2.2和commons-io-2.2添加到classpaath但没有运气。
怎么了?
我的代码
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.output.*;
import org.apache.commons.fileupload.*;
import java.sql.*;
public class regservlet extends HttpServlet
{
String fnames,mnames,lnames,genders,father,cadd1,cadd2,cadd3,ccity,cstate,cpin,day,month,year,padd1,padd2,padd3,pcity,pstate,ppin,mobile,email,cemail;
Connection con=null;
PreparedStatement pstat=null;
FileItem file;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{ response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try{
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload sfu = new ServletFileUpload(factory);
if (!ServletFileUpload.isMultipartContent(request))
{ System.out.println("sorry. No file uploaded");
return;}
List items=sfu.parseRequest(request);
FileItem fname=(FileItem) items.get(0);
fnames=fname.getString();
FileItem mname=(FileItem) items.get(1);
mnames=mname.getString();
FileItem lname=(FileItem) items.get(2);
lnames=lname.getString();
FileItem gender=(FileItem) items.get(3);
genders=gender.getString();
FileItem days=(FileItem) items.get(4);
day=days.getString();
FileItem months=(FileItem) items.get(5);
month=months.getString();
FileItem years=(FileItem) items.get(6);
year=years.getString();
FileItem fath=(FileItem) items.get(7);
father=fath.getString();
FileItem coradd1=(FileItem) items.get(8);
cadd1=coradd1.getString();
FileItem coradd2=(FileItem) items.get(9);
cadd2=coradd2.getString();
FileItem coradd3=(FileItem) items.get(10);
cadd3=coradd3.getString();
FileItem corcity=(FileItem) items.get(11);
ccity=corcity.getString();
FileItem corstate=(FileItem) items.get(12);
cstate=corstate.getString();
FileItem corpin=(FileItem) items.get(13);
cpin=corpin.getString();
FileItem peradd1=(FileItem) items.get(14);
padd1=peradd1.getString();
FileItem peradd2=(FileItem) items.get(15);
padd2=peradd2.getString();
FileItem peradd3=(FileItem) items.get(16);
padd3=peradd3.getString();
FileItem percity=(FileItem) items.get(17);
pcity=percity.getString();
FileItem perstate=(FileItem) items.get(18);
pstate=perstate.getString();
FileItem perpin=(FileItem) items.get(19);
ppin=perpin.getString();
FileItem mob=(FileItem) items.get(20);
mobile=mob.getString();
FileItem emails=(FileItem) items.get(21);
email=emails.getString();
FileItem cemails=(FileItem) items.get(22);
cemail=cemails.getString();
FileItem photo=(FileItem) items.get(23);
FileItem sign=(FileItem) items.get(24);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:reg");
con.setAutoCommit(false);
pstat=con.prepareStatement("insert into register values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
pstat.clearParameters();
pstat.setString(1,fnames);
pstat.setString(2,mnames);
pstat.setString(3,lnames);
pstat.setString(4,genders);
pstat.setString(5,day);
pstat.setString(6,month);
pstat.setString(7,year);
pstat.setString(8,father);
pstat.setString(9,cadd1);
pstat.setString(10,cadd2);
pstat.setString(11,cadd3);
pstat.setString(12,ccity);
pstat.setString(13,cstate);
pstat.setString(14,cpin);
pstat.setString(15,padd1);
pstat.setString(16,padd2);
pstat.setString(17,padd3);
pstat.setString(18,pcity);
pstat.setString(19,pstate);
pstat.setString(20,ppin);
pstat.setString(21,mobile);
pstat.setString(22,email);
pstat.setString(23,cemail);
pstat.setBinaryStream(3, file.getInputStream(), (int) file.getSize());
int i=pstat.executeUpdate();
con.commit();
con.close();
}catch(Exception e)
{
out.println( "Error --> " + e.getMessage());
}
}
public String getServletInfo() {
return "Short description";
}
}
我有一份注册表格,我在其中输入用户的所有详细信息
我几乎尝试过所有事情,但无法找到问题的解决方案。