http://bbs.csdn.net/topics/380167574?page=1
非常牛叉的楼主,自己的问题其实就是答案
原因在于目录下有一个upload文件导致的
小弟我用spring3.1.0做了一个上传文件的例子,但发现一个奇怪的问题,就是当指定requestMapping单独为upload的时候会出现404错误(如项目名是springTest,此URL为springTest/upload),调试后发现当URL中单独只有upload时它的method会被解析为GET,而我在form表单中是指定了POST的。修改为其他名称是没问题的。
下面上代码:
web.xml代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
web-app
id
=
"WebApp_ID"
version
=
"3.0"
xmlns
=
"http://java.sun.com/xml/ns/javaee"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<
servlet
>
<
servlet-name
>spring</
servlet-name
>
<
servlet-class
>org.springframework.web.servlet.DispatcherServlet</
servlet-class
>
<
load-on-startup
>1</
load-on-startup
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>spring</
servlet-name
>
<
url-pattern
>/</
url-pattern
>
</
servlet-mapping
>
</
web-app
>
|
spring-servlet.xml如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:context
=
"http://www.springframework.org/schema/context"
xmlns:mvc
=
"http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<
context:component-scan
base-package
=
"org.springframework.web.fileupload"
/>
<
bean
id
=
"multipartResolver"
class
=
"org.springframework.web.multipart.commons.CommonsMultipartResolver"
>
<
property
name
=
"maxUploadSize"
value
=
"100000"
/>
</
bean
>
<
bean
id
=
"viewResolver"
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"
/>
</
bean
>
<
mvc:annotation-driven
/>
</
beans
>
|
测试页面fileUpload.jsp如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<
html
>
<
head
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
>
<
title
>Upload File</
title
>
</
head
>
<
body
>
<
form
method
=
"post"
action="<c:url
value
=
'/upload'
/>" enctype="multipart/form-data">
<
input
type
=
"file"
name
=
"file"
/>
<
input
type
=
"submit"
value
=
"上传"
/>
</
form
>
</
body
>
</
html
>
|
另外有一个简单的Controller:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package
org.springframework.web.fileupload;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RequestParam;
import
org.springframework.web.multipart.MultipartFile;
import
org.springframework.web.servlet.ModelAndView;
@Controller
public
class
FileUploadController{
@RequestMapping
(value=
"/upload"
)
public
ModelAndView uploadFile(
@RequestParam
(value=
"file"
) MultipartFile file) {
if
(!file.isEmpty()) {
FileOutputStream fos =
null
;
try
{
byte
[] bytes = file.getBytes();
fos =
new
FileOutputStream(
"D:\\"
+file.getOriginalFilename());
fos.write(bytes);
}
catch
(IOException e) {
e.printStackTrace();
}
finally
{
try
{
fos.close();
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
return
new
ModelAndView(
"message"
);
}
}
|
这里文件默认写入到D盘根目录下。
在WEB-INF/jsp目录下有一个message.jsp,那个可有可无,只是提示而已。
不知道怎么上传图片,所以没办法上传当时的图片。
它报的一个错误是
org.springframework.web.multipart.MultipartException: The current request is not a multipart request.
而这个是由提交的方法为get引起的。
这个问题当修改form提交的action名称,即把upload修改为其他名称,或者不单独为"项目名/upload"就不会有这个问题。当然,修改后要修改相应的requesetMapping。
麻烦各位坛友看看。谢谢。