wood burning stoves 2.0*
The moose likes JSP and the fly likes Array Index Out Of Bounds Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Array Index Out Of Bounds" Watch "Array Index Out Of Bounds" New topic
Author

Array Index Out Of Bounds

Darren Alexandria
Ranch Hand

Joined: Aug 17, 2007
Posts: 185
Good day!
My task is to upload multiple files. I have the following codes which results to Array Index Out Of Bounds exception.

Please give me an advice on how to resolve this.

Thanks

index.jsp


fileUpload.jsp


ERROR:

HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Exception in JSP: /fileUpload.jsp:25

22: {
23: for(int i=start;i<(start+len);i++)
24: {
25: toBytes[i - start] = fromBytes[i];
26: }
27: }
28: %>


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:467)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:389)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


root cause

java.lang.ArrayIndexOutOfBoundsException: 14051
org.apache.jsp.fileUpload_jsp.copyByte(fileUpload_jsp.java:27)
org.apache.jsp.fileUpload_jsp._jspService(fileUpload_jsp.java:147)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16


You have to allocate the necessary size before copying the data into this array. You should not give it a fix size.


[My Blog]
All roads lead to JavaRanch
Darren Alexandria
Ranch Hand

Joined: Aug 17, 2007
Posts: 185
how can I do that?
Darren Alexandria
Ranch Hand

Joined: Aug 17, 2007
Posts: 185
Oh I see my fault. I'll try in now. Thanks so much for your reply.
Darren Alexandria
Ranch Hand

Joined: Aug 17, 2007
Posts: 185
Mr. Verre,

I have changed the code



to:


but still, it isn't working. I can't make the array length not fixed.
Please give me some advice.

Thanks.

Dada
prakash chandra
Greenhorn

Joined: Aug 14, 2007
Posts: 24
Hi,
Why have you given the same name "uploadfile" for two different <input type= "file"....> in your html?

<input type="file" name="uploadfile" size="50">
<input type="file" name="uploadfile" size="50">
<input type="file" name="uploadfile3" size="50">


SCJP 6.0
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

prakash,
yes, I thought the same here
Darren Alexandria
Ranch Hand

Joined: Aug 17, 2007
Posts: 185
Hello,

Thanks for your replies.

Earlier, I have already changed that to:



But it still gives me the same error which is:

exception

org.apache.jasper.JasperException: 2235
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:476)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:389)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


root cause

java.lang.ArrayIndexOutOfBoundsException: 2235
org.apache.jsp.fileUpload_jsp.copyByte(fileUpload_jsp.java:27)
org.apache.jsp.fileUpload_jsp._jspService(fileUpload_jsp.java:152)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:315)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16


No, that's not what I meant. You need to know the actual size needed. You are copying bytes into an array, so you need to calculate how many bytes you will insert in the array.
Darren Alexandria
Ranch Hand

Joined: Aug 17, 2007
Posts: 185
Oh, okay.

I have changed that to:



I used 4096 because the size of the 2 text files that I am uploading is 1KB each.

Still, I am getting the same error.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array Index Out Of Bounds