【发布时间】: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❓</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❓</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❓</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