【问题标题】:Uploading file using Spring MVC and CommonsMultipartResolver not working as expected使用 Spring MVC 和 CommonsMultipartResolver 上传文件未按预期工作
【发布时间】:2011-05-12 04:05:27
【问题描述】:

我正在尝试使用 Spring CommonsMultipartResolver 上传文件,但是无法识别控制器。我收到此错误消息:“请求的资源 (/WebIDE/WEB-INF/views/file/upload.jsp) 不可用。”

我在我的库中添加了 commons-fileupload-1.2.2.jar 和 commons-io.1.3.2.jar。我在我的应用程序上下文中添加了以下内容:

<context:component-scan base-package="org.webide.mvc" />

<bean id="multipartResolver"
  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

  <!-- specify maximum file size in bytes -->
  <property name="maxUploadSize" value="100000"/>
</bean>

我使用 Pojo 作为我的控制器:

@Controller
@RequestMapping (value = "/file")
public class FileController {

  @RequestMapping (value = "/upload")
  public String uploadFile(@RequestParam("file") CommonsMultipartFile file){
    if (!file.isEmpty()){
      byte fileBytes[] = file.getBytes();
      return "mainView";
    }else{
      return "errorView";
    }
  }

目前我的 html 很简单:

<form method="post" action="file/upload" enctype="multipart/form-data">
            <input type="text" name="name"/>
            <input type="file" name="file"/>
            <input type="submit"/>
</form>

如果我遗漏了什么,请告诉我吗? 谢谢

【问题讨论】:

  • 你的简单 html 的 url 是什么?

标签: java spring jsp spring-mvc apache-commons


【解决方案1】:

主 URL 是 http://localhost:8084/WebIDE/,然后 action="/file/upload" 应该向标有 @Controller 的类发送请求。我放了一个断点,它根本没有拿起控制器。

我必须更改我的 spring 配置以将应用程序上下文中指定的所有内容(见上文)复制到调度程序 servlet mvc-config.xml,并更改我声明它们的方式,以便上下文是 mvc-config 的父级.xml

看起来它成功了! :)

再次感谢您的帮助和建议。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-24
    • 1970-01-01
    • 2021-08-24
    • 2023-04-08
    • 2012-05-14
    • 1970-01-01
    • 2015-04-01
    • 2014-07-02
    相关资源
    最近更新 更多