It's not a secret anymore!*
The moose likes JSP and the fly likes Uploading Files in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Uploading Files in jsp" Watch "Uploading Files in jsp" New topic
Author

Uploading Files in jsp

Sunetra Sen
Ranch Hand

Joined: Feb 17, 2005
Posts: 43
Hi,
I need to upload files from a jsp page and later FTP them to a particular location from the sevlet which the jsp calls.
The problem is that I'm not allowed to use <input type="file"..> which would have made my job a piece of cake
Instead the requirements state that the user will manully enter the name of the file(s) in a text area on the jsp page
I now require to somehow send the contents of these files back to the server so that my servlet can pick up the contents of the file and then do a FTP.
I am all at sea on how to do this.Any suggestions would be of immense help and any piece of code even better
Thanks in advance!
Sunetra.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"bojo clown",

We're light on rule here at the JavaRanch, but we take the display name rules quite seriously.

We require display names to be two words: your first name, a space, then your last name. Fictitious names are not allowed and accounts with invalid display names get deleted. Please edit your profile immediately.

thanks,
Dave.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Browser security limitations prevent anything except 'file' input types from accessing the client file system, so you're starting with one hand behind your back. Preventing you from using this is crazy and you can tell them I said so

The only other option I can think of is a signed applet. If you sign the code and they accept the applet certificate it will be able to access the client file system.

Strange solution for something which is already catered for...
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The problem is that I'm not allowed to use <input type="file"..>

Why???

To add to David's comments:
The 'file' input type is the ONLY way for a user to send a file from the client to the server in a web app. Once you start adding smart client components, it's not a pure web app.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Sunetra Sen
Ranch Hand

Joined: Feb 17, 2005
Posts: 43
Thanks for the quick response!
Let's see if I can persuade them to go back to input type="file"..
meanwhile can this functionality be achieved in any way without using input type="file" OR signed applet if I go for XML/XSLT
Thanks,
Sunetra
PS:I have edited my diplay name and it should show up properly soon
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Sunetra Sen:
Thanks for the quick response!
Let's see if I can persuade them to go back to input type="file"..
meanwhile can this functionality be achieved in any way without using input type="file" OR signed applet if I go for XML/XSLT
Thanks,
Sunetra
PS:I have edited my diplay name and it should show up properly soon


The 'file' input type is the ONLY way for a user to send a file from the client to the server in a web app


BTW: Did they give you any reason for not wanting to use the file, input type? I can't think of any valid reason for such a requirement.
[ February 17, 2005: Message edited by: Ben Souther ]
Sunetra Sen
Ranch Hand

Joined: Feb 17, 2005
Posts: 43
Yeah they did..
It seems that the end users may upload any number of files at a time say 2,5 7 10 etc..
So i suggested having 5 >input type="file"...> and if the number of files required to be uploaded exceeded 5 the user could come back and repeat the process.
but the users can't so this it seems..too much pain
so the pain was transfered to me the developer..and now i am in a soup
anyways thanks for the quick replies!

Sunetra
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You could always build the <input...> fields dynamically, either on the server or using DHTML on the client. Then they could add as many file fields as they like.
Sunetra Sen
Ranch Hand

Joined: Feb 17, 2005
Posts: 43
Hi,
I have been thinking of a solution.
I came up with this one:
Have a <input type="file"..> in the page.Have one add button and a text box.
Whenever the user clicks on the add button submit the page and upload the file to a particular directory in the web server.
Then redirect back to the same page.
Now each time the page loads somehow get the list of the files names of the files stored in that particular folder of the webserver.
This way the user will feel as if he is adding the names of the files to the text area after browsing for those.
But actually we will be uploading files one at a time.Finally on click of some button ,say submit,on the jsp page all the files stored in the particular location in the webserver gets FTP'd or whatever.
Does this sound feasible to you?
Please post your thoughts on this.

Thanks,
Sunetra.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Originally posted by Sunetra Sen:
Now each time the page loads somehow get the list of the files names of the files stored in that particular folder of the webserver.
This way the user will feel as if he is adding the names of the files to the text area after browsing for those.


It can't be done this way, and you may not be able to dynamically add file input fields.

The 'file' input type has heavy security measures, and as such you cannot pre-populate the value in the form in any way and you cannot get a directory listing in client side JavaScript. Sorry, it's just too insecure.

There's no getting around the fact they will have to select each file manually, but the plus side is that the file selector usually remembers the last directory, so that'll make it easier.

---

Another advantage to file input over applets is that if the page is secure so is the file transfer, while the applet communication may not be unless specified separately.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by David O'Meara:


It can't be done this way, and you may not be able to dynamically add file input fields.

The 'file' input type has heavy security measures, and as such you cannot pre-populate the value in the form in any way and you cannot get a directory listing in client side JavaScript. Sorry, it's just too insecure.

There's no getting around the fact they will have to select each file manually, but the plus side is that the file selector usually remembers the last directory, so that'll make it easier.

---

Another advantage to file input over applets is that if the page is secure so is the file transfer, while the applet communication may not be unless specified separately.


You can dynamically add input fields but can't populate them. The only way to populate them is for the user to do it.

I have a form with DHTML that allows a user to click an "Add File" field to create as many input fields as s/he likes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Uploading Files in jsp
 
Similar Threads
downloading all the files in a folder through ftp
Regarding FTP froma remote server
Doing ftp from a JSP
FTP the files
how to copy files form server to local path?