Yet Another File Upload Servlet (YAFUS). Version 2.01

This servlet uses a regular expression package called PerlTools.

The HTML file that invokes the servlet


<HTML>
<HEAD>
<TITLE>Java Web Server File Upload</TITLE>
</HEAD>
<BODY>

<H1>Java Web Server File Upload</H1>

<FORM METHOD=POST ENCTYPE=multipart/form-data ACTION="/servlet/yafus">

<INPUT TYPE=file SIZE=30 NAME="upload">
<INPUT TYPE=reset VALUE="Clear the field"><br>
<INPUT TYPE=submit VALUE="Upload">
</FORM>

</BODY>
</HTML>

The Java source

/*
REVISION HISTORY
1997-08-10 Written and published by Sten Hjelmqvist,sten.hjelmqvist@comhem.se
1999-05-02 Bugfix: 'String Last100' was not used. Its now fixed.
1999-05-08 Unused variables has been removed.
1999-10-17 Totally rewritten. A bug regarding uploading of binary files has been fixed.
           YAFUS now uses 'PerlTools' instead of OROMatcher.
2000-03-11 Bugfix: Filenames that contained a space didn't work.
*/

import java.io.*;
import java.net.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

import com.oroinc.text.perl.*;


public class yafus extends HttpServlet
{
   public void service( HttpServletRequest _req, HttpServletResponse _res ) throws ServletException, IOException 
   {
      String FileNameRegex = "/filename=.*\\\\([\\w\\. ]+)\\W+Content-Type: \\b.*\r\n\r\n/";
      String FooterRegex = "/(\r\n\\-+\\w+\\-+\r\n)/";

    //Create a Perl5Util instance.
      Perl5Util Matcher = new Perl5Util();

    //Input and Output streams
      InputStream is = _req.getInputStream();
      ServletOutputStream HTMLout = _res.getOutputStream();

    //Temporary ByteArray.The initial size is set to 8164.
    //A large initial size improves performance.
      ByteArrayOutputStream bos = new ByteArrayOutputStream(8164);

    //Save the file name here
      String  FileName=null;

    //The files total length,including header and footer
     int TotLen=0;
    //Header length
     int HeaderLen=0;
    //Footer length
     int FooterLen=0;
      
    //Util
      byte[] b = new byte[8164];
      int len=0;


    //--------------------------------------------------------
      

    //Read the InputStream and store it in 'bos'.
     try
     {
        while( (len = is.read(b,0,8164)) != -1 )
        {
           bos.write(b,0,len);
                  
           TotLen += len;
        }
        is.close();           
        
     }
     catch(IOException e) 
     {
        HTMLout.println("<center><h2>Upload error</h2></center>");
        HTMLout.println("Could not open InputStream."+"<br>");
        HTMLout.println(e.getMessage()+"<br>");
        return;
     }


     try
     {
       //Get fileinfo
        if( Matcher.match(FileNameRegex, bos.toString().substring(0,(TotLen<1024?TotLen:1024))) )
        {
           FileName = Matcher.group(1);
           HeaderLen = Matcher.endOffset(0);
        }
        else
        {
           HTMLout.println("<center><h2>Upload error</h2></center>");
           HTMLout.print("Could not find FileName in Header("+bos.toString().substring(0,200)+")<br>");
           return;
        }



        // Now get the footer.
        //It looks something like this '\n-----------------------------465418cd611274--\n'
        String Last100 = bos.toString().substring(TotLen - 100<0 ?0 :TotLen - 100 );
            
        if( Matcher.match(FooterRegex, Last100) )    
        {
           FooterLen = Matcher.group(1).length();
        }
        else
        {
           HTMLout.println("<center><h2>Upload error</h2></center>");
           HTMLout.print("Could not find Footer("+Last100.substring(Last100.length()-10)+")<br>");
           return;
        }
     }
     catch(java.lang.Exception e)
     {
        HTMLout.println("<center><h2>Upload error</h2></center>");
        HTMLout.println(e.getMessage()+"<br>");
        return;
     }
                    

     //Write the file to the Servers 'uploads' directory
     try
     {                       
        FileOutputStream fos =  new FileOutputStream("uploads\\"+FileName);
            
        fos.write( bos.toByteArray(),HeaderLen, TotLen-HeaderLen-FooterLen);
            
        fos.close();
            
        HTMLout.println("<center><h2>The file \'"+FileName+"\' has been uploaded.</h2></center>");
        HTMLout.close();
     }
     catch(IOException e)
     {
        HTMLout.println("<center><h2>Upload error</h2></center>");
        HTMLout.println("Could not open FileOutputStream."+"<br>");
        HTMLout.println(e.getMessage()+"<br>");
        return;
     }
   }
}


You are visitor: since august 10 1997.