there are bugs in this code HOWTO: Upload Files with JSP

Discussion in 'Java' started by satish, Mar 17, 2005.

  1. satish

    satish Guest

    I HAVE RUN THIS CODE USING TOMCAT 4.01
    BUT I GOT SERVLET ERROR AND '}' ERRORS
    PLEASE HELP !


    Google Mike wrote:
    > The following example works on Linux RH8 with Tomcat 4.01. You may
    > have to modify slightly for other operating systems and JSP servers,
    > but I doubt it.
    >
    > Limitation: On a 1Ghz CPU with 512MB RAM, doing a localhost file
    > upload (not over the net), I could transfer a 4.7MB file in 15
    > seconds. It has timeout and memory issues once you start reaching
    > large file sizes, which I suspect would be around 10MB. Besides, FTP
    > is a more efficient upload mechanism, hands-down. (It would be nice

    if
    > web browsers supported an FTP upload mechanism in standard
    > w3c-compliant HTML tags.)
    >
    > Puts files into a directory of either "~/uploads/" on Linux/Unix, and
    > perhaps in "C:\uploads" on Windows. No need to create the directory.
    >
    > Don't change the <INPUT FILE...> tag because I'm parsing for the
    > "fldFile" name.
    >
    > Enjoy!
    >
    > If you have suggestions for optimization and better error/situation
    > handling, please post.
    >
    > ### INDEX .JSP
    > <BODY><FORM action='fupload.jsp' method='POST'
    > enctype='multipart/form-data'>
    > <P><INPUT type=file id="fldFile" name="fldFile"></P>
    > <P><INPUT type=submit id='btnSubmit' name='btnSubmit' value='Upload
    > File'></P>
    > </FORM></BODY>
    > ###
    >
    > ### FUPLOAD.JSP
    > <%
    > ServletInputStream oStream = request.getInputStream();
    > int b = 0;
    > StringBuffer sSample = new StringBuffer("");
    > try {
    > while (b > -1) {
    > b = oStream.read();
    > sSample.append((char) b);
    > }
    > } catch (java.io.IOException ioe) {}
    > //GET MIME DELIMITER AND FILENAME BLOCK
    > java.util.StringTokenizer st = new
    > java.util.StringTokenizer(sSample.toString(),"\n");
    > String sMimeDelim = "";
    > String sFileName = "";
    > String sTemp = "";
    > String sData = "";
    > int i = 0;
    > while (st.hasMoreTokens()){
    > sTemp = null;
    > sTemp = st.nextToken();
    > ++i;
    > if ((i == 1) && sTemp.startsWith("---------")){ //WE HAVE

    MIME
    > DELIMITER
    > sMimeDelim = sTemp.substring(0,sTemp.length()-1);
    > continue;
    > }
    > if (sTemp.startsWith("Content-Disposition: form-data;
    > name=\"fldFile\"; filename=\"")) { //WE HAVE FILENAME BLOCK
    > java.util.StringTokenizer stf = new
    > java.util.StringTokenizer(sTemp, "\"");
    > sTemp = stf.nextToken(); sTemp = stf.nextToken(); sTemp =
    > stf.nextToken();
    > sFileName = stf.nextToken();
    > break;
    > }
    > }
    > //GET FILE DATA BLOCK AND PARSE THE JUNK OUT OF IT
    > //Note: StringTokenizer has a bug with long delimiters, so we

    have
    > to use this technique below instead
    > String s = sSample.toString();
    > sTemp = sMimeDelim + "\n" + "Content-Disposition: form-data;
    > name=\"fldFile\"; filename=\"" + sFileName + "\"" + "\n";
    > int iStart = s.indexOf(sTemp);
    > iStart = iStart + sTemp.length()+3;
    > sTemp = sMimeDelim;
    > int iEnd = s.indexOf(sTemp, iStart);
    > sData = s.substring(iStart,iEnd);
    > sData = sData.substring(sData.indexOf("\n")+3);
    > sData = sData.substring(0,sData.length()-2);
    > //WRITE THE PARSED FILE BLOCK
    > new java.io.File("uploads").mkdirs();
    > java.io.DataOutputStream oFile = new java.io.DataOutputStream(new
    > java.io.FileOutputStream("uploads/" + sFileName));
    > oFile.writeBytes(sData);
    > oFile.close();
    > %>
    > <HTML><HEAD><TITLE>Samples</TITLE></HEAD>
    > <BODY><P>The uploaded file <%= sFileName %> was saved at
    > "~/uploads/".</P>
    > <INPUT type=button id="btnReturn" name="btnReturn" value="Do Another"
    > onClick="location.href='/samples/fupload/'">
    > </BODY></HTML>
    > ###
     
    satish, Mar 17, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Otto
    Replies:
    1
    Views:
    217
    alex23
    Apr 17, 2009
  2. Josef 'Jupp' Schugt

    Still use 'ruby-bugs' for Ruby bugs?

    Josef 'Jupp' Schugt, Nov 4, 2004, in forum: Ruby
    Replies:
    2
    Views:
    165
    Tom Copeland
    Nov 4, 2004
  3. Andrew Robinson

    XML/XHTML/HTML differences, bugs... and howto

    Andrew Robinson, Jan 23, 2013, in forum: Python
    Replies:
    0
    Views:
    83
    Andrew Robinson
    Jan 23, 2013
  4. Stefan Behnel
    Replies:
    0
    Views:
    133
    Stefan Behnel
    Jan 24, 2013
  5. Andrew Robinson
    Replies:
    0
    Views:
    92
    Andrew Robinson
    Jan 23, 2013
Loading...

Share This Page