Re: URLConnection.getOutputStream().write() fails
Did you try URLConnection.setDoOutput(true)? Ronald. On Wed Aug 03 23:52:45 CEST 2005 Tomcat Users List tomcat-user@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.txthttp://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='[EMAIL PROTECTED]' -- 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
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.txthttp://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='[EMAIL PROTECTED]' -- 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
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.txthttp://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/sample.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='[EMAIL PROTECTED]' -- 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 -- 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
Not sure what you are trying to do here, but what you are doing is attempting to POST to a text file. One your TC server is probably only allowing a GET on the .txt file as it will not process the .txt file, and two you were probably wanting to perform a PUT command(???), but you need to do things differently all the way around. Were you trying to use the PUT command? Were you trying to make POSTs? Wade --- Julian Salerno [EMAIL PROTECTED] 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.txthttp://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/sample.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='[EMAIL PROTECTED]' -- 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 -- 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 - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: FW: URLConnection.getOutputStream().write() fails
Hi Wade :) Not sure what you are trying to do here, but what you are doing is attempting to POST to a text file. Correct. One your TC server is probably only allowing a GET on the .txt file as it will not process the .txt file, So, as a Tomcat newbie, is there a configuration I need to make at the server to allow posts to this directory ? And, do I need to set a request property on the URLConnection to tell the server that I want to do this ? and two you were probably wanting to perform a PUT command(???), but you need to do things differently all the way around. Were you trying to use the PUT command? Were you trying to make POSTs? The file already exists (It is created by another process on the server). I wish to POST (not PUT) data so that the resource (sample.txt) is updated with the contents I write from the client. Thanks Julian - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: FW: URLConnection.getOutputStream().write() fails
Hi Wade :) Not sure what you are trying to do here, but what you are doing is attempting to POST to a text file. Correct. One your TC server is probably only allowing a GET on the .txt file as it will not process the .txt file, So, as a Tomcat newbie, is there a configuration I need to make at the server to allow posts to this directory ? And, do I need to set a request property on the URLConnection to tell the server that I want to do this ? and two you were probably wanting to perform a PUT command(???), but you need to do things differently all the way around. Were you trying to use the PUT command? Were you trying to make POSTs? The file already exists (It is created by another process on the server). I wish to POST (not PUT) data so that the resource (sample.txt) is updated with the contents I write from the client. Thanks Julian - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: FW: URLConnection.getOutputStream().write() fails
Yes this is a PUT actually. A POST will simply send parameters to the server as a stream with the server some how making a process operate on the POST info (usually the file POST to such as a .jsp or a .php file) unless you have a special servlet for taking a POST and dumping the input stream to file and you stream up data directly. A PUT will actually put the contents directly to the file (or is the HTTP spec operation for it), and you have to tell the server you are attempting a PUT command. Using the URLConnection it isn't directly straight forward, and I would recommend using the Apache Commons HTTPClient to do this. And, yes you need to usually tell tomcat to allow PUTs as I am pretty sure by default this is turned offnot sure...but it's handled by the default servlet in Tomcat, or by any servlet you may want to setupbut for the default servlet basically you can control access in web.xml for your web application using a security constraintsee the servlet specification at url http://www.jcp.org/aboutJava/communityprocess/final/jsr053/ example from file.you won't need so much stuff... security-constraint web-resource-collection web-resource-nameSalesInfo /web-resource-name url-pattern/salesinfo/*/url-pattern http-methodGET/http-method http-methodPOST/http-method /web-resource-collection auth-constraint role-namemanager/role-name /auth-constraint user-data-constraint transport-guaranteeCONFIDENTIAL /transport-guarantee /user-data-constraint /security-constraint You should get the idea.you'll have a http-methodPUT/http-method and this can be used to control access to overwrite the information by certain users and not just everyone.good idea...for certain sections of your web applications and their data. See the servlet spec (PDF) and read about Realms and security in the Tomcat docs, and you should be able to get this going. I hope that helps some, Wade --- [EMAIL PROTECTED] wrote: Hi Wade :) Not sure what you are trying to do here, but what you are doing is attempting to POST to a text file. Correct. One your TC server is probably only allowing a GET on the .txt file as it will not process the .txt file, So, as a Tomcat newbie, is there a configuration I need to make at the server to allow posts to this directory ? And, do I need to set a request property on the URLConnection to tell the server that I want to do this ? and two you were probably wanting to perform a PUT command(???), but you need to do things differently all the way around. Were you trying to use the PUT command? Were you trying to make POSTs? The file already exists (It is created by another process on the server). I wish to POST (not PUT) data so that the resource (sample.txt) is updated with the contents I write from the client. Thanks Julian - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: FW: URLConnection.getOutputStream().write() fails
You can also learn more about the default servlet here: http://jakarta.apache.org/tomcat/tomcat-5.5-doc/catalina/funcspecs/fs-default.html Hope it helps, Wade --- [EMAIL PROTECTED] wrote: Hi Wade :) Not sure what you are trying to do here, but what you are doing is attempting to POST to a text file. Correct. One your TC server is probably only allowing a GET on the .txt file as it will not process the .txt file, So, as a Tomcat newbie, is there a configuration I need to make at the server to allow posts to this directory ? And, do I need to set a request property on the URLConnection to tell the server that I want to do this ? and two you were probably wanting to perform a PUT command(???), but you need to do things differently all the way around. Were you trying to use the PUT command? Were you trying to make POSTs? The file already exists (It is created by another process on the server). I wish to POST (not PUT) data so that the resource (sample.txt) is updated with the contents I write from the client. Thanks Julian - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]