code for upload(pleas reply within 2 days)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • santybanty20
    New Member
    • Feb 2007
    • 10

    code for upload(pleas reply within 2 days)

    sir,i have written the code for file uploading but it is very slow like it takas 2 to 3 minutes for 5 kb file and for 1 mb abt 20 minutes.so here is my code please help me for fast uploading also the code for download is given but it has some error help me in this because i have to submit after 3 days...help

    [HTML] <html>
    <body>
    <%
    String str=(String)ses sion.getAttribu te("cn");
    //out.println(str );
    if(str==null)
    {
    %>
    <script language="javas cript">
    alert("Unauthor ised Entry" + '/n' + "Please Login");
    location.href=" Faculty Login.jsp";
    </script>
    <%
    }

    %>
    <%@ page import="java.io .*,javax.servle t.http.HttpServ letRequest,java x.servlet.Servl etInputStream" %>
    <%@ page import="java.io .FileWriter,jav a.io.IOExceptio n" %>
    <%
    String savePath = "", filepath = "", filename = "";
    String contentType = "", fileData = "", strLocalFileNam e = "";
    int startPos = 0, endPos = 0;
    int BOF = 0, EOF = 0;
    %>
    <%!
    //copy specified number of bytes from main data buffer to temp data buffer
    void copyByte(byte [] fromBytes, byte [] toBytes, int start, int len)
    {
    for(int i=start;i<(star t+len);i++)
    {
    toBytes[i-start]=fromBytes[i];
    }
    }
    %>
    <%
    contentType = request.getCont entType();
    out.println("<b r>Content type is :: " +contentType);
    if ((contentType != null) && (contentType.in dexOf("multipar t/form-data") >= 0))
    {
    DataInputStream in = new DataInputStream (request.getInp utStream());
    DataInputStream in1 = in;
    int formDataLength = request.getCont entLength();
    byte dataBytes[] = new byte[formDataLength];
    int byteRead = 0;
    int totalBytesRead = 0;
    while (totalBytesRead < formDataLength)
    {
    byteRead = in1.read(dataBy tes, totalBytesRead, formDataLength) ;
    totalBytesRead += byteRead;
    }
    out.println("<b r>totalBytesRea d : " + totalBytesRead + " : formDataLength = " + formDataLength) ;

    //String file = new String(dataByte s);
    //out.println("<b r>File Contents:<br>////////////////////////////////////<br>" + file + "<br>////////////////////////////////<br>");

    byte[] line = new byte[128];
    if (totalBytesRead < 3)
    {
    return; //exit if file length is not sufficiently large
    }

    String boundary = "";
    String s = "";
    int count = 0;
    int pos = 0;

    //loop for extracting boundry of file
    //could also be extracted from request.getCont entType()
    do
    {
    copyByte(dataBy tes, line, count ,1); //read 1 byte at a time
    count+=1;
    s = new String(line, 0, 1);
    fileData = fileData + s;
    pos = fileData.indexO f("Content-Disposition: form-data; name=\""); //set the file name
    if(pos != -1)
    endPos = pos;
    }while(pos == -1);
    boundary = fileData.substr ing(startPos, endPos);

    //loop for extracting filename
    startPos = endPos;
    do
    {
    copyByte(dataBy tes, line, count ,1); //read 1 byte at a time
    count+=1;
    s = new String(line, 0, 1);
    fileData = fileData + s;
    pos = fileData.indexO f("filename=\"" , startPos); //set the file name
    if(pos != -1)
    startPos = pos;
    }while(pos == -1);
    do
    {
    copyByte(dataBy tes, line, count ,1); //read 1 byte at a time
    count+=1;
    s = new String(line, 0, 1);
    fileData = fileData + s;
    pos = fileData.indexO f("Content-Type: ", startPos);
    if(pos != -1)
    endPos = pos;
    }while(pos == -1);
    filename = fileData.substr ing(startPos + 10, endPos - 3); //to eliminate " from start & end
    strLocalFileNam e = filename;
    int index = filename.lastIn dexOf("\\");
    if(index != -1)
    filename = filename.substr ing(index + 1);
    else
    filename = filename;

    //loop for extracting ContentType
    boolean blnNewlnFlag = false;
    startPos = endPos; //added length of "Content-Type: "
    do
    {
    copyByte(dataBy tes, line, count ,1); //read 1 byte at a time
    count+=1;
    s = new String(line, 0, 1);
    fileData = fileData + s;
    pos = fileData.indexO f("\n", startPos);
    if(pos != -1)
    {
    if(blnNewlnFlag == true)
    endPos = pos;
    else
    {
    blnNewlnFlag = true;
    pos = -1;
    }
    }
    }while(pos == -1);
    contentType = fileData.substr ing(startPos + 14, endPos);

    //loop for extracting actual file data (any type of file)
    BOF = count + 1;
    do
    {
    copyByte(dataBy tes, line, count ,1); //read 1 byte at a time
    count+=1;
    s = new String(line, 0, 1);
    fileData = fileData + s;
    pos = fileData.indexO f(boundary, startPos); //check for end of file data i.e boundry value
    }while(pos == -1);
    EOF = count - boundary.length ();
    //file data extracted

    out.println("<b r><br>0. Local File Name = " + strLocalFileNam e);
    out.println("<b r><br>1. filename = " + filename);
    out.println("<b r>2. contentType = " + contentType);
    out.println("<b r>3. startPos = " + BOF);
    out.println("<b r>4. endPos = " + EOF);
    out.println("<b r>5. boundary = " + boundary);

    //create destination path & save file there
    String appPath = application.get RealPath("/");
    out.println("<b r>appPath : " + appPath);
    String destFolder = appPath + ""; //change this as required
    filename= destFolder + filename;
    FileOutputStrea m fileOut = new FileOutputStrea m(filename);
    fileOut.write(d ataBytes, BOF, (EOF - BOF));
    fileOut.flush() ;
    fileOut.close() ;
    //out.println("<b r>File saved as >> " + filename);
    //file saved at destination
    //out.println("<b r>File data : <br><br>******* *************** ****<br>" + (new String(dataByte s,startPos, (endPos - startPos))) + "<br><br>****** *************** *****");
    }
    else
    {
    out.println("Er ror in uploading ");
    }
    %>
    <script language="javas cript">
    alert("File Successfully Uploaded");
    location.href=" Faculty Welcome.jsp"
    </script>
    </body>
    </html>
    [/HTML]
Working...