You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Julian Salerno <ju...@practica.com.au> on 2005/08/03 23:52:45 UTC

URLConnection.getOutputStream().write() fails

PROBLEM:

Trying to write to URL via URLConnection.getOutputStream().write() fails.

 

In the main method below, the code attempts to write some bytes to HYPERLINK
"http://localhost:8080/temp/sample.txt"http://localhost:8080/temp/sample.txt
.

"http://localhost:8080/temp/sample.txt" is resolvable in a web browser and
actually points to:

${tomcat.home}/server/webapps/ROOT/temp/samples.txt

 

Executing main does not result in any bytes being written.

Any input appreciated, thanks :-)

 

SERVER:

Apache Tomcat/4.0.1

Windows XP using Java 1.4.2_06

 

CLIENT CODE:

Windows XP using Java 1.4.2_06

 

      public static void main(String[] args)

      {

            OutputStream os = null;

            try

            {

                  final String string =
"http://localhost:8080/temp/sample.txt";

                  URL url = new URL(string);

                  final URLConnection conn = url.openConnection();

                  System.out.println("openConnection="+conn);

                  boolean doOutput = conn.getDoOutput();

                  conn.setDoOutput(true);

                  conn.setDoInput(true);

                  conn.setUseCaches(false);

                  conn.setAllowUserInteraction(true);

                  doOutput = conn.getDoOutput();

 

                  System.out.println("doOutput is enabled:"+doOutput);

                  

                  final String data = "Hello World";

                  conn.setRequestProperty("Content-length",
String.valueOf(data.length()));

                   

                  os = conn.getOutputStream();

                  final BufferedOutputStream bos = new
BufferedOutputStream(os);

                  System.out.println("bos='"+bos+"'");

                  //now write some bytes

                  final byte[] bytes = data.getBytes();

                  bos.write(bytes);

            }

            catch (Exception e)

            {

                  e.printStackTrace();

            }

            finally

            {

                  if(os != null)

                  {

                        try

                        {

                              os.flush();

                              os.close();

                        }

                        catch (IOException e)

                        {

                              e.printStackTrace();

                        }

                  }

            }

      }

 

CLIENT-OUTPUT:

openConnection=sun.net.www.protocol.http.HttpURLConnection:http://localhost:
8080/temp/sample.txt

doOutput is enabled:true

bos='java.io.BufferedOutputStream@18a7efd'

 

 


-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.338 / Virus Database: 267.9.9/62 - Release Date: 8/2/2005
 

Re: URLConnection.getOutputStream().write() fails

Posted by Ronald Klop <ro...@base.nl>.
Did you try URLConnection.setDoOutput(true)?

Ronald.

On Wed Aug 03 23:52:45 CEST 2005 Tomcat Users List <to...@jakarta.apache.org> wrote:
> PROBLEM:
> 
> Trying to write to URL via URLConnection.getOutputStream().write() fails.
> 
> 
> 
> In the main method below, the code attempts to write some bytes to HYPERLINK
> "http://localhost:8080/temp/sample.txt"http://localhost:8080/temp/sample.txt
> .
> 
> "http://localhost:8080/temp/sample.txt" is resolvable in a web browser and
> actually points to:
> 
> ${tomcat.home}/server/webapps/ROOT/temp/samples.txt
> 
> 
> 
> Executing main does not result in any bytes being written.
> 
> Any input appreciated, thanks :-)
> 
> 
> 
> SERVER:
> 
> Apache Tomcat/4.0.1
> 
> Windows XP using Java 1.4.2_06
> 
> 
> 
> CLIENT CODE:
> 
> Windows XP using Java 1.4.2_06
> 
> 
> 
> public static void main(String[] args)
> 
> {
> 
> OutputStream os = null;
> 
> try
> 
> {
> 
> final String string =
> "http://localhost:8080/temp/sample.txt";
> 
> URL url = new URL(string);
> 
> final URLConnection conn = url.openConnection();
> 
> System.out.println("openConnection="+conn);
> 
> boolean doOutput = conn.getDoOutput();
> 
> conn.setDoOutput(true);
> 
> conn.setDoInput(true);
> 
> conn.setUseCaches(false);
> 
> conn.setAllowUserInteraction(true);
> 
> doOutput = conn.getDoOutput();
> 
> 
> 
> System.out.println("doOutput is enabled:"+doOutput);
> 
> 
> 
> final String data = "Hello World";
> 
> conn.setRequestProperty("Content-length",
> String.valueOf(data.length()));
> 
> 
> 
> os = conn.getOutputStream();
> 
> final BufferedOutputStream bos = new
> BufferedOutputStream(os);
> 
> System.out.println("bos='"+bos+"'");
> 
> //now write some bytes
> 
> final byte[] bytes = data.getBytes();
> 
> bos.write(bytes);
> 
> }
> 
> catch (Exception e)
> 
> {
> 
> e.printStackTrace();
> 
> }
> 
> finally
> 
> {
> 
> if(os != null)
> 
> {
> 
> try
> 
> {
> 
> os.flush();
> 
> os.close();
> 
> }
> 
> catch (IOException e)
> 
> {
> 
> e.printStackTrace();
> 
> }
> 
> }
> 
> }
> 
> }
> 
> 
> 
> CLIENT-OUTPUT:
> 
> openConnection=sun.net.www.protocol.http.HttpURLConnection:http://localhost:
> 8080/temp/sample.txt
> 
> doOutput is enabled:true
> 
> bos='java.io.BufferedOutputStream@18a7efd'
> 
> 
> 
> 
> 
> 
> -- 
> No virus found in this outgoing message.
> Checked by AVG Anti-Virus.
> Version: 7.0.338 / Virus Database: 267.9.9/62 - Release Date: 8/2/2005
>