when i am trying to upload a image from user and trying to save into oracle9i database it will always give this error...... if any one know the solution...please tell me how to remove that 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: An exception occurred processing JSP page /upload.jsp at line 27 24: String file = new String(dataBytes); 25: String saveFile = file.substring(file.indexOf("filename=\"") + 10); 26: System.out.println("saveFile=" + saveFile); 27: saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+ 1,saveFile.indexOf("\"")); 28: System.out.println("saveFile" + saveFile); 29: saveFile = file.substring(file.indexOf("filename=\"") + 10); 30: saveFile = saveFile.substring(0, saveFile.indexOf("\n")); Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) root cause java.lang.StringIndexOutOfBoundsException: String index out of range: -28161 java.lang.String.substring(Unknown Source) org.apache.jsp.upload_jsp._jspService(upload_jsp.java:80) 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:377) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) note The full stack trace of the root cause is available in the Apache Tomcat/6.0.29 logs.
Apache Tomcat/6.0.29
We have used MySQl database.
1)page.jsp:
<%@ page language="java" %> <HTML> <HEAD><TITLE>Display file upload form to the user</TITLE></HEAD> <BODY> <FORM ENCTYPE="multipart/form-data" ACTION="upload.jsp" METHOD=POST> <br><br><br> <center> <table border="0" bgcolor=#ccFDDEE> <tr> <center> <td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td> </tr> <tr> <td colspan="2" align="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" align="center"> </td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="Send File"> </td> </tr> <table> </center> </FORM> </BODY> </HTML>
2)uoload.jsp:
<%@ page import="java.io.*" %> <%@ page import="java.sql.*" %> <%@ page import="java.util.zip.*"%> <% String saveFile=""; 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); 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; File ff = new File(saveFile); FileOutputStream fileOut = new FileOutputStream(ff); fileOut.write(dataBytes, startPos, (endPos - startPos)); fileOut.flush(); fileOut.close(); %><Br><table border="2"><tr><td><b>You have successfully upload the file:</b> <%out.println(saveFile);%></td></tr></table> <% Connection connection = null; String connectionURL = "jdbc:mysql://localhost:3306/test"; ResultSet rs = null; PreparedStatement psmnt = null; FileInputStream fis; try{ Class.forName("com.mysql.jdbc.Driver").newInstance(); connection = DriverManager.getConnection(connectionURL, "root", "root"); File f = new File(saveFile); psmnt = connection.prepareStatement("insert into file(file_data) values(?)"); fis = new FileInputStream(f); psmnt.setBinaryStream(1, (InputStream)fis, (int)(f.length())); int s = psmnt.executeUpdate(); if(s>0){ System.out.println("Uploaded successfully !"); } else{ System.out.println("Error!"); } } catch(Exception e){e.printStackTrace();} } %>
3)For oracle database, use BLOB datatype for image.
dear sir or madam, i wont to How to Store Image Into The Database Using netbeans
Ads