<?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>Techelp &#187; java.io Package</title>
	<atom:link href="http://techelp.in/thome/category/java-io-package/feed/" rel="self" type="application/rss+xml" />
	<link>http://techelp.in/thome</link>
	<description></description>
	<lastBuildDate>Fri, 24 Feb 2012 06:21:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>File Upload and Download using Servlets and JSP</title>
		<link>http://techelp.in/thome/2009/04/24/file-upload-and-download-using-servlets-and-jsp/</link>
		<comments>http://techelp.in/thome/2009/04/24/file-upload-and-download-using-servlets-and-jsp/#comments</comments>
		<pubDate>Fri, 24 Apr 2009 13:18:04 +0000</pubDate>
		<dc:creator>shyam</dc:creator>
				<category><![CDATA[java.io Package]]></category>
		<category><![CDATA[Servlets and JSP]]></category>
		<category><![CDATA[J2EE]]></category>
		<category><![CDATA[JSP]]></category>
		<category><![CDATA[Servlets]]></category>

		<guid isPermaLink="false">http://techelp.in/javablog/?p=67</guid>
		<description><![CDATA[fileupload.jsp &#60;%&#8211;     Document   : fileupload.jsp     Created on : Apr 24, 2009, 3:40:43 PM     Author     : shyam &#8211;%&#62; &#60;%@page contentType=&#8221;text/html&#8221; pageEncoding=&#8221;UTF-8&#8243;%&#62; &#60;html&#62;     &#60;head&#62;         &#60;meta http-equiv=&#8221;Content-Type&#8221; content=&#8221;text/html; charset=UTF-8&#8243;&#62;         &#60;title&#62;File upload&#60;/title&#62;     &#60;/head&#62;     &#60;body&#62;         &#60;h2&#62;File upload!&#60;/h2&#62;         &#60;form name=&#8221;fileupload&#8221; method=&#8221;post&#8221; enctype=&#8221;multipart/form-data&#8221; action=&#8221;filedownload.jsp&#8221;&#62;             &#60;label for=&#8221;contentTitle&#8221;&#62;File name(syntax: Filename.ext)&#60;/label&#62;&#60;input type=&#8221;text&#8221; name=&#8221;contentTitle&#8221; /&#62; [...]]]></description>
			<content:encoded><![CDATA[<h4>fileupload.jsp</h4>
<blockquote><p>&lt;%&#8211;<br />
    Document   : fileupload.jsp<br />
    Created on : Apr 24, 2009, 3:40:43 PM<br />
    Author     : shyam<br />
&#8211;%&gt;</p>
<p>&lt;%@page contentType=&#8221;text/html&#8221; pageEncoding=&#8221;UTF-8&#8243;%&gt;</p>
<p>&lt;html&gt;<br />
    &lt;head&gt;<br />
        &lt;meta http-equiv=&#8221;Content-Type&#8221; content=&#8221;text/html; charset=UTF-8&#8243;&gt;<br />
        &lt;title&gt;File upload&lt;/title&gt;<br />
    &lt;/head&gt;<br />
    &lt;body&gt;<br />
        &lt;h2&gt;File upload!&lt;/h2&gt;<br />
        &lt;form name=&#8221;fileupload&#8221; method=&#8221;post&#8221; enctype=&#8221;multipart/form-data&#8221; action=&#8221;filedownload.jsp&#8221;&gt;<br />
            &lt;label for=&#8221;contentTitle&#8221;&gt;File name(syntax: Filename.ext)&lt;/label&gt;&lt;input type=&#8221;text&#8221; name=&#8221;contentTitle&#8221; /&gt;<br />
            &lt;br /&gt;<br />
            &lt;input type=&#8221;file&#8221; name=&#8221;file&#8221; /&gt;<br />
            &lt;br /&gt;<br />
            &lt;input type=&#8221;submit&#8221; value=&#8221;Upload&#8221; /&gt;<br />
        &lt;/form&gt;<br />
    &lt;/body&gt;<br />
&lt;/html&gt;</p></blockquote>
<h4>filedownload.jsp</h4>
<blockquote><p>&lt;%&#8211;<br />
    Document   : filedownload.jsp<br />
    Created on : Apr 24, 2009, 4:26:07 PM<br />
    Author     : shyam<br />
&#8211;%&gt;<br />
&lt;%@ page import=&#8221;edu.FileUpload,edu.UploadedFormFile&#8221; %&gt;<br />
&lt;%<br />
    try {<br />
        FileUpload fileUpload = new FileUpload();<br />
        UploadedFormFile uploadedFormFile = fileUpload.fileUpload(request);<br />
        byte[] bArray = uploadedFormFile.getFileData();</p>
<p>        response.setContentType(&#8220;application/x-download&#8221;);<br />
        response.setHeader(&#8220;Content-Disposition&#8221;, &#8220;attachment;filename=&#8221; + uploadedFormFile.getContentTitle());<br />
        ServletOutputStream sos = response.getOutputStream();<br />
        sos.write(bArray, 0, uploadedFormFile.getFileSize());<br />
        sos.flush();<br />
        sos.close();</p>
<p>    } catch (Throwable t) {<br />
        t.printStackTrace();<br />
    }<br />
%&gt;</p></blockquote>
<h4>FileUpload.java</h4>
<pre class="brush: java; title: ; notranslate">
package edu;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.ArrayList;

/**
*
* @author shyam

*/
public class FileUpload {

    public UploadedFormFile fileUpload(HttpServletRequest request)
            throws Throwable {

        boolean nowUCanBreak = false;
        int totalSize = 0;
        System.out.println(&quot;The fileUpload() begning.&quot;);
        if (request != null) {
            System.out.println(&quot;The request not null.&quot;);
        } else {
            System.out.println(&quot;The request null begning.&quot;);
        }
        UploadedFormFile uploadedFormFile = null;
        ArrayList list = new ArrayList();
        ServletInputStream sis = request.getInputStream();

        totalSize = sis.available();

        StringWriter sw = new StringWriter();

        int i = sis.read();

        for (; i != -1 &amp;&amp; i != '\r'; i = sis.read()) {
            sw.write(i);
        }
        sis.read(); // ditch '\n'
        String delimiter = sw.toString();
        try {
            while (true) {
                StringWriter h = new StringWriter();
                int[] temp = new int[4];
                temp[0] = (byte) sis.read();
                temp[1] = (byte) sis.read();
                temp[2] = (byte) sis.read();
                h.write(temp[0]);
                h.write(temp[1]);
                h.write(temp[2]);

                // read header
                for (temp[3] = sis.read(); temp[3] != -1; temp[3] = sis.read()) {
                    if (temp[0] == '\r' &amp;&amp;
                            temp[1] == '\n' &amp;&amp;
                            temp[2] == '\r' &amp;&amp;
                            temp[3] == '\n') {
                        break;
                    }
                    h.write(temp[3]);
                    temp[0] = temp[1];
                    temp[1] = temp[2];
                    temp[2] = temp[3];
                }
                String header = h.toString();

                int startName = header.indexOf(&quot;name=\&quot;&quot;);
                int endName = header.indexOf(&quot;\&quot;&quot;, startName + 6);
                if (startName == -1 || endName == -1) {
                    break;
                }

                String name = header.substring(startName + 6, endName);

                if (name != null &amp;&amp; name.equals(&quot;file&quot;)) {
                    nowUCanBreak = true;

                    startName = header.indexOf(&quot;filename=\&quot;&quot;);
                    endName = header.indexOf(&quot;\&quot;&quot;, startName + 10);
                    String filePath =  header.substring(startName + 10, endName);
                    String filename = filePath.substring(filePath.lastIndexOf(&quot;\\&quot;) + 1);
                    uploadedFormFile.setFileName(filename);

                    // write whole file to disk
                    int length = 0;
                    delimiter = &quot;\r\n&quot; + delimiter;
                    byte[] body = new byte[delimiter.length()];
                    for (int j = 0; j &lt; body.length; j++) {
                        body[j] = (byte) sis.read();
                        System.out.print(((char) body[j]));
                    }
                    // check it wasn't a 0 length file
                    if (!delimiter.equals(new String(body))) {
                        try {
                            int e = body.length - 1;
                            i = sis.read();
                            for (; i != -1; i = sis.read()) {
                                list.add(new Byte(body[0]));
                                for (int l = 0; l &lt; body.length - 1; l++) {
                                    body[l] = body[l + 1];
                                }
                                body[e] = (byte) i;
                                if (delimiter.equals(new String(body))) {
                                    break;
                                }
                                length++;
                            }
                        } catch (Throwable e) {
                            e.printStackTrace();
                        }
                    } else {
                        System.out.println(&quot;The file is of zero bytes length&quot;);
                    }
                    try {
                        byte bFileData[] = null;
                        int listSize = list.size();
                        if (listSize &gt; 0) {
                            bFileData = new byte[listSize];
                        }

                        for (int z = 0; z &lt; listSize; z++) {
                            bFileData[z] = ((Byte) list.get(z)).byteValue();
                        }
                        uploadedFormFile.setFileSize((int) (listSize));
                        uploadedFormFile.setFileData(bFileData);
                    } catch (Throwable e) {
                        e.printStackTrace();
                    }
                } else if (name != null &amp;&amp; name.equals(&quot;contentTitle&quot;)) {
                    StringWriter bbbString = new StringWriter();
                    while (true) {
                        int a = 0;
                        a = sis.read();
                        bbbString.write(a);
                        if (a == '\r') {
                            break;
                        }
                    }
                    String bbbStringStr = bbbString.toString();
                    bbbStringStr = bbbStringStr.substring(0, bbbStringStr.lastIndexOf(&quot;\r&quot;));
                    uploadedFormFile = new UploadedFormFile();
                    uploadedFormFile.setContentTitle(bbbStringStr);
                } else {
                    System.out.println(&quot;The form name two is &quot; + name);
                }
                if (sis.read() == '-' &amp;&amp; sis.read() == '-' &amp;&amp; nowUCanBreak) {
                    break;
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
            throw e;
        } finally {
            sis.close();
        }
        return uploadedFormFile;
    }
}
</pre>
<h4>UploadedFormFile.java</h4>
<pre class="brush: java; title: ; notranslate">
package edu;

import java.io.InputStream;
/**
*
* @author shyam
*/
public class UploadedFormFile{
    String fileName=null;
    String contentType=null;
    byte[] fileData=null;
    int fileSize=0;
    private String contentTitle=null;
    public void destroy(){}
    public String getContentType(){return contentType;}
    public byte[] getFileData(){return fileData;}
    public String getFileName(){return fileName;}
    public int getFileSize(){return fileSize;}
    public InputStream getInputStream(){return null;}
    public void setContentType(String contentType){this.contentType=contentType;}
    public void setFileName(String fileName){this.fileName=fileName;}
    public void setFileSize(int fileSize){this.fileSize=fileSize;}
    public void setFileData(byte[] fileData){this.fileData=fileData;}
    public String getContentTitle() {return contentTitle;}
    public void setContentTitle(String contentTitle) {this.contentTitle = contentTitle;} 
}
</pre>
<p> </p>
<p><strong><span style="color: #99cc00;">Share Knowledge and Save nature</span></strong></p>
<p><strong><span style="color: #99cc00;">-Shyam.</span></strong></p>

<div id='reaction_buttons_post67' class='reaction_buttons'>
<div class="reaction_buttons_tagline">What do you think of this post?</div><a class='reaction_button reaction_button_Awesome_count' href="javascript:reaction_buttons_increment_button_ajax('67', 'Awesome');">Awesome&nbsp;<span class='count' style='display: none;'>(<span class='count_number'>1</span>)</span></a> <a class='reaction_button reaction_button_Interesting_count' href="javascript:reaction_buttons_increment_button_ajax('67', 'Interesting');">Interesting&nbsp;<span class='count' style='display: none;'>(<span class='count_number'>2</span>)</span></a> <a class='reaction_button reaction_button_Useful_count' href="javascript:reaction_buttons_increment_button_ajax('67', 'Useful');">Useful&nbsp;<span class='count' style='display: none;'>(<span class='count_number'>3</span>)</span></a> <a class='reaction_button reaction_button_Boring_count' href="javascript:reaction_buttons_increment_button_ajax('67', 'Boring');">Boring&nbsp;<span class='count' style='display: none;'>(<span class='count_number'>1</span>)</span></a> <a class='reaction_button reaction_button_Sucks_count' href="javascript:reaction_buttons_increment_button_ajax('67', 'Sucks');">Sucks&nbsp;<span class='count' style='display: none;'>(<span class='count_number'>0</span>)</span></a> </div>
]]></content:encoded>
			<wfw:commentRss>http://techelp.in/thome/2009/04/24/file-upload-and-download-using-servlets-and-jsp/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Comparing any two files with any extension in java</title>
		<link>http://techelp.in/thome/2009/04/17/comparing-any-two-files-in-java/</link>
		<comments>http://techelp.in/thome/2009/04/17/comparing-any-two-files-in-java/#comments</comments>
		<pubDate>Fri, 17 Apr 2009 14:42:20 +0000</pubDate>
		<dc:creator>shyam</dc:creator>
				<category><![CDATA[java.io Package]]></category>
		<category><![CDATA[Core Java]]></category>
		<category><![CDATA[java.io]]></category>

		<guid isPermaLink="false">http://techelp.in/javablog/?p=19</guid>
		<description><![CDATA[Comparing any two files with any extension in java]]></description>
			<content:encoded><![CDATA[<pre class="brush: java; title: ; notranslate">import java.io.File;
import java.io.FileInputStream;

import java.io.BufferedInputStream;

/**
*
* @author shyam

*/
public class Test {

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception{
String filePath1=&quot;c:/file1.txt&quot;;
String filePath2=&quot;c:/file2.txt&quot;;

Test test=new Test();

//Using FileInputStream
if(test.compareFiles(filePath1,filePath2)){
System.out.println(&quot;Both the files are same.&quot;);
}
else{
System.out.println(&quot;Files are not same.&quot;);
}

//Using BufferedInputStream on FileInputStream

if(test.compareFilesWithBuffer(filePath1,filePath2)){
System.out.println(&quot;Both the files are same.&quot;);
}
else{
System.out.println(&quot;Files are not same.&quot;);
}

 
}

 

//    A BufferedInputStream adds functionality to another input stream-namely,
//     the ability to buffer the input and to support the mark and reset methods.
//     When the BufferedInputStream is created, an internal buffer array is created.
//     As bytes from the stream are read or skipped, the internal buffer is refilled
//     as necessary from the contained input stream, many bytes at a time.
//     The mark operation remembers a point in the input stream and the reset
//     operation causes all the bytes read since the most recent mark operation
//     to be reread before new bytes are taken from the contained input stream.

boolean compareFilesWithBuffer(String filePath1,String filePath2){
        try{           
            File f1 = new File(filePath1);
            File f2 = new File(filePath2);
            if(f1.length() == f2.length()){
                BufferedInputStream bis1=new BufferedInputStream(new FileInputStream(f1));
                BufferedInputStream bis2=new BufferedInputStream(new FileInputStream(f2)); 

                while(true){
                    int a = bis1.read();
                    int b = bis2.read();
                    if(a != b){
                        return false;
                    }
                    if(a == -1){
                        return true;
                    }
                }
            }else{
                return false;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return false;
    }

 

boolean compareFiles(String filePath1,String filePath2){
try{
File f1 = new File(filePath1);
File f2 = new File(filePath2);
if(f1.length() == f2.length()){
FileInputStream fis1 = new FileInputStream(f1);
FileInputStream fis2 = new FileInputStream(f2);

while(true){
int a = fis1.read();
int b = fis2.read();
if(a != b){
return false;
}
if(a == -1){
return true;
}
}
}else{
return false;
}

}catch(Exception e){
e.printStackTrace();
}
return false;
}
 
}</pre>

<div id='reaction_buttons_post19' class='reaction_buttons'>
<div class="reaction_buttons_tagline">What do you think of this post?</div><a class='reaction_button reaction_button_Awesome_count' href="javascript:reaction_buttons_increment_button_ajax('19', 'Awesome');">Awesome&nbsp;<span class='count' style='display: none;'>(<span class='count_number'>4</span>)</span></a> <a class='reaction_button reaction_button_Interesting_count' href="javascript:reaction_buttons_increment_button_ajax('19', 'Interesting');">Interesting&nbsp;<span class='count' style='display: none;'>(<span class='count_number'>0</span>)</span></a> <a class='reaction_button reaction_button_Useful_count' href="javascript:reaction_buttons_increment_button_ajax('19', 'Useful');">Useful&nbsp;<span class='count' style='display: none;'>(<span class='count_number'>1</span>)</span></a> <a class='reaction_button reaction_button_Boring_count' href="javascript:reaction_buttons_increment_button_ajax('19', 'Boring');">Boring&nbsp;<span class='count' style='display: none;'>(<span class='count_number'>0</span>)</span></a> <a class='reaction_button reaction_button_Sucks_count' href="javascript:reaction_buttons_increment_button_ajax('19', 'Sucks');">Sucks&nbsp;<span class='count' style='display: none;'>(<span class='count_number'>1</span>)</span></a> </div>
]]></content:encoded>
			<wfw:commentRss>http://techelp.in/thome/2009/04/17/comparing-any-two-files-in-java/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

