我有一个用于上传图片的简单 servlet,用户选择一张图片,servlet 完成其工作,然后将其写入上传目录。我的项目布局:
project
├───static
│ ├───img
│ └───js
├───upload
└───WEB-INF
└───lib
上传图片后,用户被重定向到它的位置:
http://localhost:8080/upload/[image MD5 hash].png
上传代码运行良好,但即使图像存在,Tomcat 在请求 url 时也会返回 404 错误:
> wget http://localhost:8080/upload/f5d1da2cadf3bd1c1b9196ec522a5d73.png
SYSTEM_WGETRC = c:/progra~1/wget/etc/wgetrc
syswgetrc = C:\GnuWin32/etc/wgetrc
--2013-01-16 03:45:33-- http://localhost:8080/upload/f5d1da2cadf3bd1c1b9196ec522a5d73.png
Resolving localhost... 127.0.0.1, ::1
Connecting to localhost|127.0.0.1|:8080... connected.
HTTP request sent, awaiting response... 404 Not Found
2013-01-16 03:45:33 ERROR 404: Not Found.
>
单击update classes and resources
我的 IDE Intellij 后,它可以工作:
> wget http://localhost:8080/upload/f5d1da2cadf3bd1c1b9196ec522a5d73.png
SYSTEM_WGETRC = c:/progra~1/wget/etc/wgetrc
syswgetrc = C:\GnuWin32/etc/wgetrc
--2013-01-16 03:48:11-- http://localhost:8080/upload/f5d1da2cadf3bd1c1b9196ec522a5d73.png
Resolving localhost... 127.0.0.1, ::1
Connecting to localhost|127.0.0.1|:8080... connected.
HTTP request sent, awaiting response... 200 OK
Length: 11666 (11K) [image/png]
Saving to: `f5d1da2cadf3bd1c1b9196ec522a5d73.png'
100%[==========================================================>] 11,666 --.-K/s in 0s
2013-01-16 03:48:11 (72.9 MB/s) - `f5d1da2cadf3bd1c1b9196ec522a5d73.png' saved [11666/11666]
>
第二件事是当我手动删除这个图像时,我仍然可以下载它(Tomcat 是否会进行某种兑现?):
> del f5d1da2cadf3bd1c1b9196ec522a5d73.png
> wget http://localhost:8080/upload/f5d1da2cadf3bd1c1b9196ec522a5d73.png
SYSTEM_WGETRC = c:/progra~1/wget/etc/wgetrc
syswgetrc = C:\GnuWin32/etc/wgetrc
--2013-01-16 04:12:25-- http://localhost:8080/upload/f5d1da2cadf3bd1c1b9196ec522a5d73.png
Resolving localhost... 127.0.0.1, ::1
Connecting to localhost|127.0.0.1|:8080... connected.
HTTP request sent, awaiting response... 200 OK
Length: 11666 (11K) [image/png]
Saving to: `f5d1da2cadf3bd1c1b9196ec522a5d73.png'
100%[==========================================================>] 11,666 --.-K/s in 0s
2013-01-16 04:12:25 (93.2 MB/s) - `f5d1da2cadf3bd1c1b9196ec522a5d73.png' saved [11666/11666]
>
我的web.xml文件:
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
<url-pattern>/upload/*</url-pattern>
</servlet-mapping>
有人可以告诉我问题出在哪里吗?