Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Applet + write to file

0 views
Skip to first unread message

Henrik Kier

unread,
May 20, 2002, 10:26:13 AM5/20/02
to
Hello

I am trying to write to a file located on the server from an applet.

The code I am trying to do this with is:

try{
URL url1 = new URL(proto,host,file);
URLConnection urlcon1 = url1.openConnection();
urlcon1.setDoInput(true);
urlcon1.setDoOutput(true);
urlcon1.setUseCaches(false);
urlcon1.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
DataOutputStream dos = new
DataOutputStream(urlcon1.getOutputStream());
dos.writeBytes(str);
dos.flush();
dos.close();
} catch (Exception ex)
{
ex.printStackTrace();
}
}

but when I run this on the server it just says opening file and it just
stays tha way, it newer opens??

Can someone help me

Henrik


Jon Skeet

unread,
May 20, 2002, 10:45:25 AM5/20/02
to

You need to actually make the connection, either with connect() or by
getting the input stream. I suggest getting the input stream and just
closing it.

--
Jon Skeet - <sk...@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Henrik Kier

unread,
May 20, 2002, 11:09:05 AM5/20/02
to
Hey Jon

now I am writing it like this:

try{
URL url1 = new URL(proto,host,fil);


URLConnection urlcon1 = url1.openConnection();
urlcon1.setDoInput(true);
urlcon1.setDoOutput(true);
urlcon1.setUseCaches(false);
urlcon1.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");

DataInputStream dis = new DataInputStream(urlcon1.getInputStream());
dis.close();


DataOutputStream dos = new
DataOutputStream(urlcon1.getOutputStream());
dos.writeBytes(str);
dos.flush();
dos.close();
} catch (Exception ex)
{
ex.printStackTrace();
}
}

and I am getting an java.io.IOException: Connection failure with 400

Do anyone hown what I am doing wrong??

"Jon Skeet" <sk...@pobox.com> skrev i en meddelelse
news:MPG.17531c5ef...@dnews.peramon.com...

Henrik Kier

unread,
May 20, 2002, 11:22:29 AM5/20/02
to
Hello

Now I fund the firste error - but I stil can't get the file whit this code:

try{
URL url1 = new URL(proto,host,fil);


URLConnection urlcon1 = url1.openConnection();
urlcon1.setDoInput(true);
urlcon1.setDoOutput(true);
urlcon1.setUseCaches(false);
urlcon1.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");

DataInputStream dis = new DataInputStream(urlcon1.getInputStream());
dis.close();

DataOutputStream dos = new
DataOutputStream(urlcon1.getOutputStream());
dos.writeBytes(str);
dos.flush();
dos.close();
} catch (Exception ex)
{
ex.printStackTrace();
}

I am getting the error: "java.net.ProtocolException: Can't reset method:
already connected" in line:

DataOutputStream dos = new DataOutputStream(urlcon1.getOutputStream());

can someone help me????

"Henrik Kier" <hek...@mail.dk> skrev i en meddelelse
news:3ce90783$0$97283$edfa...@dspool01.news.tele.dk...

Jon Skeet

unread,
May 20, 2002, 11:44:28 AM5/20/02
to
Henrik Kier <hek...@mail.dk> wrote:
> Hey Jon
>
> now I am writing it like this:
>
> try{
> URL url1 = new URL(proto,host,fil);
> URLConnection urlcon1 = url1.openConnection();
> urlcon1.setDoInput(true);
> urlcon1.setDoOutput(true);
> urlcon1.setUseCaches(false);
> urlcon1.setRequestProperty("Content-Type",
> "application/x-www-form-urlencoded");
> DataInputStream dis = new DataInputStream(urlcon1.getInputStream());
> dis.close();
> DataOutputStream dos = new
> DataOutputStream(urlcon1.getOutputStream());
> dos.writeBytes(str);
> dos.flush();
> dos.close();
> } catch (Exception ex)
> {
> ex.printStackTrace();
> }
> }
>
> and I am getting an java.io.IOException: Connection failure with 400
>
> Do anyone hown what I am doing wrong??

You've got to get the input stream *after* the output stream - you've
got to write the complete request before you can get the response!

Henrik Kier

unread,
May 20, 2002, 4:59:33 PM5/20/02
to
Hallo

Now I have don that and I am still getting no way.

The code is like this now:

try{
URL url1 = new URL(proto,host,filx);


URLConnection urlcon1 = url1.openConnection();
urlcon1.setDoInput(true);
urlcon1.setDoOutput(true);
urlcon1.setUseCaches(false);
urlcon1.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");

DataOutputStream dos = new
DataOutputStream(urlcon1.getOutputStream());
dos.writeBytes(str);
dos.flush();
dos.close();

DataInputStream dis = new DataInputStream(urlcon1.getInputStream());

String s1 = dis.readLine();
if(dis != null && Integer.parseInt(s1) >= 0)
System.out.print("we make it" + Integer.parseInt(s1));
else
System.out.print("we did't make it");
dis.close();
}catch (Exception ex)
{
ex.printStackTrace();
}

And the file I am trying to get is set to chmod 777 - but now I am getting
this error:
java.io.IOException: Connection failure with 405
at
sun.plugin.protocol.jdk12.http.HttpURLConnection.getInputStream(Unknown
Source)
in the line:


DataInputStream dis = new DataInputStream(urlcon1.getInputStream());

Can someone help me??

Henrik


"Jon Skeet" <sk...@pobox.com> skrev i en meddelelse

news:MPG.17532a366...@dnews.peramon.com...

Jon Skeet

unread,
May 21, 2002, 2:22:52 AM5/21/02
to
Henrik Kier <hek...@mail.dk> wrote:
> Now I have don that and I am still getting no way.

What appears in the logs at the server end? I can't remember off-hand
what HTTP code 405 means - I suggest you look it up in the HTTP RFCs.

Henrik Kier

unread,
May 22, 2002, 1:45:41 PM5/22/02
to
hallo

It means "405 Method Not Allowed" and it in line:

"DataInputStream dis = new DataInputStream(urlcon1.getInputStream());"

please help me!!!


"Jon Skeet" <sk...@pobox.com> skrev i en meddelelse

news:MPG.1753f8175...@dnews.peramon.com...

Jon Skeet

unread,
May 22, 2002, 5:03:37 PM5/22/02
to
Henrik Kier <hek...@mail.dk> wrote:
> It means "405 Method Not Allowed" and it in line:

That probably means it's using GET instead of POST or vice versa.

> "DataInputStream dis = new DataInputStream(urlcon1.getInputStream());"
>
> please help me!!!

And what appears in the logs at the server end (as I asked before)?

Henrik Kier

unread,
May 23, 2002, 12:35:41 PM5/23/02
to
Hey Jon

Here is what the log at the server said:

in error log whas this linie many times when I tryede to acceses the file:
[Thu May 23 18:26:37 2002] [error] (32557)Socket is not connected:
setsockopt(SO_UPDATE_ACCEPT_CONTEXT) failed.

And the acces log said:
127.0.0.1 - - [23/May/2002:18:34:33 +0100] "POST /enarm/classes/point.hk
HTTP/1.0" 200 0

I am runing it on an apache server version 2.0.28

Can you help me??

"Jon Skeet" <sk...@pobox.com> skrev i en meddelelse

news:MPG.175618048...@dnews.peramon.com...

Jon Skeet

unread,
May 23, 2002, 12:48:27 PM5/23/02
to
Henrik Kier <hek...@mail.dk> wrote:
> Here is what the log at the server said:
>
> in error log whas this linie many times when I tryede to acceses the file:
> [Thu May 23 18:26:37 2002] [error] (32557)Socket is not connected:
> setsockopt(SO_UPDATE_ACCEPT_CONTEXT) failed.

Hmm... that's odd.

> And the acces log said:
> 127.0.0.1 - - [23/May/2002:18:34:33 +0100] "POST /enarm/classes/point.hk
> HTTP/1.0" 200 0
>
> I am runing it on an apache server version 2.0.28

And does your servlet have a doGet method?

Henrik Kier

unread,
May 23, 2002, 2:00:16 PM5/23/02
to
Hey Jon

I am not using an servlet but an applet - and no I am it has't got an doGet
method

if I shoud have an doGet method - how do I write that line of code??


"Jon Skeet" <sk...@pobox.com> skrev i en meddelelse

news:MPG.17572dbab...@dnews.peramon.com...

Jon Skeet

unread,
May 23, 2002, 2:23:43 PM5/23/02
to
Henrik Kier <hek...@mail.dk> wrote:
> I am not using an servlet but an applet - and no I am it has't got
> an doGet method

Your applet wouldn't have a doGet method, obviously - but does the
server page you're after accept post request (like the one you're
making)?

0 new messages