Pregunta

I have a problem with an image upload in spring 3 mvc. I search a couple of hours but can't find any satisfying solution.

What I want to do?

I simply want to upload an Image to the server and store it in a location where I can easily access it from my jsp sites to show the image and so on.

I don't know, if use the wrong keyword but I can't find a good solution for it and I'm to new on spring to figure it out by my self ...

So the problem is not to store the file on the server, that problem I solved already but till now I can only save it to the ServletContext#getRealPath() and there I think I can't access the image in the jsp site.

Did have anyone a good idea how I can solve that or any hints to find a solution ?

very thanks for your help and very sorry for my english ... it is quite late in germany -.-

¿Fue útil?

Solución

According to me when you try to access the image in your jsp file it will give you 404 error.

You need to include the following line of code in your servlet.xml file.

<mvc:resources location="/assets/" mapping="/assets/**" />

You just need to replace the path you have stored your uploaded images in. And then everything will work.

Hope this helps you.

Cheers.

Otros consejos

thx for your help, my problem is how to get this path for saving my file to it.

here is a code snippet how I do it now (I found this way in several tuts, but i do not know, if this is a good way to save images)

ServletContext context = RequestContextUtils.getWebApplicationContext(request).getServletContext();
    try {
        if(!image.isEmpty()) {
            validateImage(image);
            sizeChart.setImgPath(saveImage(context.getRealPath("/") + "/images/" + image.getOriginalFilename(), image));
        }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top