<%@ page errorPage="error.jsp" %>
<jsp:useBean id="prop" scope="page" class="java.util.Properties" />
<%@ page import="java.io.*,java.util.*,javax.servlet.*" %>
<%!
public String getBoundary(HttpServletRequest request,Properties prop) throws ServletException,IOException{
String boundary = null;
Enumeration enum = request.getHeaderNames();
while(enum.hasMoreElements()){
String header = (String)enum.nextElement();
String hvalue = request.getHeader(header);
prop.setProperty((header).toLowerCase(),hvalue);
if( "content-type".equalsIgnoreCase(header) ){
int idx = hvalue.lastIndexOf("boundary=");
if( idx != -1 ){
boundary= hvalue.substring( idx+9 , hvalue.length());
}
}
}
return boundary;
}
public String getFileName(String secondline){
int len = secondline.length();
int idx = secondline.lastIndexOf("filename=");
if( idx == -1 ) return null;
String filename = secondline.substring( idx+10 , len-1);
filename = "null"; //filename.replace('\\','/');
idx = filename.lastIndexOf("/");
idx = idx + 1;
filename = filename.substring( idx );
return filename;
}
%>
<%
String DPATH = "C:\\temp\\file\\";
int ROUGHSIZE=1024;
int MAXSIZE = 10; // Mega Byte
String boundary = getBoundary(request,prop);
if( boundary == null ){
boundary = prop.getProperty("boundary");// This statement line for HOTJAVABROWSER 3.0
}else{
boundary = "--"+boundary;
}
if( boundary == null ){
throw new Exception("BNF");
}
Long contentsize = new Long(prop.getProperty("content-length","0"));
int c;
StringWriter st = new StringWriter();
if( contentsize.longValue() < 1L ){
throw new Exception("ZERO");
}
long l = contentsize.longValue() - ROUGHSIZE;
/*
You can restrict upload file size in the following way
1. after uploaded u cannt find out file size then display error message
2. from contentsize it will give approximate size of file in bytes.
based on this u can restrict uploading.
*/
int KB = 1024;
int MB = 1024 * KB;
int csize = (int)(l / MB);
if( csize > MAXSIZE ){
throw new Exception("EL");
}
ServletInputStream fin = request.getInputStream();
//Getting Start Boundary Line
int cn;
int count=0;
while( (c=fin.read()) != -1 ){
if( c == '\r') break;
st.write(c);
count++;
}
c=fin.read();//read new line
String tboundary = st.getBuffer().toString();
tboundary=tboundary.trim();
if( ! tboundary.equalsIgnoreCase( boundary) ){
throw new Exception("BBNF");
}
st.close();
st = null;
st = new StringWriter();
//Getting File Information
while( (c=fin.read()) != -1 ){
if( c == '\r' ) break;
st.write(c);
}
c=fin.read();//read new line
String secondline = st.getBuffer().toString();
String filename = getFileName(secondline);
st.close();
st = null;
st = new StringWriter();
//Read Content Type of File
while( (c=fin.read()) != -1 ){
if( c == '\r' ) break;
st.write( c );
}
c=fin.read();//read new line
//out.println( st.getBuffer().toString() );
//Read \r and \n character
fin.read();
fin.read();
File newfile = null;
FileOutputStream fout =null;
try{
if( filename == null ) throw new FileNotFoundException("File Name not found");
newfile = new File(DPATH+filename);
fout = new FileOutputStream( newfile );
}catch(FileNotFoundException fnexp){
fin.close();
throw new Exception("FNF");
}
byte b[] = null;
while( l > 1024L ){
b = new byte[1024];
fin.read(b,0,1024);
fout.write(b);
b=null;
l -= 1024L;
}
if( l > 0 ){
b = new byte[(int)l];
fin.read(b,0,(int)l);
fout.write(b);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while( (c = fin.read()) != -1 ){
baos.write(c);
}
String laststring = baos.toString();
int idx = laststring.indexOf(boundary);
b = baos.toByteArray();
if( idx > 2 ){
fout.write(b,0,idx-2);
}else
{
fout.close();
newfile.delete();
throw new Exception("EBNF");
}
fout.flush();
fout.close();
fin.close();
%>