【问题标题】:I am trying to upload multiple files using spring mvc,Only first file is uploading我正在尝试使用spring mvc上传多个文件,只有第一个文件正在上传
【发布时间】:2016-09-05 10:23:23
【问题描述】:

我正在尝试使用 Spring MVC 上传多个文件,第一个文件上传正常,从第二个开始获取 org.springframework.web.servlet.DispatcherServlet noHandlerFound 在名称为“spring”的 DispatcherServlet 中找不到具有 URI [user2-160x160.jpg] 的 HTTP 请求的映射

这是我的控制器:

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST) 
public ModelAndView viewProducts(HttpServletRequest request, HttpServletResponse response,@RequestParam CommonsMultipartFile[] fileUpload) throws IllegalStateException, IOException 
        {       
            ModelAndView model= null;
             String saveDirectory = "E:/Test/Upload/";
            model = new ModelAndView("user/dashboard/profile-view");

            System.out.println("In New");
            if (fileUpload != null && fileUpload.length > 0) {
                for (CommonsMultipartFile aFile : fileUpload){

                    System.out.println("Saving file: " + aFile.getOriginalFilename());

                    if (!aFile.getOriginalFilename().equals("")) {
                        aFile.transferTo(new File(saveDirectory + aFile.getOriginalFilename()));
                    }
                }
            }

            // returns to the view "Result"

            return model;
        }

我的小服务程序:

<mvc:annotation-driven />

<bean id="jspViewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
    <property name="order" value="1" />

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- max upload size in bytes -->
    <property name="maxUploadSize" value="20971520" /> <!-- 20MB -->

    <!-- max size of file in memory (in bytes) -->
    <property name="maxInMemorySize" value="1048576" /> <!-- 1MB -->



</bean>


<bean
    class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <property name="exceptionMappings">
        <props>
            <prop key="java.lang.Exception">Error</prop>
        </props>
    </property>
</bean>    

我的jsp看起来像:

<form class="form-horizontal" method="post" action="uploadFile" enctype="multipart/form-data">
                              <div class="box-body">
                                <div class="form-group">
                                  <label for="inputEmail3" class="col-sm-2 control-label">Business Registration Proof&#10067;</label>
                                  <div class="col-sm-10">
                                    <input type="file" class="form-control" id="bus_reg_proof" name="fileUpload" size="50" placeholder="Choose file">
                                  </div>
                                </div>

                                    <div class="form-group">
                                  <label for="inputPassword3" class="col-sm-2 control-label">Business PAN&#10067;</label>
                                  <div class="col-sm-10">
                                    <input type="file" class="form-control" id="bus_pan" name="fileUpload" size="50" placeholder="Choose file">
                                  </div>
                                </div>
                                <div class="form-group">
                                  <label for="inputPassword3" class="col-sm-2 control-label">Company's Bank Account Statement with Address</label>
                                  <div class="col-sm-10">
                                    <input type="file" class="form-control" id="com_bank_acc_st" name="fileUpload" size="50" placeholder="Choose file">
                                  </div>
                                </div>
                                <div class="form-group">
                                  <label for="inputPassword3" class="col-sm-2 control-label">Authorised Signatory Address Proof&#10067;</label>
                                  <div class="col-sm-10">
                                    <input type="file" class="form-control" id="auth_sign_add_proof" name="fileUpload" size="50" placeholder="Choose file">
                                  </div>
                                </div>
                                     <!-- /.box-body -->
                              </div>
                              <div class="box-footer">
                                 <input type="submit"  class="btn btn-info pull-right" "></button>
                              </div><!-- /.box-footer -->

【问题讨论】:

  • 第二次提交表单?还是单个表单中的多个文件?至少你的行为是错误的!目前您有一个相对 URL,这意味着您第二次(上传后)提交它将提交到/uploadFile/uploadFile。您的控制器也有缺陷,您应该针对接口MultipartFile 而不是具体的CommonsMultipartFile 进行编程。
  • 一个表单中的多个文件!
  • 你能说得更具体些吗?我现在需要做什么?
  • 阅读评论...您还没有发布有关如何配置 Spring MVC 的注释驱动性质的配置。还要先解决前面提到的问题。
  • jar 文件有问题,我已经为文件上传创建了一个单独的项目,工作正常...当我将上传文件集成到我的项目时出现问题...我尝试删除我的 jar 文件项目,上传再次正常工作..有什么解决方案吗?

标签: java spring spring-mvc


【解决方案1】:

要上传文件列表,您需要将所有这些文件包装在您将从 jsp 发送/恢复的对象中。 让我们想象一个名为 FilesWrapper 的对象应该如下所示:

public class FilesWrapper{
    private List<CommonsMultipartFile> files;
    //getters - setters
}

然后在您的 GET 方法中绘制您需要在 ModelAndView 中发送该对象的页面

modelAndView.add("filesWrapper", new FilesWrapper());

在您的 jsp 表单中。你应该发送一个文件数组,那么属性名应该是一个数组。

<form:form class="form-horizontal" method="post" action="uploadFile" enctype="multipart/form-data" modelAttribute="filesWrapper">


<div class="col-sm-10">
   <input type="file" class="form-control" id="bus_reg_proof" name="files[0]" size="50" placeholder="Choose file">
</div>
<div class="col-sm-10">
   <input type="file" class="form-control" id="bus_reg_proof" name="files[1]" size="50" placeholder="Choose file">
</div>

...

在你的控制器方法中 POST

@RequestParam("filesWrapepr") FilesWrapper filesWrapper)

【讨论】:

  • 感谢您的回答,我已经尝试过了。获取所需 [Lorg.springframework.web.multipart.commons.CommonsMultipartFile;参数“fileUpload”不存在
  • @ManjunathReddy 尝试使用参数名称添加我添加到控制器的更改。
  • 我已经尝试将它添加到我的控制器中,得到相同的错误消息
  • @ManjunathReddy 我编辑了我的回复,您无法将列表发送到 POST 方法,spring 无法正确处理它,您需要将所有这些日期包装到一个对象中。
  • jar 文件有问题,我已经为文件上传创建了一个单独的项目,工作正常...当我将上传文件集成到我的项目时出现问题...我尝试删除我的 jar 文件项目,上传再次正常工作..有什么解决方案吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-28
  • 2021-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-24
  • 1970-01-01
相关资源
最近更新 更多