File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Getting file name from input=file??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Getting file name from input=file???" Watch "Getting file name from input=file???" New topic

Getting file name from input=file???

mocca az
Ranch Hand

Joined: Mar 07, 2001
Posts: 93
I have .htm page with <input type="file" name="myFile">, my .jsp page uses request.getParameter("myFile");. As a result i get the entire path of the file "C:/blah/blah/blah/blah.gif".
How do i go about extracting the file name out of the entire string.
Greatly appreciated,
m, phx
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
parse it yourself using substring from the last file separator.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

Look at the docs for (which is a misnomer, because it actually refers to the locations of files, not the files themselves).

Will do it, I think. This is OS-independent, at least for Unix and Windows. Mac File seperators I don't know about.

An IDE is no substitute for an Intelligent Developer.
Manjunath Reddy
Ranch Hand

Joined: Jul 26, 2001
Posts: 60
File f = new File (request.getParameter("file"));
String fileName = f.getName().substring(f.getName().lastIndexOf("/") + 1, f.getName().length());
Just in case you want to take care of unix/ms file seperator problems, you might want to use File.Seperator instead of ("/").
I agree. Here's the link:
subject: Getting file name from input=file???
It's not a secret anymore!