Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Index Out Of Bounds

 
Darren Alexandria
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You have to allocate the necessary size before copying the data into this array. You should not give it a fix size.
 
Darren Alexandria
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can I do that?
 
Darren Alexandria
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I see my fault. I'll try in now. Thanks so much for your reply.
 
Darren Alexandria
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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">
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
prakash,
yes, I thought the same here
 
Darren Alexandria
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic