<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Java Interview Questions &#187; File Uploading using Java/Jsp</title>
	<atom:link href="http://www.bestjavainterviewquestions.com/category/jsp-inteview-questions/file-uploading-using-javajsp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bestjavainterviewquestions.com</link>
	<description>Java Interview Questions &#124; Core Java Interview Questions &#124; Advanced Java Interview Questions &#124; EJB Interview Questions &#124; J2EE Interview Questions &#124; Hibernate Interview Questions</description>
	<lastBuildDate>Fri, 03 Feb 2012 10:23:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Jsp Example: File Uploading</title>
		<link>http://www.bestjavainterviewquestions.com/jsp-example-file-uploading/</link>
		<comments>http://www.bestjavainterviewquestions.com/jsp-example-file-uploading/#comments</comments>
		<pubDate>Mon, 26 Jan 2009 08:07:11 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[File Uploading using Java/Jsp]]></category>
		<category><![CDATA[file upload]]></category>
		<category><![CDATA[file upload examples]]></category>
		<category><![CDATA[file upload using java]]></category>
		<category><![CDATA[file uploading example using jsp]]></category>
		<category><![CDATA[j2ee file upload]]></category>
		<category><![CDATA[j2ee file upload examples]]></category>
		<category><![CDATA[jsp]]></category>
		<category><![CDATA[jsp file upload]]></category>
		<category><![CDATA[jsp file upload examples]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=527</guid>
		<description><![CDATA[FileUploading Using JSP(JAVA) This is an example of the file Uploading in Java using JSP . This file uploading concept Works with the Java as follows This Java  API parses InputStream of HttpPost request with multipart/form-data encode. By this Java API, only the single method call is enough for your file upload using jsp.In  this [...]]]></description>
			<content:encoded><![CDATA[<p><span style="text-decoration: underline;"><strong>FileUploading Using JSP(JAVA)</strong></span></p>
<p>This is an example of the file Uploading in Java using JSP . This file uploading concept Works with the Java as follows</p>
<p>This Java  API parses <em>InputStream of HttpPost request with <strong>multipart/form-data encode</strong>.</em> By this Java API, only the single method call is enough for your file upload using jsp.In  this example the the file(image) will be saved in your server automatically after you run the application in the Server.</p>
<p>The server takes the default path as the destination if you not specified any path by hard coded.</p>
<p>Using the<em> multipart/form-data</em> you are saying to the server that the request is of type multipart and the data is encrypted data</p>
<p>Intially you have to design the  html file</p>
<p>1)First create the html file by using</p>
<p>&#8211;&gt; the file tag in the form attribute(&lt;input type=file name=file1&gt;<br />
&#8211;&gt;use the submit tag to send the request to the jsp page.</p>
<p><strong>name these page as the fileupload.html </strong></p>
<p><em>&lt;form method=&#8221;post&#8221; action=&#8221;/fileUpload.jsp&#8221; name=&#8221;upform&#8221; enctype=&#8221;multipart/form-data&#8221;&gt;<br />
&lt;table width=&#8221;60%&#8221; border=&#8221;0&#8243; cellspacing=&#8221;1&#8243; cellpadding=&#8221;1&#8243; align=&#8221;center&#8221; class=&#8221;style1&#8243;&gt;<br />
&lt;tr&gt;<br />
&lt;td align=&#8221;left&#8221;&gt;&lt;b&gt;Select a file to upload :&lt;/b&gt;&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;td align=&#8221;left&#8221;&gt;<br />
&lt;input type=&#8221;file&#8221; name=&#8221;uploadfile&#8221; size=&#8221;50&#8243;&gt;<br />
&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;tr&gt;<br />
&lt;td align=&#8221;left&#8221;&gt;<br />
&lt;input type=&#8221;submit&#8221; name=&#8221;Submit&#8221; value=&#8221;Upload&#8221;&gt;<br />
&lt;input type=&#8221;reset&#8221; name=&#8221;Reset&#8221; value=&#8221;Cancel&#8221;&gt;<br />
&lt;/td&gt;<br />
&lt;/tr&gt;<br />
&lt;/table&gt;<br />
&lt;/form&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt; </em></p>
<p>Once the request goes to the jsp the request is collected through getContentType() method (to learn about <a class="wp-caption" title="Javax.Servlet Interface" href="http://computerpreferedcourses.blogspot.com/2009/01/javaxservlet-interface-servletrequest.html" target="_blank">getContentType</a>)</p>
<p>Then Using the InputSreams and OutputStream the data is modified and do what the way you want the output to show</p>
<p>Name this Jsp file as <em><strong>fileUpload</strong> <strong>.jsp</strong> </em>Your program will run now<em><br />
</em></p>
<p><em>&lt;%@ page import=&#8221;java.io.*,javax.servlet.http.HttpServletRequest,javax.servlet.ServletInputStream&#8221; %&gt;<br />
&lt;%@ page import=&#8221;java.io.FileWriter,java.io.IOException,java.io.PrintWriter,java.text.DecimalFormat&#8221; %&gt;<br />
&lt;%@ page import=&#8221;java.sql.*&#8221; %&gt;<br />
&lt;%<br />
String filename = &#8220;&#8221;;<br />
String contentType = &#8220;&#8221;, fileData = &#8220;&#8221;, strLocalFileName = &#8220;&#8221;;<br />
int startPos = 0, endPos = 0;<br />
%&gt;<br />
&lt;%!<br />
//copy specified number of bytes from main data buffer to temp data buffer</em></p>
<p><em>void copyByte(byte [] fromBytes, byte [] toBytes, int start, int len)<br />
{<br />
for(int i=start;i&lt;(start+len);i++)<br />
{<br />
toBytes[i - start] = fromBytes[i];<br />
}<br />
}<br />
%&gt;<br />
&lt;%<br />
contentType = request.getContentType();<br />
//out.println(&#8220;&lt;br&gt;Content type is :: &#8221; +contentType);<br />
if ((contentType != null) &amp;&amp; (contentType.indexOf(&#8220;multipart/form-data&#8221;) &gt;= 0))<br />
{<br />
DataInputStream in = new DataInputStream(request.getInputStream());<br />
DataInputStream in1 = in;<br />
int formDataLength = request.getContentLength();<br />
byte dataBytes[] = new byte[formDataLength];<br />
int byteRead = 0;<br />
int totalBytesRead = 0;<br />
while (totalBytesRead &lt; formDataLength)<br />
{<br />
byteRead = in1.read(dataBytes, totalBytesRead, formDataLength);<br />
totalBytesRead += byteRead;<br />
}<br />
//out.println(&#8220;&lt;br&gt;totalBytesRead : &#8221; + totalBytesRead + &#8220;    :   formDataLength = &#8221; + formDataLength);</em></p>
<p><em>//String file = new String(dataBytes);<br />
//out.println(&#8220;&lt;br&gt;File Contents:&lt;br&gt;////////////////////////////////////&lt;br&gt;&#8221; + file + &#8220;&lt;br&gt;////////////////////////////////&lt;br&gt;&#8221;);<br />
byte[] line = new byte[128];<br />
if (totalBytesRead &lt; 3)<br />
{<br />
return;    //exit if file length is not sufficiently large<br />
}</em></p>
<p><em>String boundary = &#8220;&#8221;;<br />
String s = &#8220;&#8221;;<br />
int count = 0;<br />
int pos = 0;</em></p>
<p><em>//loop for extracting boundry of file</em></p>
<p><em>//could also be extracted from request.getContentType()</em></p>
<p><em>do<br />
{<br />
copyByte(dataBytes, line, count ,1);    //read 1 byte at a time<br />
count+=1;<br />
s = new String(line, 0, 1);<br />
fileData = fileData + s;<br />
pos = fileData.indexOf(&#8220;Content-Disposition: form-data; name=\&#8221;"); //set the file name<br />
if(pos != -1)<br />
endPos = pos;<br />
}while(pos == -1);<br />
boundary = fileData.substring(startPos, endPos);</em></p>
<p><em>//loop for extracting filename</em></p>
<p><em>startPos = endPos;<br />
do<br />
{<br />
copyByte(dataBytes, line, count ,1);    //read 1 byte at a time<br />
count+=1;<br />
s = new String(line, 0, 1);<br />
fileData = fileData + s;<br />
pos = fileData.indexOf(&#8220;filename=\&#8221;", startPos);  //set the file name<br />
if(pos != -1)<br />
startPos = pos;<br />
}while(pos == -1);<br />
do<br />
{<br />
copyByte(dataBytes, line, count ,1);    //read 1 byte at a time<br />
count+=1;<br />
s = new String(line, 0, 1);<br />
fileData = fileData + s;<br />
pos = fileData.indexOf(&#8220;Content-Type: &#8220;, startPos);<br />
if(pos != -1)<br />
endPos = pos;<br />
}while(pos == -1);</em></p>
<p><em>//to eliminate &#8221; from start &amp; end</em></p>
<p><em>filename = fileData.substring(startPos + 10, endPos &#8211; 3);</em></p>
<p><em>strLocalFileName = filename;<br />
int index = filename.lastIndexOf(&#8220;\\&#8221;);<br />
if(index != -1)<br />
filename = filename.substring(index + 1);<br />
else<br />
filename = filename;</em></p>
<p><em>s = new String(line, 0, 1);<br />
fileData = fileData + s;</em></p>
<p><em>//for invoking the value of starting address path</em></p>
<p><em>int pic1=fileData.lastIndexOf(&#8220;=&#8221;)+1;</em></p>
<p><em>// for invoking the value of ending address path</em></p>
<p><em>int pic2=fileData.lastIndexOf(&#8220;C&#8221;)-1;<br />
int img1=pic1+1;<br />
int img2=pic2-2;</em></p>
<p><em>// for getting the abs path using substring</em></p>
<p><em>String filename2=fileData.substring(img1,img2);</em></p>
<p><em>// for getting the abs path without quots</em></p>
<p><em>filename=fileData.substring(pic1,pic2);</em></p>
<p><em>// invoking the file name  without quots</em></p>
<p><em>int path=filename2.lastIndexOf(&#8220;\\&#8221; )+1;<br />
String path1=filename2.substring(path);</em></p>
<p><em>// invoing the file extension without quots</em></p>
<p><em>int lastpath=filename2.lastIndexOf(&#8220;.&#8221;)+1;<br />
String lastpath1=filename2.substring(lastpath);<br />
System.out.println(&#8221; output &#8220;+filename);<br />
out.println(&#8220;&lt;img src=&#8221;+filename+&#8221;&gt;&lt;/img&gt;&#8221;);</em></p>
<p><em>FileOutputStream fileOut = new FileOutputStream(filename);<br />
fileOut.write(dataBytes, startPos, (endPos &#8211; startPos));<br />
fileOut.flush();<br />
fileOut.close();<br />
}<br />
else<br />
{<br />
out.println(&#8220;Error in uploading &#8220;);<br />
}</em></p>
<p><em>%&gt; </em></p>
<p>Your image will be saved in path where your server installed</p>
<h3><em><strong><span style="color: #ff0000;">Find More file upload examples click here</span> <a title="uploading image into DB" href="http://www.bestjavainterviewquestions.com/uploading-file-name-of-image-to-database/" target="_blank">1</a> , <a title="File Upload using servlet" href="http://www.bestjavainterviewquestions.com/file-uploading-using-servlet/" target="_blank">2</a> and <a title="File Upload using JSP" href="http://www.bestjavainterviewquestions.com/jsp-example-file-uploading/" target="_blank">3</a></strong></em></h3>
<h2  class="related_post_title">Related Post</h2><ul class="related_post"><li>February 9, 2009 -- <a href="http://www.bestjavainterviewquestions.com/uploading-excel-file-using-jsp/" title="Uploading Excel File  Using Jsp">Uploading Excel File  Using Jsp</a> (0)</li><li>April 7, 2009 -- <a href="http://www.bestjavainterviewquestions.com/j2ee-interview-questions-3/" title="J2EE Interview Questions">J2EE Interview Questions</a> (0)</li><li>January 26, 2009 -- <a href="http://www.bestjavainterviewquestions.com/file-uploading-using-servlet/" title="File Uploading Using Servlet">File Uploading Using Servlet</a> (0)</li><li>June 23, 2008 -- <a href="http://www.bestjavainterviewquestions.com/19/" title="Java Basic Questions">Java Basic Questions</a> (1)</li><li>May 22, 2008 -- <a href="http://www.bestjavainterviewquestions.com/core-java-interview-questions/" title="Core Java Interview Questions">Core Java Interview Questions</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/jsp-example-file-uploading/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

