当前位置: 首页 > 知识库问答 >
问题:

Spring MVC:尝试上载csv文件时出现HTTP 400错误

冀耀
2023-03-14

问题:我正在按照本指南通过Spring MVC上传文件:https://Spring.io/guides/gs/uploading-files/。但是,当我这样做时,当我通过.jsp页面上的表单上传一个文件时,我总是会收到以下错误:

HTTP状态400-所需的字符串参数“name”不存在

类型状态报告

message Required字符串参数“name”不存在

描述客户端发送的请求语法不正确。

Apache Tomcat/7.0.52

问题:如何解决400错误并成功上传文件?

方法:通过遵循指南,我创建了一个控制器类TestController和一个名为handleFileUpload的方法,这是处理用户上载文件的方法。我没有使用multipartResolver bean,因为它会给我带来与Apache Commons相关的各种错误,我在这里问了一个问题:https://stackoverflow.com/questions/35467250/uncall-to-upload-a-file-using-spring-apache-commons-issue

代码

testController.java

    @Controller
    public class TestController {

       @RequestMapping(value="/upload", method=RequestMethod.POST)
       public @ResponseBody String handleFileUpload(@RequestParam("name") String name, @RequestParam("file") MultipartFile file) {
        if (!file.isEmpty()) {
            try {
                byte[] bytes = file.getBytes();
                BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name)));
                stream.write(bytes);
                stream.close();
                return "You successfully uploaded " + name + "!";
            } catch (Exception e) {
                return "You failed to upload " + name + " => " + e.getMessage();
            }
        } else {
            return "You failed to upload " + name + " because the file was empty.";
        }
      }


    }

下面是index.jsp中的表单。请记住,我正在jsp页面中使用TWBS,我不确定这是否会对此产生任何巨大的差异。

<form method="POST" enctype="multipart/form-data" action="/upload">
    File to upload: <input type="file" name="file"><br/>
    Name: <input type="text" name="name"><br/><br/>
    <input type = "submit" value = "Upload"> Press here to upload the file!
</form>
HTTP Status 500 - Servlet.init() for servlet mvc-dispatcher threw exception

type Exception report

message Servlet.init() for servlet mvc-dispatcher threw exception

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet.init() for servlet mvc-dispatcher threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2441)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2430)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    java.lang.Thread.run(Thread.java:745)
root cause

java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory
    java.lang.Class.getDeclaredMethods0(Native Method)
    java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    java.lang.Class.getDeclaredMethods(Class.java:1975)
    org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:609)
    org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:521)
    org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:507)
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:241)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1069)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1042)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:838)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
    org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:667)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:633)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:681)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:552)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:493)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2441)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2430)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    java.lang.Thread.run(Thread.java:745)
root cause

java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1718)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1569)
    java.lang.Class.getDeclaredMethods0(Native Method)
    java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    java.lang.Class.getDeclaredMethods(Class.java:1975)
    org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:609)
    org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:521)
    org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:507)
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:241)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1069)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1042)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:838)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
    org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:667)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:633)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:681)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:552)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:493)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2441)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2430)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    java.lang.Thread.run(Thread.java:745)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.52 logs.

Apache Tomcat/7.0.52

关于我收到上述错误的原因,需要添加的注意事项:每当我将以下bean添加到mvc-dispatcher-servlet.xml文件时,当我尝试上载文件时,就会出现上述错误:

共有1个答案

孙洋
2023-03-14

确保在应用程序的类路径中包含所有必需的.jar文件。

这个错误听起来像是在运行时找不到jar文件

换句话说,所有必需的外部库(用于开发、编译)也需要在运行时包含在类路径中。

在IntelliJ中,您可以通过进入'Project Structure>Artifacts'面板来检查工件中包含了什么。

我不是Eclipse用户,但也有验证的方法。

 类似资料:
  • 我正在尝试上载一个csv文件到服务器。下面是我的HTML代码: 还有我的JS:- 我面临以下错误:- 加载资源失败:服务器响应的状态为500(HTTP/1.1 500) 可能未经处理的拒绝:{“data”:{“timestamp”:1510643953084,“status”:500,“error”:“内部服务器错误”,“exception”:“java.lang.NullPointerExcep

  • 我试图将一个目录中的几个csv文件读入pandas,并将它们连接到一个大数据帧中,但出现以下错误: 这是我的密码 我不知道为什么它不起作用。我试图用chmod解决这个问题,但一切都变了

  • 我正在尝试使用GetPreSignedUrlRequest方法访问已上载到AmazonS3的文件。我使用的代码如下所示:- 由此生成的url然后用于指向文件。看起来像http://s3.amazonaws.com/mybucketname/VZcbKsZgR2qyOMkLU1XT_jquery_ui_touch-punch_min_js.txt?X-Amz-Expires=300 但这给了我签名不

  • 我正在尝试将文件上传到 Azure Blob 存储,但在将文件推送到存储中时收到错误。 我使用java 11和Quarkus进行开发。在POM上,我添加了工件azure-storage-blob和azure-sdk-bom 法典: 恢复错误 io.net.cha.DefaultChannelPipeline] (vert.x-eventloop-thread-2) 一个 exceptionCaug

  • 我试图使用以下命令将第三方jar部署到sonatype nexus存储库: 我得到以下错误: [error]在当前项目和插件组[org.apache.maven.plugins,org.codehaus.mojo]中找不到前缀“–Durl=http”的插件,这些插件组可从中央存储库[local(/Users/ssurendran/.m2/repository)获得(https://repo.mav

  • 问题内容: 我在Bower注册我的bower.json文件时遇到问题。任何帮助将不胜感激。 当我尝试使用Bower注册我的项目时 我收到错误消息: 我的bower.json的内容很简单(为了解决问题,我删除了空格和换行符): (请注意,该文件中没有?) 这是我运行Bower Register时屏幕上显示的详细信息: 问题答案: 您的Bower json文件(在此处可见:https : //raw.