• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting file name from input=file???

 
mocca az
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings!
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
Posts: 371
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
parse it yourself using substring from the last file separator.
 
Tim Holloway
Saloon Keeper
Posts: 18300
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the docs for java.io.File (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.
 
Manjunath Reddy
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ("/").
cheers,
mpr
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic