> Programming Languages > Java
Various Topics Home | Disclaimer | Report Adult Posts

Various Topics on Java



Java - "Writing to a URL???" in Programming Languages


Old 09-05-2004   #1
.... ..t..
 
Default Re: Writing to a URL???

Aviv Gurwitz wrote:

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


Don't multi-post.

--
Paul Lutus
http://www.arachnoid.com

 
Old 09-05-2004   #2
.... ..rwi..
 
Default Writing to a URL???

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.getOutputStrea m());
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.


 
Old 09-05-2004   #3
.... ..mphr..
 
Default Re: Writing to a URL???


"Aviv Gurwitz" <temp1604@hotmail.com> wrote in message
news:413a3dc4$1@news.012.net.il...
> 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.getOutputStrea m());
> 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 matth@ivizNOSPAM.com http://www.iviz.com/


 
Old 09-05-2004   #4
..c.. ....
 
Default Re: Writing to a URL???

Aviv Gurwitz <temp1604@hotmail.com> wrote:
> 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.getOutputStrea m());
> 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".


--
Oscar Kind http://home.hccnet.nl/okind/
Software Developer for contact information, see website

PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
 
Old 09-05-2004   #5
....
 
Default Re: Writing to a URL???

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.

"Matt Humphrey" <matth@ivizNOSPAM.com> wrote in message
news:QpGdnTglNpnn_qfcRVn-hg@adelphia.com...
>
> "Aviv Gurwitz" <temp1604@hotmail.com> wrote in message
> news:413a3dc4$1@news.012.net.il...
> > 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.getOutputStrea m());
> > 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 matth@ivizNOSPAM.com http://www.iviz.com/
>
>



 
Old 09-05-2004   #6
.... ..mphr..
 
Default Re: Writing to a URL???


"Aviv" <me@here.com> wrote in message news:cheu0o$muf$1@news.iucc.ac.il...
> Hi again,
> I managed to find an article on writing to files and I copied and ran from
> it the following code:


<snip 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 matth@ivizNOSPAM.com http://www.iviz.com/


 

Thread Tools
Display Modes





Powered by vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0