aspose file tools*
The moose likes JSP and the fly likes JSP uploading files & creating new files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP uploading files & creating new files" Watch "JSP uploading files & creating new files" New topic
Author

JSP uploading files & creating new files

Zein Nunna
Ranch Hand

Joined: Mar 31, 2005
Posts: 245
Hey guys,

Was just wandering if anyone could give advice on how I should

i) upload a file using a JSP (i.e. copying the file from the client to a specific location on the webserver) - I have the html etc.. in place, so i can browse the C: and get the right file path. The issue is when i click on 'upload', what do i need to do there? [I want to do this in my JSP - i know servelets are better, but i want to start using this method]

ii) Creating a new file on the application server - What I am trying to achive is to copy text from a text area (html/jsp), create a file, and then store this on the server in a specific location.

Do i make sense? any ideas? Links to tutorials/ sample code etc.. welcome,

Thanks in advance
Zein
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Zein Nunna:
Hey guys,

Was just wandering if anyone could give advice on how I should

i) upload a file using a JSP (i.e. copying the file from the client to a specific location on the webserver) - I have the html etc.. in place, so i can browse the C: and get the right file path. The issue is when i click on 'upload', what do i need to do there? [I want to do this in my JSP - i know servelets are better, but i want to start using this method]

ii) Creating a new file on the application server - What I am trying to achive is to copy text from a text area (html/jsp), create a file, and then store this on the server in a specific location.

Do i make sense? any ideas? Links to tutorials/ sample code etc.. welcome,

Thanks in advance
Zein


i) Look at http://jakarta.apache.org/commons/fileupload
Very easy to use.

ii) Look at java.util.File
http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html
Also very easy to use.


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

Joined: Mar 31, 2005
Posts: 245
Thanks Ben for the above.

I know how to create files locally, thats no problem, its just creating the file and then saving it on the server? Which doesnt work for some reason.

My method of doing that is the JSP creates an instance of a class (saved on the server) that creates/writes to the file at the the server base directory( namely "/".., for now anyway, later i will change this). But this doesnt work.

Also the the jakarta link you sent me, i have downloaded the zip file, what do i do with that, it has loads of html's there?

Sorry a big novice, i know.
Thanks Zein
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Zein Nunna:
I know how to create files locally, thats no problem, its just creating the file and then saving it on the server? Which doesnt work for some reason.

My method of doing that is the JSP creates an instance of a class (saved on the server) that creates/writes to the file at the the server base directory( namely "/".., for now anyway, later i will change this). But this doesnt work.


One thing to be aware of when working with J2EE apps is that Servlet containers are required to be able to run apps as "war" files. Most also give you the option to unpack the war file and run the app from an exploded file system.

When running from an unpacked war file, you're not going to be able to write to the file system under your web app's root because the web app doesn't exist on the file system. Also, using getRealPath() will return null.

The best way to insure portability is to write to and read from an absolute path on your file system that exists outside of your web app. Context Init Params and/or Servlet Init Params make it easy to configure the path.

Originally posted by Zein Nunna:
Also the the jakarta link you sent me, i have downloaded the zip file, what do i do with that, it has loads of html's there?
Sorry a big novice, i know.
Thanks Zein


No need to be sorry, but it would a waste of time and bandwidth for me to re-type the documentaion for fileupload here.

http://jakarta.apache.org/commons/fileupload/using.html
 
wood burning stoves
 
subject: JSP uploading files & creating new files