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
>
|
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
>
|
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" %>
<
html
>
<
head
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
>
<
title
>Upload File
title
>
head
>
<
body
>
<
form
method
=
"post"
action="
<
input
type
=
"file"
name
=
"file"
/>
<
input
type
=
"submit"
value
=
"上传"
/>
form
>
body
>
html
>
|
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"
);
}
}
|