aspose file tools*
The moose likes JSP and the fly likes Stripping path out of a filename Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Stripping path out of a filename" Watch "Stripping path out of a filename" New topic
Author

Stripping path out of a filename

Jacob Fenwick
Ranch Hand

Joined: Apr 28, 2006
Posts: 55
I've built a fileupload jsp using Apache Commons fileUpload and I want to get the original filename of an uploaded file.
I put the file into a FileItem object and use the getName function. When I'm in Firefox it works perfectly, as it gets just the name and not the path. However when I'm in IE it uses the entire path of the file as the name. I want it to work the way it does in Firefox, I want to get only the name and not the path. Has anyone encountered this problem, and if you did, how did you solve it?
This is not necessarily specific to the Apache Commons fileUpload, I imagine the problem of stripping a path out of a filename is something people encounter often.
Jacob Fenwick
Ranch Hand

Joined: Apr 28, 2006
Posts: 55
Er... nevermind. It helps to read FAQs, heh.


Why does FileItem.getName() return the whole path, and not just the file name?
Internet Explorer provides the entire path to the uploaded file and not just the base file name. Since FileUpload provides exactly what was supplied by the client (browser), you may want to remove this path information in your application. You can do that using the following method from Commons IO (which you already have, since it is used by FileUpload).

String fileName = item.getName();
if (fileName != null) {
filename = FilenameUtils.getName(filename);
}


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stripping path out of a filename
 
Similar Threads
session variable for file upload box
Configuring Servers or have to use existing or have to make a new?
Uploading file in a database
image not shows full path
Open a file in a jsp page