Writing to a URL???

A

Aviv Gurwitz

Hello,
I am trying to use the following code to write my data to my applet's home
URL on my server:

try
{
URL colorsURL = new URL(getCodeBase() + "data/colors.dat");
URLConnection colorsConnection = colorsURL.openConnection();
colorsConnection.setDoOutput(true);
ObjectOutputStream colorsStream = new
ObjectOutputStream(colorsConnection.getOutputStream());
colorsStream.writeObject(jBoard.getColors());
colorsStream.flush();
colorsStream.close();
}
catch (MalformedURLException mue)
{
System.out.println(mue.getMessage());
}
catch (IOException ie)
{
System.out.println(ie.getMessage());
}

The code compiles properly and does not generate any runtime error or caught
exception error message in the Sun Java Console window (when run on my
server).
When run on my local computer I get the caught runtime error message:
"protocol doesn't support output". Here I'm not surprised.
But why does the code run well on the server and yet there is no colors.dat
file to be found in the data directory on the server?

Thanks for any ideas,
Aviv.
 
M

Matt Humphrey

Aviv Gurwitz said:
Hello,
I am trying to use the following code to write my data to my applet's home
URL on my server:

try
{
URL colorsURL = new URL(getCodeBase() + "data/colors.dat");
URLConnection colorsConnection = colorsURL.openConnection();
colorsConnection.setDoOutput(true);
ObjectOutputStream colorsStream = new
ObjectOutputStream(colorsConnection.getOutputStream());
colorsStream.writeObject(jBoard.getColors());
colorsStream.flush();
colorsStream.close();
}
catch (MalformedURLException mue)
{
System.out.println(mue.getMessage());
}
catch (IOException ie)
{
System.out.println(ie.getMessage());
}

The code compiles properly and does not generate any runtime error or caught
exception error message in the Sun Java Console window (when run on my
server).
When run on my local computer I get the caught runtime error message:
"protocol doesn't support output". Here I'm not surprised.
But why does the code run well on the server and yet there is no colors.dat
file to be found in the data directory on the server?

The first thing you need to realize when you are having your applet write a
file back to your web server is that your web server is not a file server
and there is no presumed protocol available for writing files. You cannot
simply
open a URL for writing and put data into it. The
filepaths you think you see in URLs are (at best) read-only and at worst not
related to the server's file system. Rather, web servers respond to
requests and you must make an HTTP POST request that contains the file
contents and have a servlet or some small script put that into your
filesystem. This is not done with Files, but with URLs and HTTP requests.
(Alternatively, you can arrange for other techniques to transfer files such
as FTP and SCP / SSH, but ultimately you must provide the receiver and a
corresponding sender.) This is a common request, so search Google and the
news groups again for information on how to form an HTTP Post request.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
O

Oscar kind

Aviv Gurwitz said:
Hello,
I am trying to use the following code to write my data to my applet's home
URL on my server:

try
{
URL colorsURL = new URL(getCodeBase() + "data/colors.dat");
URLConnection colorsConnection = colorsURL.openConnection();
colorsConnection.setDoOutput(true);
ObjectOutputStream colorsStream = new
ObjectOutputStream(colorsConnection.getOutputStream());
colorsStream.writeObject(jBoard.getColors());
colorsStream.flush();
colorsStream.close();
}
[...]

Without specifying the HTTP method, you're likely to issue a "get" request.
This is read-only. To actually write a file to a URL, you have two options:

- Install a servlet that writes the file for you, and have it listen to
either "post" or "put" requests.

- Use a webserver that can handle "put" requests.

In both cases, you'll have to set the HTTP request method, using
HttpURLConnection#setRequestMethod(String)

When run on my local computer I get the caught runtime error message:
"protocol doesn't support output". Here I'm not surprised.
But why does the code run well on the server and yet there is no colors.dat
file to be found in the data directory on the server?

It is entirely possible that the HTTP implementation doesn't allow writing
to a URL, but I doubt that; I've created successful HTTP "put" requests.

It's more likely that writing is not allowed because the request method
defaults to "get".
 
A

Aviv

Hi again,
I managed to find an article on writing to files and I copied and ran from
it the following code:

try
{
String data = "Hello World";
System.out.println("1");
URL url = new URL(getCodeBase() + "cgi-bin/wdwrite");
System.out.println("2");
URLConnection con = url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setUseCaches(false);
con.setRequestProperty("Content-type", "text/plain");
con.setRequestProperty("Content-length", data.length()+"");
PrintStream out = new PrintStream(con.getOutputStream());
out.print(data);
out.flush();
out.close();
DataInputStream in =
new DataInputStream(con.getInputStream());
int i;
while ((i = in.read()) != -1)
{
System.out.println((char)i);
}
in.close();

}
catch (MalformedURLException mue)
{
System.out.println(mue.getMessage());
}
catch (IOException ie)
{
System.out.println(ie.getMessage());
}

also, the accompaning CGI script is:

#!/usr/bin/perl
# wdwrite
# this is the CGI that will take the # applet info and write it to a file

open(OUT, "> hello.txt");
print "content-type: text/plain\n\n";

while (<>) {
print OUT $_;
print $_;
}
close (OUT);
exit 0;

I placed the cgi script in a cgi-bin folder in my home directory under my
html directory and my applets directory. Yet when I run the applet it runs
smoothly with no exceptions and yet there is no file to be found in the
cgi-bin directory...
What am I doing wrong?

thanks,
Aviv.
 
M

Matt Humphrey

Aviv said:
Hi again,
I managed to find an article on writing to files and I copied and ran from
it the following code:

I placed the cgi script in a cgi-bin folder in my home directory under my
html directory and my applets directory. Yet when I run the applet it runs
smoothly with no exceptions and yet there is no file to be found in the
cgi-bin directory...
What am I doing wrong?

Ok, you're on the right track. All HTTP requests produce a respose. What
does the response say? Is the cgi being run at all? Because you're really
performing a POST request via Java you can test your CGI by producing a
simple HTML web page that does the post directly and automatically shows you
the response. Try that and see what you get. (Also, Oscar's comment also
applies--are you setting the request type to POST?)

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top