Java Web Application on Tomcat - Displaying a File Uploaded by User
I need to display uploaded files 开发者_开发问答on a web application, the flow is as follows
- User uploads file through web UI
- Validation on image
- Makes call to
imageRepository.store( uploadedImage, user.getSite() )
- The user wants the image displayed which adds
<img src="${anUploadedImage.getUrl()}"/>
but that is where I'm stuck, what can getUrl()
do? The simple solution is to put it someplace and let Apache serve the file, but then I can't use the application to ensure that one user isn't modifying the URL to view other users files, which in this case is important
You have a couple of options if you want to manage the path to the file.
- You can create a service to return a file by ID. Essentially this controller-action (or servlet) would load the image back from the repository store and serialize it back to the user. This provides the most control.
- You can create a filter that performs the control you want, then delegates back to the web server to perform the actual file serving. This still gives you control, but takes the responsibility off of your app to serialize the file efficiently.
The one thing I would want to verify (since I'm not sure what type the ${anUploadedImage}
is) is what URL getUrl()
is returning. If it is a File
object, then getUrl()
provides a file URL which is useless in the HTML. Essentially the web browser would go looking on the user's drive or report some sort of fishy activity.
The URL you include in your HTML needs to point to where your service will send the desired image.
精彩评论