使用Servlet ServletFileUpload上传

时间:2014-03-27 11:37:44

标签: java file-upload servlet-3.0

我是Java / Java Web的新手。目前,我正在学习如何将文件上传到服务器。
我正面临一个问题,我一直试图解决这个问题2天。我一直在stackoverflow和其他资源中搜索很多而没有任何结果。

这是我正在学习的资源; http://www.tutorialspoint.com/servlets/servlets-file-uploading.htm

我确信本教程使用的是来自'c:\'的Windows,而我使用的是Ubuntu。

这是我的代码:

<web-app>
....
<context-param> 
    <description>Location to store uploaded file</description> 
    <param-name>file-upload</param-name> 
    <param-value>
         /home/someone/test_upload/
     </param-value> 
</context-param>
....
</web-app>


我写了一个index.html包含表单上传。一切都很好,表格在浏览器上显示。

这是我的servlet (导入正常;我在构建路径中添加了commons-fileupload-1.3.1.jar)

public class UploadServlet extends HttpServlet {

   private boolean isMultipart;
   private String filePath;
   private int maxFileSize = 50 * 1024;
   private int maxMemSize = 4 * 1024;
   private File file ;

   public void init( ){
      // Get the file location where it would be stored.
      filePath = 
             getServletContext().getInitParameter("file-upload"); 
   }

   public void doPost(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, java.io.IOException {

      // Check that we have a file upload request
      isMultipart = ServletFileUpload.isMultipartContent(request);
      response.setContentType("text/html");
      java.io.PrintWriter out = response.getWriter( );

      DiskFileItemFactory factory = new DiskFileItemFactory();
      // maximum size that will be stored in memory
      factory.setSizeThreshold(maxMemSize);
      // Location to save data that is larger than maxMemSize.
      factory.setRepository(new File("\\resources\\"));

      // Create a new file upload handler
      ServletFileUpload upload = new ServletFileUpload(factory); //when this line is active, everything is messed up
      // maximum file size to be uploaded.
      //upload.setSizeMax( maxFileSize );


      [...] 

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().print("the test");
    }

我尽力调试servlet。我从一行到另一行写,直到我在行上发现错误

ServletFileUpload upload = new ServletFileUpload(factory);

这是错误页面:

HTTP Status 404 - /testupload/

type Status report

message /testupload/

description The requested resource is not available.

VMware vFabric tc Runtime 2.9.3.RELEASE/7.0.42.A.RELEASE

为了便于分析,我认为我的工厂设置有问题,例如 factory.setSizeThreshold(maxMemSize); factory.setRepository(new文件( “\\资源\\”));

也许我的分析也是错的。请考虑设置,因为我使用的是Ubuntu。

你能帮我解决一下这个问题吗?

----------------------------------我的eclipse控制台---------- ----------------------

Mar 27, 2014 6:40:33 PM com.springsource.tcserver.security.PropertyDecoder <init>
INFO: tc Runtime property decoder using memory-based key
Mar 27, 2014 6:40:33 PM com.springsource.tcserver.security.PropertyDecoder <init>
INFO: tcServer Runtime property decoder has been initialized in 342 ms
Mar 27, 2014 6:40:34 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Mar 27, 2014 6:40:34 PM com.springsource.tcserver.serviceability.rmi.JmxSocketListener init
INFO: Started up JMX registry on 127.0.0.1:6969 in 145 ms
Mar 27, 2014 6:40:34 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1213 ms
Mar 27, 2014 6:40:34 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Mar 27, 2014 6:40:34 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: VMware vFabric tc Runtime 2.9.3.RELEASE/7.0.42.A.RELEASE
Mar 27, 2014 6:40:34 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor /home/someone/Documents/springsource/vfabric-tc-server-developer-2.9.3.RELEASE/base-instance/conf/Catalina/localhost/testupload.xml
Mar 27, 2014 6:40:34 PM org.apache.catalina.startup.SetContextPropertiesRule begin
WARNING: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:test_upload' did not find a matching property.
Mar 27, 2014 6:40:35 PM org.apache.catalina.core.ContainerBase addChildInternal
SEVERE: ContainerBase.addChild: start: 
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/testupload]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:656)
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1635)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory
    at java.lang.Class.getDeclaredFields0(Native Method)
    at java.lang.Class.privateGetDeclaredFields(Class.java:2397)
    at java.lang.Class.getDeclaredFields(Class.java:1806)
    at org.apache.catalina.util.Introspection.getDeclaredFields(Introspection.java:106)
    at org.apache.catalina.startup.WebAnnotationSet.loadFieldsAnnotation(WebAnnotationSet.java:263)
    at org.apache.catalina.startup.WebAnnotationSet.loadApplicationServletAnnotations(WebAnnotationSet.java:142)
    at org.apache.catalina.startup.WebAnnotationSet.loadApplicationAnnotations(WebAnnotationSet.java:67)
    at org.apache.catalina.startup.ContextConfig.applicationAnnotationsConfig(ContextConfig.java:405)
    at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:881)
    at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:376)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5322)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 10 more
Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    ... 24 more

Mar 27, 2014 6:40:35 PM org.apache.catalina.startup.HostConfig deployDescriptor
SEVERE: Error deploying configuration descriptor /home/someone/Documents/springsource/vfabric-tc-server-developer-2.9.3.RELEASE/base-instance/conf/Catalina/localhost/testupload.xml
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/testupload]]
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:904)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:656)
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1635)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

Mar 27, 2014 6:40:35 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /home/someone/Documents/springsource/vfabric-tc-server-developer-2.9.3.RELEASE/base-instance/webapps/manager
Mar 27, 2014 6:40:36 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /home/someone/Documents/springsource/vfabric-tc-server-developer-2.9.3.RELEASE/base-instance/webapps/ROOT
Mar 27, 2014 6:40:36 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Mar 27, 2014 6:40:36 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1526 ms

2 个答案:

答案 0 :(得分:1)

您错过了类路径中的commons-fileupload.jar。请将此jar添加到项目的构建路径中,并将.war与其一起打包并部署!

注意:此link可帮助您下载

答案 1 :(得分:0)

你缺少org.apache.commons.fileupload.FileItemFactory,所以缺少mons-fileupload

您需要将其添加到类路径中。