<%
response.setContentType("text/html");
response.setHeader("Cache-control","no-cache");
String err = "";
String lastFileName = "";
String contentType = request.getContentType();
String boundary = "";
final int BOUNDARY_WORD_SIZE = "boundary=".length();
System.out.println("contentType --> "+contentType);
System.out.println("BOUNDARY_WORD_SIZE --> "+BOUNDARY_WORD_SIZE);
if(contentType == null || !contentType.startsWith("multipart/form-data"))
{
err = "Ilegal ENCTYPE : must be multipart/form-data\n";
err += "ENCTYPE set = " + contentType;
}
else
{
boundary = contentType.substring(contentType.indexOf("boundar y=") + BOUNDARY_WORD_SIZE);
System.out.println("boundary --> "+boundary);
boundary = "--" + boundary;
try
{
ServletInputStream sis = request.getInputStream();
byte[] b = new byte[1024];
int x=0;
int state=0;
String name=null,fileName=null,contentType2=null;
java.io.FileOutputStream buffer = null;
while((x=sis.readLine(b,0,1024))>-1)
{
System.out.println("************ x ********** "+x);
String s = new String(b,0,x);
System.out.println("************ s ********** \n"+s);
if(s.startsWith(boundary))
{
state = 0;
System.out.println("name="+name);
System.out.println("filename="+fileName);
name = null;
contentType2 = null;
fileName = null;
}
else if(s.startsWith("Content-Disposition") && state==0)
{
System.out.println("-- 1 --");
state = 1;
System.out.println("s.indexOf(filename=) --> "+s.indexOf("filename="));
if(s.indexOf("filename=") == -1)
{
name = s.substring(s.indexOf("name=") + "name=".length(),s.length()-2);
System.out.println("after name substring 1 "+name);
}
else
{
name = s.substring(s.indexOf("name=") + "name=".length(),s.lastIndexOf(";"));
System.out.println("after name substring 2 "+name);
fileName = s.substring(s.indexOf("filename=") + "filename=".length(),s.length()-2);
System.out.println("fileName --> "+fileName);
//String fileName1 = s.substring(s.indexOf("filename=") + "filename=".length(),s.length());
//System.out.println("fileName1 -->"+fileName1);
if(fileName.equals("\"\""))
{
fileName = null;
}
else
{
String userAgent = request.getHeader("User-Agent");
System.out.println("userAgent --> "+userAgent);
String userSeparator="/"; // default
if (userAgent.indexOf("Windows")!=-1)
{
System.out.println("test --> "+"
");
userSeparator="
";
}
if (userAgent.indexOf("Linux")!=-1)
{
userSeparator="/";
}
System.out.println("userSeparator "+userSeparator);
System.out.println("fileName before inserting userSeparators "+fileName);
fileName = fileName.substring(fileName.lastIndexOf(userSepara tor)+1,fileName.length()-1);
System.out.println("fileName after userSeparators "+fileName);
if(fileName.startsWith( "\""))
{
fileName = fileName.substring( 1);
}
}
}
name = name.substring(1,name.length()-1);
System.out.println("name 2 --> "+name);
System.out.println("final file name "+fileName);
if (name.equals("file"))
{
if (buffer!=null)
buffer.close();
lastFileName = fileName;
buffer = new java.io.FileOutputStream("/Documents and Settings/sunil/Desktop/images/"+fileName);
}
}
else if(s.startsWith("Content-Type") && state==1)
{
System.out.println("-- 2 --");
state = 2;
contentType2 = s.substring(s.indexOf(":")+2,s.length()-2);
System.out.println("contentType2 --> "+contentType2);
}
else if(s.equals("\r\n") && state != 3)
{
System.out.println("-- 3 --");
state = 3;
}
else
{
System.out.println("-- 4 --");
if (name.equals("file"))
{
System.out.println("Final x :: "+x);
buffer.write(b,0,x);
}
}
} // while closing
sis.close();
buffer.close();
}catch(java.io.IOException e)
{
err = e.toString();
}
}
boolean ok = err.equals("");
if(!ok)
{
out.println(err);
}
else
{
%>
<SCRIPT language="javascript">
history.back(1);
alert('Uploaded <%=lastFileName%>');
window.location.reload(false);
</SCRIPT>
<%
}
out.println("done");
%>
</BODY>
</HTML>
|