Hi to ALL
I tried to upload file using JSP and Oracle.
The first page is page.jsp which the user choose the file
///////////////

<%@ page language="java" %>
<%@ page session="true" %>

<HTml>
<HEAD><TITLE>Display file upload form to the user</TITLE></HEAD> 

<BODY>
    <%=session.getAttribute("Name")%>
<FORM ENCTYPE="multipart/form-data" ACTION=
"upload_page.jsp" METHOD=POST>
<br><br><br>
<center>
    <table border="2" >
<tr>
    <center>
        <td colspan="2">
        <p align="center"><B>UPLOAD THE FILE</B><center>
            </td>
            </tr>
<tr><td><b>Choose the file To Upload:</b>
</td>
<td><INPUT NAME="file" TYPE="file"></td></tr>
<tr><td colspan="2">
<p align="right"><INPUT TYPE="submit" VALUE="Send File" ></p></td></tr>
<table>
</center> 
</FORM>
</BODY>
</HTML>

//////////////
then the uploade_page.jsp which the file insert into the database
////////////

<%@ page import="java.io.*,java.sql.*" %>
<%@ page session="true" %>

<html>


<%
            String contentType = 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);
            System.out.println("saveFile=" + saveFile);
            saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+ 1,saveFile.indexOf("\""));
            System.out.println("saveFile" + saveFile);
            saveFile = file.substring(file.indexOf("filename=\"") + 10);
            saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
            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));
            %>
            <b>File <% out.println(saveFile); %> has been uploaded and inserted into Database.</b>
            <%

            String line = null;
            String value=null;
            //
            Connection con=null;
             ResultSet rs=null;
             Statement pst=null;
            //
            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);
            //
                    String driverName = "oracle.jdbc.driver.OracleDriver";
                    Class.forName(driverName).newInstance();
                        con=DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe", "MID", "123");
                    pst=con.createStatement();
            //
            String username = session.getAttribute("user_id").toString();
            String filename=saveFile;
             java.util.Date today = new java.util.Date();
            int val = pst.executeUpdate("insert into file_share(File_id, username,File_Date,File_doc,filename) values(Fileid.nextval,'"+username+"','"+new java.sql.Timestamp(today.getTime())+"','"+value+"','"+filename+"')");
            }
            catch(Exception e)
            {}
            }
%>
</html>

//////////
the problem in upload_page.jsp
IT occur this message
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 24

21: String file = new String(dataBytes);
22: String saveFile = file.substring(file.indexOf("filename=\"") + 10);
23: System.out.println("saveFile=" + saveFile);
24: saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+ 1,saveFile.indexOf("\""));
25: System.out.println("saveFile" + saveFile);
26: saveFile = file.substring(file.indexOf("filename=\"") + 10);
27: saveFile = saveFile.substring(0, saveFile.indexOf("\n"));


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:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)

root cause

java.lang.StringIndexOutOfBoundsException: String index out of range: -81920
java.lang.String.substring(String.java:1768)
org.apache.jsp.upload_005fpage_jsp._jspService(upload_005fpage_jsp.java:78)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)


please can any one help me in my project

This article has been dead for over six months. Start a new discussion instead.