You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Mihael Knezevic <m....@porta.de> on 2005/05/04 13:04:29 UTC

[FileUpload] Getting wrong filename from FileItem

hi,

i'm quite new to this project and perhaps i'm making some mistakes. perhaps 
you can help me.

i'm trying to upload a file from a windows desktop to a linux server running 
tomcat 5.0.28. 

i'm using the following code to get a FileItem object:

// create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
 
// parse the request
try
{
 List items = upload.parseRequest(request);

 for (Iterator iter = items.iterator(); iter.hasNext();)
 {
  FileItem element = (FileItem) iter.next();

...
...
...

now when i use the element.getName() method i'm getting the full path (C:
\mydir\myfile.txt) instead of just name of the file (myfile.txt). there is no 
problem if the client is also a linux client. has it to do with the different 
file separator characters?

any help will be appreciated very much.

thanx in advance

mihael knezevic

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [FileUpload] Getting wrong filename from FileItem

Posted by Dakota Jack <da...@gmail.com>.
Just so there is no misunderstanding, the browsers have nothing to do
with what getName returns.  The browsers differ in what name they
provide for the file.  Thus, when you get a File object for IE it
gives the full name and for the others the actual file name itself. 
You make them all the same if you change commonsupload to give you
file,getName() rather than just file.



    // Just add this to the "protected String getFileName(Map
headers)" method in FileUploadBase
    if(fileName != null) {
      fileName = new java.io.File(fileName).getName();
    }



On 5/5/05, Mihael Knezevic <m....@porta.de> wrote:
> thanks to all the responses for my question.
> 
> actually i didn't know that it was a browser "thing". i thought it was an
> operating system thing. thanks for clearing this up. and as this is a browser
> specific thing IMHO there should be just a change in the docu where you
> should add the fact that the IE is not behaving like any other browser with
> the method getName. that would be of much help.
> 
> and this is the kind of "feature" i don't like of MS software.
> 
> Am Mittwoch, 4. Mai 2005 22:02 schrieb Frank W. Zammetti:
> > I know, I've run into this myself... But, it should be a fairly trivial
> > exercise to write code to extract just the filename (as I believe you've
> > done).  This makes sense to me as an additional method.  That way no
> > existing code gets broken, but newer code can use the new method, which
> > works as getName() probably should have been working all along anyway
> > (IMHO).
> >
> > Frank
> >
> > Dakota Jack wrote:
> > > The data difference is not in the code but in the request data from
> > > the browser.  IE puts in the full name and other browsers put in just
> > > the name.  Calling [file].getName() gives the same result for all of
> > > them.  I don't think there is a way to get a full name from the
> > > browsers that send only the file name.  There is no reason, of course,
> > > to know the full name.
> > >
> > > On 5/4/05, Frank W. Zammetti <fz...@omnytex.com> wrote:
> > >
> > >>It seems to me a method to get JUST the filename makes a lot of sense...
> > >>what has been the reason for not adding it IN ADDITION to what's there
> > >>now?  Unfortunately it would make more sense if getName() returned just
> > >>the name while there was another method, maybe getFullName() that
> > >>returned what getName() returns now, but that breaks existing code
> > >>potentially, so I can certainly see why that hasn't been done, but
> > >>adding something like getJustTheName() or something (obviously there
> > >>must be a better choice!) makes sense to me.
> > >>
> > >>Frank
> > >>
> > >>Dakota Jack wrote:
> > >>
> > >>>That is an excellent point, Robert.  I often forget how horrible it
> > >>>must be to be immeshed in those programming worlds where there is no
> > >>>recourse.  Too often we shy away, I think, at changing code, even with
> > >>>the Java distribution itself, license issues notwithstanding.
> > >>>
> > >>>On 5/4/05, robert burrell donkin <ro...@blueyonder.co.uk>
> wrote:
> > >>>
> > >>>
> > >>>>On Wed, 2005-05-04 at 10:26 -0700, Dakota Jack wrote:
> > >>>>
> > >>>>
> > >>>>>Some people think this is a bug and some people think that this is a
> > >>>>>feature.
> > >>>>
> > >>>><snip>
> > >>>>
> > >>>>>That is not a problem, however.  I just went in and changed the
> > >>>>>commons upload code adding .getName() to the file at this stage.  The
> > >>>>>problem you are seeing is in Internet Explorer, right?  Firefox and
> > >>>>>Netscape return the file name and Internet Explorer returns the full
> > >>>>>path.  Anyway, you either have to solve the problem in the commons
> > >>>>>upload code itself, where it is easy to solve, or to go through some
> > >>>>>wasted mechanisms after the fact.  Everyone has to solve this problem
> > >>>>>somewhere, so I definitely come down on the side of "bug" versus
> > >>>>>"feature".
> > >>>>
> > >>>>the great thing about open source is that you are free to do exactly
> > >>>>this :)
> > >>>>
> > >>>>- robert
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
> 


-- 
"You can lead a horse to water but you cannot make it float on its back."
~Dakota Jack~

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [FileUpload] Getting wrong filename from FileItem

Posted by Mihael Knezevic <m....@porta.de>.
thanks to all the responses for my question.

actually i didn't know that it was a browser "thing". i thought it was an 
operating system thing. thanks for clearing this up. and as this is a browser 
specific thing IMHO there should be just a change in the docu where you 
should add the fact that the IE is not behaving like any other browser with 
the method getName. that would be of much help.

and this is the kind of "feature" i don't like of MS software.

Am Mittwoch, 4. Mai 2005 22:02 schrieb Frank W. Zammetti:
> I know, I've run into this myself... But, it should be a fairly trivial 
> exercise to write code to extract just the filename (as I believe you've 
> done).  This makes sense to me as an additional method.  That way no 
> existing code gets broken, but newer code can use the new method, which 
> works as getName() probably should have been working all along anyway 
> (IMHO).
> 
> Frank
> 
> Dakota Jack wrote:
> > The data difference is not in the code but in the request data from
> > the browser.  IE puts in the full name and other browsers put in just
> > the name.  Calling [file].getName() gives the same result for all of
> > them.  I don't think there is a way to get a full name from the
> > browsers that send only the file name.  There is no reason, of course,
> > to know the full name.
> > 
> > On 5/4/05, Frank W. Zammetti <fz...@omnytex.com> wrote:
> > 
> >>It seems to me a method to get JUST the filename makes a lot of sense...
> >>what has been the reason for not adding it IN ADDITION to what's there
> >>now?  Unfortunately it would make more sense if getName() returned just
> >>the name while there was another method, maybe getFullName() that
> >>returned what getName() returns now, but that breaks existing code
> >>potentially, so I can certainly see why that hasn't been done, but
> >>adding something like getJustTheName() or something (obviously there
> >>must be a better choice!) makes sense to me.
> >>
> >>Frank
> >>
> >>Dakota Jack wrote:
> >>
> >>>That is an excellent point, Robert.  I often forget how horrible it
> >>>must be to be immeshed in those programming worlds where there is no
> >>>recourse.  Too often we shy away, I think, at changing code, even with
> >>>the Java distribution itself, license issues notwithstanding.
> >>>
> >>>On 5/4/05, robert burrell donkin <ro...@blueyonder.co.uk> 
wrote:
> >>>
> >>>
> >>>>On Wed, 2005-05-04 at 10:26 -0700, Dakota Jack wrote:
> >>>>
> >>>>
> >>>>>Some people think this is a bug and some people think that this is a
> >>>>>feature.
> >>>>
> >>>><snip>
> >>>>
> >>>>>That is not a problem, however.  I just went in and changed the
> >>>>>commons upload code adding .getName() to the file at this stage.  The
> >>>>>problem you are seeing is in Internet Explorer, right?  Firefox and
> >>>>>Netscape return the file name and Internet Explorer returns the full
> >>>>>path.  Anyway, you either have to solve the problem in the commons
> >>>>>upload code itself, where it is easy to solve, or to go through some
> >>>>>wasted mechanisms after the fact.  Everyone has to solve this problem
> >>>>>somewhere, so I definitely come down on the side of "bug" versus
> >>>>>"feature".
> >>>>
> >>>>the great thing about open source is that you are free to do exactly
> >>>>this :)
> >>>>
> >>>>- robert

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [FileUpload] Getting wrong filename from FileItem

Posted by "Frank W. Zammetti" <fz...@omnytex.com>.
I know, I've run into this myself... But, it should be a fairly trivial 
exercise to write code to extract just the filename (as I believe you've 
done).  This makes sense to me as an additional method.  That way no 
existing code gets broken, but newer code can use the new method, which 
works as getName() probably should have been working all along anyway 
(IMHO).

Frank

Dakota Jack wrote:
> The data difference is not in the code but in the request data from
> the browser.  IE puts in the full name and other browsers put in just
> the name.  Calling [file].getName() gives the same result for all of
> them.  I don't think there is a way to get a full name from the
> browsers that send only the file name.  There is no reason, of course,
> to know the full name.
> 
> On 5/4/05, Frank W. Zammetti <fz...@omnytex.com> wrote:
> 
>>It seems to me a method to get JUST the filename makes a lot of sense...
>>what has been the reason for not adding it IN ADDITION to what's there
>>now?  Unfortunately it would make more sense if getName() returned just
>>the name while there was another method, maybe getFullName() that
>>returned what getName() returns now, but that breaks existing code
>>potentially, so I can certainly see why that hasn't been done, but
>>adding something like getJustTheName() or something (obviously there
>>must be a better choice!) makes sense to me.
>>
>>Frank
>>
>>Dakota Jack wrote:
>>
>>>That is an excellent point, Robert.  I often forget how horrible it
>>>must be to be immeshed in those programming worlds where there is no
>>>recourse.  Too often we shy away, I think, at changing code, even with
>>>the Java distribution itself, license issues notwithstanding.
>>>
>>>On 5/4/05, robert burrell donkin <ro...@blueyonder.co.uk> wrote:
>>>
>>>
>>>>On Wed, 2005-05-04 at 10:26 -0700, Dakota Jack wrote:
>>>>
>>>>
>>>>>Some people think this is a bug and some people think that this is a
>>>>>feature.
>>>>
>>>><snip>
>>>>
>>>>>That is not a problem, however.  I just went in and changed the
>>>>>commons upload code adding .getName() to the file at this stage.  The
>>>>>problem you are seeing is in Internet Explorer, right?  Firefox and
>>>>>Netscape return the file name and Internet Explorer returns the full
>>>>>path.  Anyway, you either have to solve the problem in the commons
>>>>>upload code itself, where it is easy to solve, or to go through some
>>>>>wasted mechanisms after the fact.  Everyone has to solve this problem
>>>>>somewhere, so I definitely come down on the side of "bug" versus
>>>>>"feature".
>>>>
>>>>the great thing about open source is that you are free to do exactly
>>>>this :)
>>>>
>>>>- robert
>>>>
>>>>
>>>>---------------------------------------------------------------------
>>>>To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>>>>For additional commands, e-mail: commons-user-help@jakarta.apache.org
>>>>
>>>>
>>>
>>>
>>>
>>--
>>Frank W. Zammetti
>>Founder and Chief Software Architect
>>Omnytex Technologies
>>http://www.omnytex.com
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: commons-user-help@jakarta.apache.org
>>
>>
> 
> 
> 

-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [FileUpload] Getting wrong filename from FileItem

Posted by Dakota Jack <da...@gmail.com>.
The data difference is not in the code but in the request data from
the browser.  IE puts in the full name and other browsers put in just
the name.  Calling [file].getName() gives the same result for all of
them.  I don't think there is a way to get a full name from the
browsers that send only the file name.  There is no reason, of course,
to know the full name.

On 5/4/05, Frank W. Zammetti <fz...@omnytex.com> wrote:
> It seems to me a method to get JUST the filename makes a lot of sense...
> what has been the reason for not adding it IN ADDITION to what's there
> now?  Unfortunately it would make more sense if getName() returned just
> the name while there was another method, maybe getFullName() that
> returned what getName() returns now, but that breaks existing code
> potentially, so I can certainly see why that hasn't been done, but
> adding something like getJustTheName() or something (obviously there
> must be a better choice!) makes sense to me.
> 
> Frank
> 
> Dakota Jack wrote:
> > That is an excellent point, Robert.  I often forget how horrible it
> > must be to be immeshed in those programming worlds where there is no
> > recourse.  Too often we shy away, I think, at changing code, even with
> > the Java distribution itself, license issues notwithstanding.
> >
> > On 5/4/05, robert burrell donkin <ro...@blueyonder.co.uk> wrote:
> >
> >>On Wed, 2005-05-04 at 10:26 -0700, Dakota Jack wrote:
> >>
> >>>Some people think this is a bug and some people think that this is a
> >>>feature.
> >>
> >><snip>
> >>
> >>>That is not a problem, however.  I just went in and changed the
> >>>commons upload code adding .getName() to the file at this stage.  The
> >>>problem you are seeing is in Internet Explorer, right?  Firefox and
> >>>Netscape return the file name and Internet Explorer returns the full
> >>>path.  Anyway, you either have to solve the problem in the commons
> >>>upload code itself, where it is easy to solve, or to go through some
> >>>wasted mechanisms after the fact.  Everyone has to solve this problem
> >>>somewhere, so I definitely come down on the side of "bug" versus
> >>>"feature".
> >>
> >>the great thing about open source is that you are free to do exactly
> >>this :)
> >>
> >>- robert
> >>
> >>
> >>---------------------------------------------------------------------
> >>To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> >>For additional commands, e-mail: commons-user-help@jakarta.apache.org
> >>
> >>
> >
> >
> >
> 
> --
> Frank W. Zammetti
> Founder and Chief Software Architect
> Omnytex Technologies
> http://www.omnytex.com
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
> 


-- 
"You can lead a horse to water but you cannot make it float on its back."
~Dakota Jack~

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [FileUpload] Getting wrong filename from FileItem

Posted by "Frank W. Zammetti" <fz...@omnytex.com>.
It seems to me a method to get JUST the filename makes a lot of sense... 
what has been the reason for not adding it IN ADDITION to what's there 
now?  Unfortunately it would make more sense if getName() returned just 
the name while there was another method, maybe getFullName() that 
returned what getName() returns now, but that breaks existing code 
potentially, so I can certainly see why that hasn't been done, but 
adding something like getJustTheName() or something (obviously there 
must be a better choice!) makes sense to me.

Frank

Dakota Jack wrote:
> That is an excellent point, Robert.  I often forget how horrible it
> must be to be immeshed in those programming worlds where there is no
> recourse.  Too often we shy away, I think, at changing code, even with
> the Java distribution itself, license issues notwithstanding.
> 
> On 5/4/05, robert burrell donkin <ro...@blueyonder.co.uk> wrote:
> 
>>On Wed, 2005-05-04 at 10:26 -0700, Dakota Jack wrote:
>>
>>>Some people think this is a bug and some people think that this is a
>>>feature.
>>
>><snip>
>>
>>>That is not a problem, however.  I just went in and changed the
>>>commons upload code adding .getName() to the file at this stage.  The
>>>problem you are seeing is in Internet Explorer, right?  Firefox and
>>>Netscape return the file name and Internet Explorer returns the full
>>>path.  Anyway, you either have to solve the problem in the commons
>>>upload code itself, where it is easy to solve, or to go through some
>>>wasted mechanisms after the fact.  Everyone has to solve this problem
>>>somewhere, so I definitely come down on the side of "bug" versus
>>>"feature".
>>
>>the great thing about open source is that you are free to do exactly
>>this :)
>>
>>- robert
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: commons-user-help@jakarta.apache.org
>>
>>
> 
> 
> 

-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [FileUpload] Getting wrong filename from FileItem

Posted by Dakota Jack <da...@gmail.com>.
That is an excellent point, Robert.  I often forget how horrible it
must be to be immeshed in those programming worlds where there is no
recourse.  Too often we shy away, I think, at changing code, even with
the Java distribution itself, license issues notwithstanding.

On 5/4/05, robert burrell donkin <ro...@blueyonder.co.uk> wrote:
> On Wed, 2005-05-04 at 10:26 -0700, Dakota Jack wrote:
> > Some people think this is a bug and some people think that this is a
> > feature.
> 
> <snip>
> 
> > That is not a problem, however.  I just went in and changed the
> > commons upload code adding .getName() to the file at this stage.  The
> > problem you are seeing is in Internet Explorer, right?  Firefox and
> > Netscape return the file name and Internet Explorer returns the full
> > path.  Anyway, you either have to solve the problem in the commons
> > upload code itself, where it is easy to solve, or to go through some
> > wasted mechanisms after the fact.  Everyone has to solve this problem
> > somewhere, so I definitely come down on the side of "bug" versus
> > "feature".
> 
> the great thing about open source is that you are free to do exactly
> this :)
> 
> - robert
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
> 


-- 
"You can lead a horse to water but you cannot make it float on its back."
~Dakota Jack~

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [FileUpload] Getting wrong filename from FileItem

Posted by robert burrell donkin <ro...@blueyonder.co.uk>.
On Wed, 2005-05-04 at 10:26 -0700, Dakota Jack wrote:
> Some people think this is a bug and some people think that this is a
> feature.  

<snip>

> That is not a problem, however.  I just went in and changed the
> commons upload code adding .getName() to the file at this stage.  The
> problem you are seeing is in Internet Explorer, right?  Firefox and
> Netscape return the file name and Internet Explorer returns the full
> path.  Anyway, you either have to solve the problem in the commons
> upload code itself, where it is easy to solve, or to go through some
> wasted mechanisms after the fact.  Everyone has to solve this problem
> somewhere, so I definitely come down on the side of "bug" versus
> "feature".

the great thing about open source is that you are free to do exactly
this :) 

- robert


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [FileUpload] Getting wrong filename from FileItem

Posted by Dakota Jack <da...@gmail.com>.
Some people think this is a bug and some people think that this is a
feature.  Martin, who makes all the decisions as a practical but not
as a theoretical (so please let's not debate this) matter on this has
decided it is a feature.  Since he seems to integrate his day job and
this work, that is not likely to change.  That is not to suggest there
is anything wrong but merely to advise you that you are not likely to
convince him this is a bug when it is integrated into his own work. 
That is not a problem, however.  I just went in and changed the
commons upload code adding .getName() to the file at this stage.  The
problem you are seeing is in Internet Explorer, right?  Firefox and
Netscape return the file name and Internet Explorer returns the full
path.  Anyway, you either have to solve the problem in the commons
upload code itself, where it is easy to solve, or to go through some
wasted mechanisms after the fact.  Everyone has to solve this problem
somewhere, so I definitely come down on the side of "bug" versus
"feature".

Jack

On 5/4/05, Mihael Knezevic <m....@porta.de> wrote:
> hi,
> 
> i'm quite new to this project and perhaps i'm making some mistakes. perhaps
> you can help me.
> 
> i'm trying to upload a file from a windows desktop to a linux server running
> tomcat 5.0.28.
> 
> i'm using the following code to get a FileItem object:
> 
> // create a new file upload handler
> ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
> 
> // parse the request
> try
> {
>  List items = upload.parseRequest(request);
> 
>  for (Iterator iter = items.iterator(); iter.hasNext();)
>  {
>   FileItem element = (FileItem) iter.next();
> 
> ...
> ...
> ...
> 
> now when i use the element.getName() method i'm getting the full path (C:
> \mydir\myfile.txt) instead of just name of the file (myfile.txt). there is no
> problem if the client is also a linux client. has it to do with the different
> file separator characters?
> 
> any help will be appreciated very much.
> 
> thanx in advance
> 
> mihael knezevic
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
> 


-- 
"You can lead a horse to water but you cannot make it float on its back."
~Dakota Jack~

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org