Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > servlet - file upload problem... IndexOutOfBoundsException

Reply
Thread Tools

servlet - file upload problem... IndexOutOfBoundsException

 
 
tiewknvc9
Guest
Posts: n/a
 
      11-01-2006
Hi,

Im trying to allow a user to upload a file ( 0 - 1 meg). However when
the file gets larger than about 3k, its throwing an
IndexOutOfBoundsException.

Its odd that it works perfectly with extremely small files, yet breaks
on still very small files. What is the deal?

I gave the code below (mostly taken from sample code found on the web),
Im lost, please help! Im sure its something simple,,, simply
irritating!

Thank you so much in advance!


---------


private void doStuff(PrintWriter out, String strContentType,
ServletInputStream disRequest, int iContentLength){

String strFileString = "";

String contentType = strContentType;
if ((contentType != null) &&
(contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(disRequest);
int formDataLength = iContentLength;

byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
try{
byteRead = in.read(dataBytes, totalBytesRead, formDataLength -
totalBytesRead);
totalBytesRead += byteRead;
}catch(IOException ioe){
out.println("IOException: ERROR analyzing file. Error
Code:en3kd");
}
}

String file = new String(dataBytes);
strFileString = file.substring(file.indexOf("filename=\"") + 10);
strFileString = strFileString.substring(0,
strFileString.indexOf("\n"));
strFileString =
strFileString.substring(strFileString.lastIndexOf( "\\") +
1,strFileString.indexOf("\""));

int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex +
1,contentType.length());

int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;

int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0,
boundaryLocation)).getBytes()).length;

String strTheirFileName = strFileString;
strFileString = "/tmp/" + strFileString;

boolean bVtEnding = false;
if (strFileString.endsWith("vt")){
//good, continue
bVtEnding = true;
}else{
//bad, exit
out.println("You can only finalize unfinalized VideoTagger
files.");
out.flush();
}

if (bVtEnding){
File filOld = null;
try{
filOld = new File(strFileString);
filOld.createNewFile();
FileOutputStream fos = new FileOutputStream(filOld);
//breaks on this line.
fos.write(dataBytes, startPos, (endPos - startPos));
fos.flush();
fos.close();
}catch(FileNotFoundException fnfe){
out.println("FileNotFoundException: <b>Error</b>, could not find
upload file... Error Code:md9ww");
out.println("<b>ERROR</b>");
}catch(IOException ioe){
out.println("IOException: ERROR analyzing file... <b>Error</b>
Codel1nm " + strFileString + "<br><br>" +
ioe.getLocalizedMessage());
out.println("<b>ERROR</b>");
}//catch(IndexOutOfBoundsException iobe){
// out.println("IndexOutOfBoundsException: <b>ERROR</b> analyzing
file... Error Code:ne8ni<br>");
// out.println("<b>ERROR</b>");
// iobe.printStackTrace();
//}
}
}

 
Reply With Quote
 
 
 
 
Manish Pandit
Guest
Posts: n/a
 
      11-01-2006
Your best bet will be to use Apache File Upload API
(http://jakarta.apache.org/commons/fileupload/) instead of
programatically extracting filenames and other information by using
string operations left and right.

I believe somewhere in your code there is a substring() happening that
exceeds the string size, leading to an ArrayIndexOutOfBoundsException -
pretty much like "Hello".substring(8,5).

Apache File Upload is simpler to use and the API will provide you the
file names, size, etc.

-cheers,
Manish

 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      11-04-2006
tiewknvc9 wrote:
---------
> private void doStuff(PrintWriter out, String strContentType,
> ServletInputStream disRequest, int iContentLength){
>
> String strFileString = "";


Eliminate the type prefixes on your variables.

- Lew
 
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
NegativeArraySizeException ... IndexOutOfBoundsException ... Albretch Mueller Java 28 01-20-2010 02:37 AM
IndexOutOfBoundsException getting a PrinterJob.defaultPage() on a Linux box joseph_daniel_zukiger@yahoo.com Java 1 01-10-2006 09:44 PM
Upload a file without file Upload control - ASP.Net =?Utf-8?B?U2FyYXY=?= ASP .Net 3 08-03-2005 01:09 AM
Servlet question(Tomcat, web.xml, servlet-class, servlet-name) circuit_breaker Java 2 04-04-2004 03:26 AM
File upload servlet using MultipartRequest - file not created on the server Paul Smith Java 1 11-17-2003 10:24 AM



Advertisments