Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java Applet File Export

Status
Not open for further replies.

alexkillby

Technical User
Mar 29, 2005
3
CA
Hi,
I have got the concept down of reading text-based files from a webserver. The next task I need to accomplish is writing a file to the webserver, on the internet the most I can research is the code needed and they tell you that you need a CGI script on the server to HTTP-POST the data to, which will in turn create the file on the server. However, I can not find this downloadble CGI script. Can anyone help?
 
Are you asking :

1) How to I put/write a CGI script on a web server so I can upload data ?

or

2) How to I upload data to a web server resource from an Applet ?

Or both ? If #1 - then knowing your web server may help, and also, you may find it of more use to post in the CGI forum !

--------------------------------------------------
Free Database Connection Pooling Software
 
All i want to do is export a text file from an applet on my webserver. On the internet it says you need to post the data FROM the applet TO a CGI SCRIPT, which will create the file on the server.
 
Thats not really answering my two questions though is it - but just reiterating your original post.

Please re-read my reply to you earlier, and post the relevant answers ...

--------------------------------------------------
Free Database Connection Pooling Software
 
I know how to put the script on the server, thats not what I was asking. I just need to find a script to use and export text files to it from an applet, creatint the file on the server.
 
Again you fail to answer the questions being asked of you. It is impossible to help you if you are not willing to listen to the people trying to help you !

How you write your CGI or whatever is up to you - this is a Java forum , not a CGI forum . Considering you don't even say what operating system, or web server you are using, this is a moot point.

In any case, once you have some resource on the web server that will recieve data, this is how you send data to a URL using Java.

Code:
File f = new File("somefile.txt");
FileInputStream fis = new FileInputStream(f);
byte[] data = new byte[(int)f.length()];
fis.read(data);
fis.close();

String u = "[URL unfurl="true"]http://www.yourdomain.com/someresource.whatever";[/URL]
URL url = new URL(u);
HttpURLConnection huc = (HttpURLConnection)url.openConnection();
huc.setRequestMethod("POST");
huc.setDoOutput(true);
huc.setUseCaches(false);
huc.setFollowRedirects(true);

DataOutputStream dos = new DataOutputStream(huc.getOutputStream());
dos.write(data, 0, data.length);
dos.flush();
dos.close();

huc.disconnect();

I seriously suggest you take time to review how you ask questions, and respond to queries, if you want any help in the future (not just on tek-tips, but in life).

Good Luck.



--------------------------------------------------
Free Database Connection Pooling Software
 
sedj - I was looking at your code that you posted and I'm trying to understand it (I'm still fairly new in Java...so please forgive me if this is a dumb quesiton...)

I don't understand what it is doing. What do you mean "this is how you send data to a URL using Java." where on the server is this data being sent to?
 
The URL should be a resource (be it something written in ASP, JSP, a servlet, ASP.NET, a DLL, PHP - whatever) that basically sits on a web server of some kind (be it Apache, IIS, Tomcat - whatever), and handles requests for sending data.

HTTP is a protocol built on top of TCP/IP - and so is basically just a raw socket with an extra protocol layer on top. So any resource that sites within a server that speaks HTTP can have access to the connection/socket's IO streams.

So this code basically establishes a connection, gets a handle to the output stream, and sends the data.

--------------------------------------------------
Free Database Connection Pooling Software
 
I know that this is not the php forum, but if I were to have a php script on the server handling the http-post, would I handle it the same as I would handle post from a form?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top