Google 網路論壇不再支援新的 Usenet 貼文或訂閱項目,但過往內容仍可供查看。

Applet + write to file

瀏覽次數:0 次
跳到第一則未讀訊息

Henrik Kier

未讀,
2002年5月20日 上午10:26:132002/5/20
收件者:
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

未讀,
2002年5月20日 上午10:45:252002/5/20
收件者:

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

未讀,
2002年5月20日 上午11:09:052002/5/20
收件者:
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

未讀,
2002年5月20日 上午11:22:292002/5/20
收件者:
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

未讀,
2002年5月20日 上午11:44:282002/5/20
收件者:
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

未讀,
2002年5月20日 下午4:59:332002/5/20
收件者:
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

未讀,
2002年5月21日 凌晨2:22:522002/5/21
收件者:
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

未讀,
2002年5月22日 下午1:45:412002/5/22
收件者:
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

未讀,
2002年5月22日 下午5:03:372002/5/22
收件者:
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

未讀,
2002年5月23日 中午12:35:412002/5/23
收件者:
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

未讀,
2002年5月23日 中午12:48:272002/5/23
收件者:
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

未讀,
2002年5月23日 下午2:00:162002/5/23
收件者:
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

未讀,
2002年5月23日 下午2:23:432002/5/23
收件者:
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 則新訊息