home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 November / PCONLINE_11_99.ISO / filesbbs / OS2 / APCHSSL2.ZIP / OS2HTTPD / servlets / UploadServlet.java < prev    next >
Encoding:
Java Source  |  1999-06-20  |  5.0 KB  |  169 lines

  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.*;
  4. import javax.servlet.*;
  5. import javax.servlet.http.*;
  6. import com.oroinc.text.regex.*;
  7.  
  8. /*
  9. REVISION HISTORY
  10. 1997-08-10 Written and published by Sten Hjelmqvist,sten@Mailhost.org
  11. 1999-05-02 Bugfix: 'String Last100' was not used. Its now fixed.
  12. 1999-05-08 Unused variables has been removed.
  13. */
  14.  
  15. public class UploadServlet extends HttpServlet
  16. {
  17.    public void service( HttpServletRequest _req, HttpServletResponse _res ) throws ServletException, IOException 
  18.    {
  19.  
  20.       _res.setContentType("text/html");
  21.       String FileNameRegex = ".*filename=.*\\W(\\w+\\.\\w+)";
  22.       String FooterRegex = "(\r\n\\-+\\d+\\-+\r\n)";
  23.  
  24.       // Create Perl5Compiler and Perl5Matcher instances.
  25.  
  26.       Perl5Compiler FileNameCompiler = new Perl5Compiler();;
  27.       Perl5Compiler FooterCompiler = new Perl5Compiler();;
  28.  
  29.       Perl5Matcher FileNameMatcher = new Perl5Matcher();;
  30.       Perl5Matcher FooterMatcher = new Perl5Matcher();;
  31.  
  32.       Perl5Pattern FileNamePattern = null;
  33.       Perl5Pattern FooterPattern = null;
  34.  
  35.  
  36.       Perl5StreamInput input;
  37.       MatchResult result;
  38.  
  39.       BufferedReader InFile = new BufferedReader(new InputStreamReader(_req.getInputStream()));
  40.       ServletOutputStream HTMLout = _res.getOutputStream();
  41.       String   FileName=null;
  42.       String HeaderData=null;
  43.  
  44.       // Attempt to compile the pattern.  If the pattern is not valid,
  45.       // report the error and exit.
  46.       try
  47.       {
  48.          FileNamePattern = (Perl5Pattern)FileNameCompiler.compile(FileNameRegex);
  49.       }
  50.       catch(MalformedPatternException e) 
  51.       {
  52.          HTMLout.println("Bad pattern."+"<br>");
  53.          HTMLout.println(e.getMessage()+"<br>");
  54.          //System.exit(1);
  55.       }
  56.  
  57.       // Attempt to compile the Filepattern.  If the Filepattern is not valid,
  58.       // report the error and exit.
  59.       try
  60.       {
  61.          FooterPattern = (Perl5Pattern)FooterCompiler.compile(FooterRegex);
  62.       }
  63.       catch(MalformedPatternException e)
  64.       {
  65.          HTMLout.println("Bad pattern."+"<br>");
  66.          HTMLout.println(e.getMessage()+"<br>");
  67.          //System.exit(1);
  68.       }
  69.  
  70.       // Open and read InFile, get the HTML header. It ends whith a empty line.
  71.       //The readLine 'eats' the \n
  72.       try
  73.       {
  74.          String s="";
  75.          StringBuffer sb = new StringBuffer();
  76.  
  77.          for(; (s = InFile.readLine()) != null; )
  78.          {
  79.             sb.append(s+"\n");
  80.  
  81.             if( s.length() == 0 )//Is this the End?
  82.                break;
  83.          }
  84.  
  85.          HeaderData = sb.toString();
  86.  
  87.          HTMLout.print("Header= (" + HeaderData + ")<br>");
  88.  
  89.          //System.out.println("Header= (" + HeaderData + ")");
  90.  
  91.       }
  92.       catch(IOException e) 
  93.       {
  94.          HTMLout.println("Error opening streamInput."+"<br>");
  95.          HTMLout.println(e.getMessage()+"<br>");
  96.          //System.exit(1);
  97.       }
  98.  
  99.  
  100.       //Get fileinfo
  101.       do
  102.       {
  103.          FileNameMatcher.contains(HeaderData, FileNamePattern);
  104.  
  105.          result = FileNameMatcher.getMatch(); 
  106.  
  107.       }while(result == null );
  108.  
  109.       FileName = result.group(1);
  110.  
  111.  
  112.  
  113.       // Print group 1.
  114.       HTMLout.print("FileName (" + result.group(1)+ ")<br>");
  115.       //System.out.println("FileName (" + result.group(1)+ ")");
  116.  
  117.  
  118.       // Now get the footer.
  119.       //It looks something like this '\n-----------------------------46541840611274--\n'
  120.       try
  121.       {
  122.          int len=0;
  123.          String s = "";
  124.          StringBuffer sb = new StringBuffer();
  125.  
  126.          char[] b = new char[4096];
  127.    
  128.          while( (len = InFile.read(b,0,4096)) != -1 )
  129.          {
  130.             s = new String(b);
  131.  
  132.             sb.append( s.substring(0,len) );
  133.          }
  134.          
  135.          int TotLen = sb.toString().length();
  136.  
  137.          //Look for the footer only in the last 100 bytes.
  138.          String Last100 = new String( sb.toString().substring( TotLen - 100<0 ?0 :TotLen - 100 ) );
  139.  
  140.          FooterMatcher.contains(Last100, FooterPattern);
  141.  
  142.          result = FooterMatcher.getMatch(); 
  143.  
  144.          //Is this the End?
  145.          int FooterLen = 0;
  146.          if( result != null )//This may be the beginning of the End
  147.             if( result.group(1).length() != 0 )//Yes my friend, this is the End 
  148.                FooterLen = result.group(1).length();
  149.  
  150.          
  151.          String FileData = new String( sb.toString().substring(0,TotLen-FooterLen) );
  152.  
  153.          FileOutputStream fos =  new FileOutputStream("\\OS2httpd\\servlets\\upload\\"+FileName);
  154.          OutputStreamWriter osw = new OutputStreamWriter( fos );
  155.          Writer OutFile = new BufferedWriter( osw );
  156.          OutFile.write( FileData );
  157.          OutFile.close();
  158.  
  159.          InFile.close();
  160.       }
  161.       catch(IOException e)
  162.       {
  163.             HTMLout.println("Error opening RandomAccessFile " + FileName + " for writing."+"<br>");
  164.             HTMLout.println(e.getMessage()+"<br>");
  165.             //System.exit(1);
  166.       }
  167.    }
  168. }
  169.