This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Servlets and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "" Watch "" New topic

Azz Romaysa
Ranch Hand

Joined: Dec 08, 2004
Posts: 66

I'm trting to send an attachment file using javaMail. My problem is if I set the whole path of the file (for example: C:/myFile.jsp), then it works. But not if I set a relative path.

This is the code:

Ans I call this method from my servlet

The exception that I get: \vrijblijvend_offerte.jsp (The system cannot find the path specified)

My structure:

Thank you,
[ October 22, 2005: Message edited by: Azz Romaysa ]
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13007
You "relative" file path usage depends on the "current" directory - something you can't control in a servlet. You can convert a relative path in a servlet context to an absolute path - see the ServletContext getRealPath method.
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

There are two methods you should read up on:
getRealPath and getResourceAsStream.

Both are methods of ServletContext

Note: getRealPath has a serious drawback. If you're running your application from a packed war file getRealPath will return null.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Azz Romaysa
Ranch Hand

Joined: Dec 08, 2004
Posts: 66
Thank you very much,
The getRealPath() has fixed the problem.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
It's not a secret anymore!