我正在尝试使用spring mvc上传多个文件,只有第一个文件正在上传

时间:2016-05-10 12:26:55

标签: java spring spring-mvc

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

这是我的控制器:

@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;
        }

我的servlet:

<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 -->

1 个答案:

答案 0 :(得分:0)

要上传文件列表,您需要将所有这些文件包装在您将从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)