jsp - Upload an excel file into oracle database using browse button -


i uploading excel file database. when giving file path directly without using browse code works fine.

but when uploading excel file through browse button shows

filenotfoundexception:(access denied)   <%@page import="org.apache.poi.ss.usermodel.cell"%> <%@page import="org.apache.poi.ss.usermodel.row"%> <%@page  language="java" import="java.sql.*"  contenttype="text/html"  pageencoding="utf-8" %> <%@ page import ="java.util.date" %> <%@ page import ="java.io.*" %> <%@ page import ="java.io.filenotfoundexception" %> <%@ page import ="java.io.ioexception" %> <%@ page import ="java.util.iterator" %> <%@ page import ="java.util.arraylist" %> <%@ page import ="org.apache.poi.hssf.usermodel.hssfcell" %> <%@ page import ="org.apache.poi.hssf.usermodel.hssfrow" %><%@ page import ="org.apache.poi.hssf.usermodel.hssfsheet" %> <%@ page import ="org.apache.poi.hssf.usermodel.hssfworkbook" %> <%@ page import ="org.apache.poi.poifs.filesystem.poifsfilesystem" %>   <html> <head>  <title>excel file uploading</title> </head> <body>  <jsp:usebean id="connection" class="pk1.db_connection" scope="application">     <jsp:setproperty name="connection" property="*"/> </jsp:usebean>  <%!      connection con; preparedstatement ps=null; public static arraylist readexcelfile(string filename) {     /** --define arraylist         --holds arraylist of cells      */     arraylist cellarraylisstholder = new arraylist();      try{     /** creating input stream**/         fileinputstream myinput = new fileinputstream(filename);      /** create poifsfilesystem object**/         poifsfilesystem myfilesystem = new poifsfilesystem(myinput);      /** create workbook using file system**/         hssfworkbook myworkbook = new hssfworkbook(myfilesystem);       /** first sheet workbook**/         hssfsheet mysheet = myworkbook.getsheetat(0);      /** need iterate through cells.**/         iterator rowiter = mysheet.rowiterator();             while(rowiter.hasnext())             {                 hssfrow myrow = (hssfrow) rowiter.next();                   iterator celliter = myrow.celliterator();                 arraylist cellstorearraylist=new arraylist();                 while(celliter.hasnext())                 {                   hssfcell mycell = (hssfcell) celliter.next();                   cellstorearraylist.add(mycell);                 }                     cellarraylisstholder.add(cellstorearraylist);               }       }     catch (exception e){e.printstacktrace(); }     return cellarraylisstholder; } %>  <%  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);  savefile = savefile.substring(0, savefile.indexof("\n"));  savefile = savefile.substring(savefile.lastindexof("\\") + 1,savefile.indexof("\""));  out.println(savefile);  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));  fileout.flush();  fileout.close();   out.println(savefile); 

.............................. ......................................so on

**error:** type exception report  message   description server encountered internal error () prevented fulfilling request.  exception   org.apache.jasper.jasperexception: exception occurred processing jsp page /excel.jsp @ line 129  126:  127: int endpos = ((file.substring(0, boundarylocation)).getbytes()).length; 128:  129: fileoutputstream fileout = new fileoutputstream(savefile); 130:  131: fileout.write(databytes, startpos, (endpos - startpos)); 132:    stacktrace:     org.apache.jasper.servlet.jspservletwrapper.handlejspexception(jspservletwrapper.java:553)     org.apache.jasper.servlet.jspservletwrapper.service(jspservletwrapper.java:447)     org.apache.jasper.servlet.jspservlet.servicejspfile(jspservlet.java:390)     org.apache.jasper.servlet.jspservlet.service(jspservlet.java:333)     javax.servlet.http.httpservlet.service(httpservlet.java:722)     org.netbeans.modules.web.monitor.server.monitorfilter.dofilter(monitorfilter.java:393)   root cause   java.io.filenotfoundexception: super.xls (access denied)     java.io.fileoutputstream.open(native method)     java.io.fileoutputstream.<init>(fileoutputstream.java:179)     java.io.fileoutputstream.<init>(fileoutputstream.java:70)     org.apache.jsp.excel_jsp._jspservice(excel_jsp.java:208)     org.apache.jasper.runtime.httpjspbase.service(httpjspbase.java:70)     javax.servlet.http.httpservlet.service(httpservlet.java:722)     org.apache.jasper.servlet.jspservletwrapper.service(jspservletwrapper.java:419)     org.apache.jasper.servlet.jspservlet.servicejspfile(jspservlet.java:390)     org.apache.jasper.servlet.jspservlet.service(jspservlet.java:333)     javax.servlet.http.httpservlet.service(httpservlet.java:722)     org.netbeans.modules.web.monitor.server.monitorfilter.dofilter(monitorfilter.java: 

try giving other directory path default.

something

string struploaddir = "d:\\uploads\\"; \\valid directory on server

\\ other stuff doing.....

fileoutputstream fileout = new fileoutputstream(struploaddir + savefile);

i don't know write file default location should work.

p.s. should use servlet perform operation, not practice use jsp other presentation (view) logic.


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -