错误日志
2015-11-05 00:14:42 [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] DispatcherServlet with name 'springmvc' processing GET request for [/DailyPhoto/fileUpload/]
2015-11-05 00:14:42 [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping]-[DEBUG] Looking up handler method for path /fileUpload/
2015-11-05 00:14:42 [org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver]-[DEBUG] Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
2015-11-05 00:14:42 [org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver]-[DEBUG] Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
2015-11-05 00:14:42 [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver]-[DEBUG] Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
2015-11-05 00:14:42 [org.springframework.web.servlet.PageNotFound]-[WARN] Request method 'GET' not supported
2015-11-05 00:14:42 [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] Null ModelAndView returned to DispatcherServlet with name 'springmvc': assuming HandlerAdapter completed request handling
2015-11-05 00:14:42 [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] Successfully completed request
JSP form:
<form action="fileUpload" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="file">
<input type="submit" value="提交">
</form>
Controller:
@RequestMapping(value="/fileUpload", method=RequestMethod.POST)
public String fileUpload(@RequestParam("file") MultipartFile file) {
// 判断文件是否为空
if (!file.isEmpty()) {
try {
// 文件保存路径
String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/"
+ file.getOriginalFilename();
// 转存文件
file.transferTo(new File(filePath));
} catch (Exception e) {
e.printStackTrace();
}
}
// 重定向
return "redirect:/list.html";
}
解决办法:
将JSP代码改成如下:
<form action="fileUpload/" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="file">
<input type="submit" value="提交">
</form>
其实就是在action的值后面增加了一个 “/”,问题就解决了。