gpt4 book ai didi

java - 使用 Servlet 上传 ServletFileUpload

转载 作者:行者123 更新时间:2023-12-01 13:13:05 25 4
gpt4 key购买 nike

我是 Java/Java Web 新手。目前,我正在学习如何将文件上传到服务器。
我遇到了一个问题,我已经尝试解决这个问题两天了。我在 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 设置有问题,例如 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

最佳答案

您在类路径中缺少commons-fileupload.jar。请将这个 jar 添加到项目的构建路径中,并将 .war 与其一起打包并部署!

注意:这个link将帮助您下载

关于java - 使用 Servlet 上传 ServletFileUpload,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22686460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com