Please help me in upload code

Reply

Join Date: Apr 2009
Posts: 1
Reputation: UAEU Student is an unknown quantity at this point 
Solved Threads: 0
UAEU Student UAEU Student is offline Offline
Newbie Poster

Please help me in upload code

 
0
  #1
May 9th, 2009
Hi to ALL
I tried to upload file using JSP and Oracle.
The first page is page.jsp which the user choose the file
///////////////
  1. <%@ page language="java" %>
  2. <%@ page session="true" %>
  3.  
  4. <HTml>
  5. <HEAD><TITLE>Display file upload form to the user</TITLE></HEAD>
  6.  
  7. <BODY>
  8. <%=session.getAttribute("Name")%>
  9. <FORM ENCTYPE="multipart/form-data" ACTION=
  10. "upload_page.jsp" METHOD=POST>
  11. <br><br><br>
  12. <center>
  13. <table border="2" >
  14. <tr>
  15. <center>
  16. <td colspan="2">
  17. <p align="center"><B>UPLOAD THE FILE</B><center>
  18. </td>
  19. </tr>
  20. <tr><td><b>Choose the file To Upload:</b>
  21. </td>
  22. <td><INPUT NAME="file" TYPE="file"></td></tr>
  23. <tr><td colspan="2">
  24. <p align="right"><INPUT TYPE="submit" VALUE="Send File" ></p></td></tr>
  25. <table>
  26. </center>
  27. </FORM>
  28. </BODY>
  29. </HTML>
//////////////
then the uploade_page.jsp which the file insert into the database
////////////
  1. <%@ page import="java.io.*,java.sql.*" %>
  2. <%@ page session="true" %>
  3.  
  4. <html>
  5.  
  6.  
  7. <%
  8. String contentType = request.getContentType();
  9. if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
  10. DataInputStream in = new DataInputStream(request.getInputStream());
  11. int formDataLength = request.getContentLength();
  12. byte dataBytes[] = new byte[formDataLength];
  13. int byteRead = 0;
  14. int totalBytesRead = 0;
  15.  
  16. while (totalBytesRead < formDataLength) {
  17. byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
  18. totalBytesRead += byteRead;
  19. }
  20.  
  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"));
  28. saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+ 1,saveFile.indexOf("\""));
  29. int lastIndex = contentType.lastIndexOf("=");
  30. String boundary = contentType.substring(lastIndex + 1,contentType.length());
  31. int pos;
  32.  
  33. pos = file.indexOf("filename=\"");
  34. pos = file.indexOf("\n", pos) + 1;
  35. pos = file.indexOf("\n", pos) + 1;
  36. pos = file.indexOf("\n", pos) + 1;
  37. int boundaryLocation = file.indexOf(boundary, pos) - 4;
  38. int startPos = ((file.substring(0, pos)).getBytes()).length;
  39. int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
  40.  
  41. FileOutputStream fileOut = new FileOutputStream(saveFile);
  42. fileOut.write(dataBytes, startPos, (endPos - startPos));
  43. %>
  44. <b>File <% out.println(saveFile); %> has been uploaded and inserted into Database.</b>
  45. <%
  46.  
  47. String line = null;
  48. String value=null;
  49. //
  50. Connection con=null;
  51. ResultSet rs=null;
  52. Statement pst=null;
  53. //
  54. try{
  55. StringBuilder contents = new StringBuilder();
  56. BufferedReader input = new BufferedReader(new FileReader(saveFile));
  57. while (( line = input.readLine()) != null){
  58. contents.append(line);
  59. }
  60. value = contents.toString();
  61. System.out.println("Value:"+value);
  62. //
  63. String driverName = "oracle.jdbc.driver.OracleDriver";
  64. Class.forName(driverName).newInstance();
  65. con=DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe", "MID", "123");
  66. pst=con.createStatement();
  67. //
  68. String username = session.getAttribute("user_id").toString();
  69. String filename=saveFile;
  70. java.util.Date today = new java.util.Date();
  71. 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+"')");
  72. }
  73. catch(Exception e)
  74. {}
  75. }
  76. %>
  77. </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
Last edited by Ezzaral; May 9th, 2009 at 10:45 am. Reason: Added [code] [/code] tags. Please use them to format any code that you post.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 463
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Please help me in upload code

 
0
  #2
May 25th, 2009
Use utils from apache - commons-fileupload-1.0.jar - to upload.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC