<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[JavaRanch: Latest posts for the topic "Outputstream the file through http response"]]></title>
		<link>http://www.coderanch.com/forums/t/38/Streams/Outputstream-file-through-http-response</link>
		<description><![CDATA[Latest messages posted in the topic "Outputstream the file through http response"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>Outputstream the file through http response</title>
				<description><![CDATA[I have the requirement like this<br /> 1) I have to create a file which would contain data from DB<br /> 2) and outputstream this file to http response.<br /> <br /> eg: if the request is "http://localhost/abcd/product.csv "<br />   then the user should save the file from his browser.<br /> <br /> I have never worked on IO object  and I have little knowledge about <a href="http://java.sun.com/javase/6/docs/api/java/io/FileWriter.html" class="api" title="Java API" target="_new" rel="nofollow">FileWriter</a>, OutputStreams. Not sure If I have to use Objectoutstream for this purpose ?.<br /> <br /> I could manage Mime type, buffer and other properties.<br /> <br /> I would really appreciate if some one could help me on this.<br /> <br /> <br /> <br /> <br /> <br /> ]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/451173/2008905</guid>
				<link>http://www.coderanch.com/forums/posts/preList/451173/2008905</link>
				<pubDate><![CDATA[Wed, Jun 24 2009 14:09:38 MDT]]></pubDate>
				<author><![CDATA[Anandha Loganathan]]></author>
			</item>
			<item>
				<title>Outputstream the file through http response</title>
				<description><![CDATA[I found solution but Do you have an elegant way to do this code.  <br /> <br /> <pre>   	FileWriter sw= new FileWriter(&quot;products.csv&quot;);
	 		CSVWriter writer = new CSVWriter(sw, '\u0009');
	 		for(int i=0;i &lt;l.size();i++){
				Object[] obj=(Object[])l.get(i);
				writer.writeNext(convertObjectToString(obj));
		  	}
	 	 
	 		sw.close();
	 		
	 		FileInputStream fis = new FileInputStream(&quot;products.csv&quot;);
			byte b[];
			int x = fis.available();
			b = new byte[x];
			System.out.println(&quot; b size&quot;+b.length);
			fis.read(b);
			
			// FIXME: this is ugly
			if (response != null)
				response.setContentType(mimeType);
			
			OutputStream os = response.getOutputStream();
	        os.write(b);
	        os.flush();	
 </pre><br /> 	 <br /> &gt;]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/451173/2008946</guid>
				<link>http://www.coderanch.com/forums/posts/preList/451173/2008946</link>
				<pubDate><![CDATA[Wed, Jun 24 2009 15:12:16 MDT]]></pubDate>
				<author><![CDATA[Anandha Loganathan]]></author>
			</item>
			<item>
				<title>Outputstream the file through http response</title>
				<description><![CDATA[That looks about right to me. It a good idea to set the content length of the response as well. I've run into trouble with that before, but it was only when sending the response to a non-standard browser. The normal browsers seemed happy enough with the 0 content length. <br /> <br /> If you expect the file to be large, you could read it a chunk at a time and write each chunk immediately to the output stream. That avoids having the whole file resident in a byte array. Actually, unless you need the file for other reasons, you could just let the CSVWriter() write it directly to the servlet output stream (well, through a <a href="http://java.sun.com/javase/6/docs/api/java/io/OutputStreamWriter.html" class="api" title="Java API" target="_new" rel="nofollow">OutputStreamWriter</a> anyway). In that case, you probably won't know the size though, so you have to hope a 0 content length won't freak out your client.]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/451173/2009136</guid>
				<link>http://www.coderanch.com/forums/posts/preList/451173/2009136</link>
				<pubDate><![CDATA[Thu, Jun 25 2009 00:36:07 MDT]]></pubDate>
				<author><![CDATA[Greg Charles]]></author>
			</item>
			<item>
				<title>Outputstream the file through http response</title>
				<description><![CDATA[I couldn't get you. How do we use <a href="http://java.sun.com/javase/6/docs/api/java/io/OutputStreamWriter.html" class="api" title="Java API" target="_new" rel="nofollow">OutputStreamWriter</a>?.<br /> <br /> If you could provide me the example code then I would appreciate that.<br /> <br /> Thanks<br /> Ananda]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/451173/2010467</guid>
				<link>http://www.coderanch.com/forums/posts/preList/451173/2010467</link>
				<pubDate><![CDATA[Fri, Jun 26 2009 12:38:49 MDT]]></pubDate>
				<author><![CDATA[Anandha Loganathan]]></author>
			</item>
			<item>
				<title>Outputstream the file through http response</title>
				<description><![CDATA[Oh, I just meant you needed an <a href="http://java.sun.com/javase/6/docs/api/java/io/OutputStreamWriter.html" class="api" title="Java API" target="_new" rel="nofollow">OutputStreamWriter</a> to fit your interface. Something like this:<br /> <br /> <pre>    
		response.setContentType(mimeType);
		
		OutputStream os = response.getOutputStream();
 		CSVWriter writer = new CSVWriter(new OutputStreamWriter(os), '\u0009');
 		for(int i=0;i &lt;l.size();i++){
			Object[] obj=(Object[])l.get(i);
			writer.writeNext(convertObjectToString(obj));
	  	}
 	 
 		writer.flush();

</pre>&gt;]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/451173/2010526</guid>
				<link>http://www.coderanch.com/forums/posts/preList/451173/2010526</link>
				<pubDate><![CDATA[Fri, Jun 26 2009 14:15:00 MDT]]></pubDate>
				<author><![CDATA[Greg Charles]]></author>
			</item>
			<item>
				<title>Outputstream the file through http response</title>
				<description><![CDATA[Wrote the code in a better way.<br /> <pre>  	 		response.setContentType(mimeType);
 	 		OutputStream fout= response.getOutputStream();
 	        OutputStream bos = new BufferedOutputStream(fout); 
  			OutputStreamWriter       outputwriter       = new OutputStreamWriter(bos);
 	 		 
  			CSVWriter writer = new CSVWriter(outputwriter);
	 		 for(int i=1;i &lt;l.size();i++){	 		 
  	 			Object[] obj=(Object[])l.get(i);
				if(obj!=null){
 					writer.writeNext(convertObjectToString(obj));
  				}				
 	 		} 	

	 		 
	 		outputwriter.flush(); 
	 		outputwriter.close();   </pre><br /> &gt;]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/451173/2010529</guid>
				<link>http://www.coderanch.com/forums/posts/preList/451173/2010529</link>
				<pubDate><![CDATA[Fri, Jun 26 2009 14:24:42 MDT]]></pubDate>
				<author><![CDATA[Anandha Loganathan]]></author>
			</item>
			<item>
				<title>Outputstream the file through http response</title>
				<description><![CDATA[Greg thanks for your help.<br /> <br /> ]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/451173/2010544</guid>
				<link>http://www.coderanch.com/forums/posts/preList/451173/2010544</link>
				<pubDate><![CDATA[Fri, Jun 26 2009 15:14:25 MDT]]></pubDate>
				<author><![CDATA[Anandha Loganathan]]></author>
			</item>
			<item>
				<title>Outputstream the file through http response</title>
				<description><![CDATA[You're welcome. IIRC,  you don't want to close the servlet output stream though. Your close() call will chain all the way through. If it doesn't cause you any problems though, I guess it's OK.]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/451173/2010654</guid>
				<link>http://www.coderanch.com/forums/posts/preList/451173/2010654</link>
				<pubDate><![CDATA[Fri, Jun 26 2009 23:18:04 MDT]]></pubDate>
				<author><![CDATA[Greg Charles]]></author>
			</item>
	</channel>
</rss>
