You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Rhino <rh...@sympatico.ca> on 2004/12/21 01:50:26 UTC

[FileUpload] Testing for existence of file

I'm just polishing my first servlet that uses FileUpload and I'd like to ask
the experts how they're handling one situation that I encountered in my
testing.

While the normal (sensible?) way to do a download is to click the browse
button, then use the file dialog to find and select the file you want to
upload to the server, I noticed that it is possible for the user to simply
type in a file name in the input field. Unfortunately, that means the user
can type the name of a file that doesn't exist.

I would like my servlet to handle that situation gracefully: if a user
enters a file name that doesn't exist, I want to tell the user that this
happened and let them try again. What is the best way to detect that the
file name specified in the list of items does not exist on the user's PC?

I tried using the exists() method in the File class and it worked fine as
long as I was uploading from my PC to somewhere else on my PC. However, when
I put the servlet on our server, this logic mistakenly told me that a file
which certainly DID exist on my PC didn't exist; I assume it was looking on
the server to see if that file was where I'd specified in the upload.

How are the experts handling this problem? I don't see anything in the
FileUpload API that specifically tests for the existence of the file on the
user's machine so I'm curious what people are doing.

Rhino
---
rhino1 AT sympatico DOT ca
"There are two ways of constructing a software design. One way is to make it
so simple that there are obviously no deficiencies. And the other way is to
make it so complicated that there are no obvious deficiencies." - C.A.R.
Hoare


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


Re: [FileUpload] Testing for existence of file

Posted by Wade Chandler <wc...@redesetgrow.com>.
Rhino wrote:
> I'm just polishing my first servlet that uses FileUpload and I'd like to ask
> the experts how they're handling one situation that I encountered in my
> testing.
> 
> While the normal (sensible?) way to do a download is to click the browse
> button, then use the file dialog to find and select the file you want to
> upload to the server, I noticed that it is possible for the user to simply
> type in a file name in the input field. Unfortunately, that means the user
> can type the name of a file that doesn't exist.
> 
> I would like my servlet to handle that situation gracefully: if a user
> enters a file name that doesn't exist, I want to tell the user that this
> happened and let them try again. What is the best way to detect that the
> file name specified in the list of items does not exist on the user's PC?
> 
> I tried using the exists() method in the File class and it worked fine as
> long as I was uploading from my PC to somewhere else on my PC. However, when
> I put the servlet on our server, this logic mistakenly told me that a file
> which certainly DID exist on my PC didn't exist; I assume it was looking on
> the server to see if that file was where I'd specified in the upload.
> 
> How are the experts handling this problem? I don't see anything in the
> FileUpload API that specifically tests for the existence of the file on the
> user's machine so I'm curious what people are doing.
> 
> Rhino
> ---
> rhino1 AT sympatico DOT ca
> "There are two ways of constructing a software design. One way is to make it
> so simple that there are obviously no deficiencies. And the other way is to
> make it so complicated that there are no obvious deficiencies." - C.A.R.
> Hoare
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
> 
> 
Well, the data will be completely empty once you get to the server side. 
  So now it's up to you whether or not in your context this is an issue 
or not.  You can always explain to the user that you received the file, 
it was empty, and they can make sure this is correct.  It's a limitation 
of html basically.

If you are writing a complicated application to run over the web I 
suggest using web start and java as a front end.  This way you have a 
much richer client yet the application can still be distributed.

If that is over kill for what you need to do, then basically that is the 
only option you have.....either empty files are an error or they are 
not, and if not you should explain to the user what might have happened, 
but not rely on being able to tell exactly what the reason is for the 
file being empty, so you can't force the issue that much except for 
maybe do something like send them back another simple form for 
verification....store off the data on the server for a period of time 
and tell the user how long they have to accept or deny the submit.  If 
that time runs out....delete the info from the server.

You could use a simple thread that sleeps for a special period of time 
for this, or you could have a process that runs watching your files, and 
you can use your db to register things that should be deleted at a 
certain time....how you stop that situation from happening if you need 
to you I'll leave up to you...and there are many other possibilities for 
this as well.

Wade


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


Re: [FileUpload] Testing for existence of file

Posted by Dakota Jack <da...@gmail.com>.
Lo, Rhino,

One thing you have to do is to integrate whatever you do here with
what you do about uploading empty files.  As long as your policies on
the two are consistent and integrated, I don't see a big problem.  If
you accept empty files, then you might have to inform your client that
the file was empty and that this can be so for a number of reasons
(including a non-existent file) which you might want to list with
choices to the client on what to do.  You might also want to take care
of this on the client end, leaving the server side ignorant of
whatever choices the client happens to be making.

Jack  


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

~Dakota Jack~

"You can't wake a person who is pretending to be asleep."

~Native Proverb~

"Each man is good in His sight. It is not necessary for eagles to be crows."

~Hunkesni (Sitting Bull), Hunkpapa Sioux~

-----------------------------------------------

"This message may contain confidential and/or privileged information.
If you are not the addressee or authorized to receive this for the
addressee, you must not use, copy, disclose, or take any action based
on this message or any information herein. If you have received this
message in error, please advise the sender immediately by reply e-mail
and delete this message. Thank you for your cooperation."

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