New uploaded files and Tomcat?

2019-05-29 19:22发布

问题:

I have a simple servlet for images uploading, a user select an image and the servlet does its job and then write it to upload directory. My project layout:

project
    ├───static
    │   ├───img
    │   └───js
    ├───upload
    └───WEB-INF
        └───lib

After uploading the image, the user is redirected to it's location:

http://localhost:8080/upload/[image MD5 hash].png

The uploading code works pretty well, but Tomcat returns 404 error when requesting the url even when the image is there:

> 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.
> 

After I click update classes and resources in my IDE, Intellij, it works:

> 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]
>

The second thing is that when I manualy delete this image, I still can download it (does Tomcat do some sort of cashing?):

> 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]
> 

My web.xml file:

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/static/*</url-pattern>
    <url-pattern>/upload/*</url-pattern>
</servlet-mapping>

Could someone please tell me where is the problem?

回答1:

when deploy in development your resouce will deploy in development resource not workspace ,and when you remove in workspace must redeploy for change in development resource

sory in my english,



回答2:

This is not the right way to deal with file uploads. You should not store uploaded files in the deploy folder. You should for sure also not map the container-specific default servlet in webapp's own web.xml. You should store uploaded files in a fixed non-deploy-related folder elsewhere on the local disk file system publish it via a new <Context> in Tomcat. E.g.

<Context docBase="/var/webapp/upload" path="/upload" />

See also:

  • Uploaded image only available after refreshing the page