编写好Java类,该类依赖2个jar包:commons-fileupload-1.3.1.jar和commons-io-2.4.jar
使用javac编译该类文件,碰到下面的错误:
C:\tools\Tomcat 8.0\webapps\servlet>javac UploadServlet.java
UploadServlet.java:11: error: package org.apache.commons.fileupload does not exist
import org.apache.commons.fileupload.FileItem;
UploadServlet.java:12: error: package org.apache.commons.fileupload does not exist
import org.apache.commons.fileupload.FileUploadException;
UploadServlet.java:13: error: package org.apache.commons.fileupload.disk does not exist
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
UploadServlet.java:14: error: package org.apache.commons.fileupload.servlet does not exist
import org.apache.commons.fileupload.servlet.ServletFileUpload;
UploadServlet.java:15: error: package org.apache.commons.io.output does not exist
import org.apache.commons.io.output.*;
UploadServlet.java:34: error: cannot find symbol isMultipart = ServletFileUpload.isMultipartContent(request);
symbol: variable ServletFileUpload
location: class UploadServlet
UploadServlet.java:48: error: cannot find symbol
DiskFileItemFactory factory = new DiskFileItemFactory();
symbol: class DiskFileItemFactory
location: class UploadServlet
UploadServlet.java:48: error: cannot find symbol
DiskFileItemFactory factory = new DiskFileItemFactory();
symbol: class DiskFileItemFactory
location: class UploadServlet
UploadServlet.java:55: error: cannot find symbol
ServletFileUpload upload = new ServletFileUpload(factory);
symbol: class ServletFileUpload
location: class UploadServlet
UploadServlet.java:55: error: cannot find symbol
ServletFileUpload upload = new ServletFileUpload(factory);
symbol: class ServletFileUpload
location: class UploadServlet
UploadServlet.java:73: error: cannot find symbol
FileItem fi = (FileItem)i.next();
symbol: class FileItem
location: class UploadServlet
UploadServlet.java:73: error: cannot find symbol
FileItem fi = (FileItem)i.next();
symbol: class FileItem
location: class UploadServlet
12 errors
解决方法:将这两个引用的jar文件添加到classpath环境变量即可。