MDP
MDP

Reputation: 4287

How to retrieve the right URL of a file uploaded (with spring MVC) on the server (Tomcat)

I have a form that allow user to upload an image (jpg), in order to show that image in another page.

This is the method I used:

public void salvaFoto(...) throws FotoUtilException{                
            ...
            String path = context.getRealPath("prova/1.jpg");
            File file = new File(path);     
            commonsMultipartFile.transferTo(file);
            ... 
}

It works.

The jpg is saved here: D:\root backup\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\SpringGestioneErrori\prova\1.jpg, that is the url the I get with context.getRealPath("prova/1.jpg")

Now, I have problem printing that image.

I tried to find the image using again context.getRealPath("prova/1.jpg"), this way:

<img src="D:/root backup/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/SpringGestioneErrori/prova/1.jpg">

but it doesn't work.

I tried also:

<img src="/SpringGestioneErrori/prova/1.jpg">

but it didn't work as well.

Thank you

Upvotes: 0

Views: 727

Answers (1)

Dheeraj
Dheeraj

Reputation: 173

I am using local rootDirecotry instead. This is working fine for me.

String rootDirectory = request.getSession().getServletContext().getRealPath("/");
image.transferTo(new File(rootDirectory+"\images\"+ImageName.jpg"));

Then access it from same directory

<img src="<c:url value="/images/imageName.jpg">

Upvotes: 1

Related Questions