Hi
I am running Tomcat 3.2 and Apache 1.3.12.
I get some funny behaviour when uploading a file through the
apache/tomcat connection. It looks like the input stream is broken.
The output from Tomcat looks like
Using classpath:
/usr/local/tomcat/lib/ant.jar:/usr/local/tomcat/lib/jasper.jar:/usr/local/tomcat/lib/servlet.jar:/usr/local/tomcat/lib/test:/usr/local/tomcat/lib/webserver.jar:/usr/java/jdk/lib/tools.jar:/usr/java/jsse/lib/jcert.jar:/usr/java/jsse/lib/jsse.jar:/usr/java/jsse/lib/jnet.jar:/usr/java/jaxp/jaxp.jar:/usr/java/jaxp/parser.jar:/usr/java/jdk/bin
Starting tomcat. Check logs/tomcat.log for error messages
>>>>>> upload GIF using http://localhost:8080/spqr/servlet/LogoUpload
LogoUpload.service: begin
LogoUpload.service: reading file
LogoUpload.service: reading new block len = 5283,5283
LogoUpload.service: reading file completed
LogoUpload.service: file is GIF
LogoUpload.service: looking for footer
LogoUpload.service: found footer
LogoUpload.service: end
>>>>>> upload GIF using http://localhost/spqr/servlet/LogoUpload i.e.
over
LogoUpload.service: begin
LogoUpload.service: reading file
Y
LogoUpload.service: reading new block len = 340,340
Y
LogoUpload.service: reading new block len = 0,340
Y
LogoUpload.service: reading new block len = 0,340
Y
...
LogoUpload.service: reading new block len = 0,340
Y
in an infinite loop!
My code (attached) does not print the Y anywhere that I can see!
The Apache httpd.conf file is out of the box with the mod_jk.conf.spqr
(attached) file included right at
the end.
Do I have to configure anything in Apache to get it to pass the
multi-part request without mangling it?
Thanks
Sandy
package com.mapquest.spqr.db;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.oroinc.text.perl.*;
public class LogoUpload extends HttpServlet
{
public static final int maxFileSize=65535;
String[] getFileTypes( )
{
return new String[] {".gif",".jpg", ".jpeg" };
}
public void service( HttpServletRequest _req, HttpServletResponse _res )
throws ServletException, IOException
{
System.out.println( "LogoUpload.service: begin" );
Client client = (Client)_req.getSession( ).getAttribute( "client" );
if ( client == null )
{
throw new ServletException( "No bean called \"client\" in session" );
}
String user=client.getUser( );
//
// Input and Output streams
//
InputStream is = _req.getInputStream();
//
// Temporary ByteArray.The initial size is set to 8164.
// A large initial size improves performance.
//
ByteArrayOutputStream bos = new ByteArrayOutputStream(8164);
//
// The files total length,including header and footer
//
int TotLen=0;
int HeaderLen=0;
int FooterLen=0;
//
// byte array for read
//
byte[] b = new byte[8164];
int len=0;
//
// Read the InputStream and store it in 'bos'.
//
System.out.println( "LogoUpload.service: reading file" );
try
{
while( ((len = is.read(b,0,8164)) != -1) && TotLen <= maxFileSize )
{
bos.write(b,0,len);
TotLen += len;
System.out.println( "LogoUpload.service: reading new block len
= "+len + "," +TotLen );
}
is.close();
System.out.println( "LogoUpload.service: reading file completed" );
}
catch(IOException e)
{
System.out.println( "LogoUpload.service: upload failed" );
throw new ServletException( "File upload failed", e );
}
if ( TotLen > maxFileSize )
{
System.out.println( "LogoUpload.service: file too big" );
getServletContext( )
.getRequestDispatcher("/"+client.getDefaultLanguage(
)+"/logo-big.jsp")
.forward( _req, _res );
return;
}
String fileExtension = null;
try
{
String GIFRegex = "/Content-Type: image/gif\r\n\r\n/";
String PJPEGRegex = "/Content-Type: image/pjpeg\r\n\r\n/";
String JPEGRegex = "/Content-Type: image/jpeg\r\n\r\n/";
String JPGRegex = "/Content-Type: image/jpg\r\n\r\n/";
String FooterRegex = "/(\r\n\\-+\\w+\\-+\r\n)/";
//
// Create a Perl5Util instance.
//
Perl5Util Matcher = new Perl5Util();
//
// check that the file is a GIF or a JPEG and find the end of the
header
//
String header = bos.toString().substring(0,(TotLen<1024?TotLen:1024));
if( Matcher.match(GIFRegex, header) )
{
HeaderLen = Matcher.endOffset(0);
fileExtension=".gif";
System.out.println( "LogoUpload.service: file is GIF" );
}
else if( Matcher.match(PJPEGRegex, header) )
{
HeaderLen = Matcher.endOffset(0);
fileExtension=".jpg";
System.out.println( "LogoUpload.service: file is PJPEG" );
}
else if( Matcher.match(JPEGRegex, header) )
{
HeaderLen = Matcher.endOffset(0);
fileExtension=".jpg";
System.out.println( "LogoUpload.service: file is PJPEG" );
}
else if( Matcher.match(JPGRegex, header) )
{
HeaderLen = Matcher.endOffset(0);
fileExtension=".jpg";
System.out.println( "LogoUpload.service: file is JPG" );
}
else
{
System.out.println( "LogoUpload.service: file is wrong type" );
getServletContext( )
.getRequestDispatcher("/"+client.getDefaultLanguage(
)+"/logo-type.jsp")
.forward( _req, _res );
return;
}
//
// Now get the footer.
// It looks something like this
'\n-----------------------------465418cd611274--\n'
//
String Last100 = bos.toString().substring(TotLen - 100<0 ?0 :TotLen -
100 );
System.out.println( "LogoUpload.service: looking for footer" );
if( Matcher.match(FooterRegex, Last100) )
{
FooterLen = Matcher.group(1).length();
System.out.println( "LogoUpload.service: found footer" );
}
else
{
throw new ServletException( "Could not find end of Image
Footer" );
}
}
catch(java.lang.Exception e)
{
throw new ServletException( "File upload failed", e );
}
//Write the file to the Servers 'uploads' directory
try
{
String filename = getServletContext( )
.getRealPath(
"/clients/logos/"+user );
//
// delete any potentially existing files
//
try
{
new File( filename+".gif").delete( );
}
catch( Throwable ex )
{
}
try
{
new File( filename+".jpg").delete( );
}
catch( Throwable ex )
{
}
filename+=fileExtension;
FileOutputStream fos = new FileOutputStream(filename);
fos.write( bos.toByteArray(),HeaderLen, TotLen-HeaderLen-FooterLen);
client.setLogoURL( "/spqr/clients/logos/"+user+fileExtension );
fos.close();
}
catch(IOException e)
{
System.out.println( "LogoUpload.service: exception" );
throw new ServletException( "User = " +user+ "Writing logo failed "
+bos.toString( ), e );
}
getServletContext( )
.getRequestDispatcher("/"+client.getDefaultLanguage( )+"/logo-ok.jsp")
.forward( _req, _res );
System.out.println( "LogoUpload.service: end" );
}
boolean isJPEG( byte[] content )
{
if ( content[0] != 0377 )
return false;
if ( content[1] != 0330 )
return false;
if ( content[2] != 0377 )
return false;
if ( content[3] != 0340 )
return false;
if ( content[6] != 'J' )
return false;
if ( content[7] != 'F' )
return false;
if ( content[8] != 'I' )
return false;
if ( content[9] != 'F' )
return false;
return true;
}
boolean isGIF( byte[] content )
{
if ( content[0] != 'G' )
return false;
if ( content[1] != 'I' )
return false;
if ( content[2] != 'F' )
return false;
if ( content[3] != '8' )
return false;
if ( content[4] != '9' )
return false;
return true;
}
}
###################################################################
# Auto generated configuration. Dated: Wed Dec 06 10:05:24 CET 2000
###################################################################
#
# The following line instructs Apache to load the jk module
#
LoadModule jk_module libexec/mod_jk.so
JkWorkersFile /usr/local/apache-group/dist/tomcat/conf/workers.properties
JkLogFile /usr/local/apache-group/dist/tomcat/logs/mod_jk.log
#
# Log level to be used by mod_jk
#
JkLogLevel error
###################################################################
# SSL configuration #
#
# By default mod_jk is configured to collect SSL information from
# the apache environment and send it to the Tomcat workers. The
# problem is that there are many SSL solutions for Apache and as
# a result the environment variable names may change.
#
# The following (commented out) JK related SSL configureation
# can be used to customize mod_jk's SSL behaviour.
#
# Should mod_jk send SSL information to Tomact (default is On)
# JkExtractSSL Off
#
# What is the indicator for SSL (default is HTTPS)
# JkHTTPSIndicator HTTPS
#
# What is the indicator for SSL session (default is SSL_SESSION_ID)
# JkSESSIONIndicator SSL_SESSION_ID
#
# What is the indicator for client SSL cipher suit (default is SSL_CIPHER)
# JkCIPHERIndicator SSL_CIPHER
#
# What is the indicator for the client SSL certificated (default is SSL_CLIENT_CERT)
# JkCERTSIndicator SSL_CLIENT_CERT
#
# #
###################################################################
#
# Root context mounts for Tomcat
#
JkMount /*.jsp ajp13
JkMount /servlet/* ajp13
#########################################################
# Auto configuration for the /examples context starts.
#########################################################
#
# The following line makes apache aware of the location of the /examples context
#
Alias /examples "/usr/local/apache-group/dist/tomcat/webapps/examples"
<Directory "/usr/local/apache-group/dist/tomcat/webapps/examples">
Options Indexes FollowSymLinks
</Directory>
#
# The following line mounts all JSP files and the /servlet/ uri to tomcat
#
JkMount /examples/servlet/* ajp13
JkMount /examples/*.jsp ajp13
#
# The following line prohibits users from directly accessing WEB-INF
#
<Location "/examples/WEB-INF/">
AllowOverride None
deny from all
</Location>
#
# The following line prohibits users from directly accessing META-INF
#
<Location "/examples/META-INF/">
AllowOverride None
deny from all
</Location>
#######################################################
# Auto configuration for the /examples context ends.
#######################################################
#########################################################
# Auto configuration for the /spqr context starts.
#########################################################
#
# The following line makes apache aware of the location of the /spqr context
#
Alias /spqr "/usr/local/tomcat/webapps/spqr"
<Directory "/usr/local/tomcat/webapps/spqr">
Options Indexes FollowSymLinks
DirectoryIndex index.jsp EN/index.jsp
</Directory>
#
# The following line mounts all JSP files and the /servlet/ uri to tomcat
#
JkMount /spqr/servlet/* ajp13
JkMount /spqr/*.jsp ajp13
#
# The following line prohibits users from directly accessing WEB-INF
#
<Location "/spqr/WEB-INF/">
AllowOverride None
deny from all
</Location>
#
# The following line prohibits users from directly accessing META-INF
#
<Location "/spqr/META-INF/">
AllowOverride None
deny from all
</Location>
#######################################################
# Auto configuration for the /spqr context ends.
#######################################################
#########################################################
# Auto configuration for the /admin context starts.
#########################################################
#
# The following line makes apache aware of the location of the /admin context
#
Alias /admin "/usr/local/apache-group/dist/tomcat/webapps/admin"
<Directory "/usr/local/apache-group/dist/tomcat/webapps/admin">
Options Indexes FollowSymLinks
</Directory>
#
# The following line mounts all JSP files and the /servlet/ uri to tomcat
#
JkMount /admin/servlet/* ajp13
JkMount /admin/*.jsp ajp13
#
# The following line prohibits users from directly accessing WEB-INF
#
<Location "/admin/WEB-INF/">
AllowOverride None
deny from all
</Location>
#
# The following line prohibits users from directly accessing META-INF
#
<Location "/admin/META-INF/">
AllowOverride None
deny from all
</Location>
#######################################################
# Auto configuration for the /admin context ends.
#######################################################
#########################################################
# Auto configuration for the /test context starts.
#########################################################
#
# The following line makes apache aware of the location of the /test context
#
Alias /test "/usr/local/apache-group/dist/tomcat/webapps/test"
<Directory "/usr/local/apache-group/dist/tomcat/webapps/test">
Options Indexes FollowSymLinks
</Directory>
#
# The following line mounts all JSP files and the /servlet/ uri to tomcat
#
JkMount /test/servlet/* ajp13
JkMount /test/*.jsp ajp13
#
# The following line prohibits users from directly accessing WEB-INF
#
<Location "/test/WEB-INF/">
AllowOverride None
deny from all
</Location>
#
# The following line prohibits users from directly accessing META-INF
#
<Location "/test/META-INF/">
AllowOverride None
deny from all
</Location>
#######################################################
# Auto configuration for the /test context ends.
#######################################################
begin:vcard
n:McPherson;Sandy
tel;cell:+31 6 224 70517
tel;fax:+31 527 68 74 80
tel;work:+31 527 68 70 10
x-mozilla-html:TRUE
org:Allset Consultancy BV.
version:2.1
email;internet:[EMAIL PROTECTED]
title:Senior Consultant
adr;quoted-printable:;;Robbenplaat 18=0D=0A;Urk;;8321PA;Netherlands
x-mozilla-cpt:;32288
fn:Sandy McPherson
end:vcard