Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > there are bugs in this code HOWTO: Upload Files with JSP

Reply
Thread Tools

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

 
 
satish
Guest
Posts: n/a
 
      03-17-2005

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>
> ###


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to upload multiple files in jsp/html Ulfrei Java 0 12-12-2007 04:27 PM
[JSP] difference between jsp:forward and jsp:include alexjaquet@gmail.com Java 0 06-02-2006 01:21 PM
Bugs and Bugs...get rid of them Jason Computer Security 1 01-31-2006 10:47 PM
how to upload and download files to a webserver using FTP in JSP satish Java 2 03-17-2005 11:03 PM
Still use 'ruby-bugs' for Ruby bugs? Josef 'Jupp' Schugt Ruby 2 11-04-2004 10:10 PM



Advertisments