i have the below code: its appear error when i run the code:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@ page import="java.io.*,java.sql.*,java.util.*,java.text.*,java.text.SimpleDateFormat" %>
<html>
<%
int val =0;
StringcontentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;

while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+ 1,saveFile.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;

FileOutputStream fileOut = new FileOutputStream(saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
%>

<%
Connection con=null;
PreparedStatement pstatement = null;
String line = null;
String value=null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "thundercatz";
String driver = "com.mysql.jdbc.Driver";
String userName = "root"; 
String password = "";
try{
StringBuilder contents = new StringBuilder();
BufferedReader input = new BufferedReader(new FileReader(saveFile));
while (( line = input.readLine()) != null){
contents.append(line);
}
value = contents.toString();
System.out.println("Value:"+value);
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url+dbName,userName,password);
java.util.Date now = new java.util.Date();
String DATE_FORMAT = "yyyy-MM-dd hh:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
String strDateNew = sdf.format(now) ;

String queryString = "INSERT INTO file_tbl set file_data='"+value+"',file_date='"+strDateNew+"'";

//out.println(queryString);

pstatement=con.prepareStatement(queryString);


val = pstatement.executeUpdate();

if(val>0)
{
%>
<br><br>
<b>File <% out.println(saveFile); %> has been uploaded and inserted into Database at <%=strDateNew%>.</b>
<%
}


}
catch(Exception e)
{}
}
%>
</html>

and the error that appear is this:

type Exception report

message

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

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /upload_page.jsp at line 22

19: }
20: String file = new String(dataBytes);
21: String saveFile = file.substring(file.indexOf("filename=\"") + 10);
22: saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+ 1,saveFile.indexOf("\""));
23: int lastIndex = contentType.lastIndexOf("=");
24: String boundary = contentType.substring(lastIndex + 1,contentType.length());
25: int pos;


Stacktrace:
	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)

root cause

java.lang.StringIndexOutOfBoundsException: String index out of range: -83577
	java.lang.String.substring(String.java:1938)
	org.apache.jsp.upload_005fpage_jsp._jspService(upload_005fpage_jsp.java:79)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)

If you look at this very obvious part of the exception:

......
org.apache.jasper.JasperException: An exception occurred processing JSP page /upload_page.jsp at line 22
......
22: saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+ 1,saveFile.indexOf("\""));
............
java.lang.StringIndexOutOfBoundsException: String index out of range: -83577
java.lang.String.substring(String.java:1938)

The reason why you get the error is quite clear

then,may i know how to solve it?

Have you read the part of the stack trace I posted?:

An exception occurred processing JSP page /upload_page.jsp at line 22

22: saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+ 1,saveFile.indexOf("\""));

java.lang.StringIndexOutOfBoundsException: String index out of range: -83577
java.lang.String.substring(String.java:1938)

Perhaps you should test first in a separate main java file what you are trying to accomplish with saveFile.substring


Also I noticed that you connect to the database inside the jsp file. Don't do that. Use the tutorial that is at the top of the JSP forum with title ... MVC ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.