i have program upload excel file , it's content put in database.
this part of code works when try upload excel file, here code in uploading excel file:
public class uploadfile extends fileuploadaction { public boolean processrequest(httpservlet servlet, httpservletrequest request, httpservletresponse response) { if ((request.getcontenttype() != null)){ try { properties appproperties = getappproperties(); string dbmap = appproperties.getproperty("dbmap"); db db = getdbconnection(dbmap); string filesavepathtemp = appproperties.getproperty("filesavepathtemp"); multipartparser parser = new multipartparser(request, 10485760); part _part = null; string urlvars = ""; string fname = ""; while ((_part = parser.readnextpart()) != null) { if (_part.isfile()) { filepart fpart = (filepart)_part; long filesize = 0l; fname = request.getparameter("filename"); if (fname != null) { filesize = fpart.writeto(new file(filesavepathtemp + system.getproperty("file.separator") + fname)); urlvars = urlvars + "filename=" + urlencoder.encode(fname, "utf-8") + "&"; } continue; } parampart ppart = (parampart)_part; urlvars = urlvars + ppart.getname() + "=" + urlencoder.encode(ppart.getstringvalue(), "utf-8") + "&"; } response.sendredirect("appservlet?" + urlvars); return false; } catch (exception e) { system.out.println("error on uploadfile class: " + e.tostring()); request.setattribute("msgtitle", "file upload failed"); request.setattribute("message", e.tostring()); } } return true; } }
here code getting contents of excel file:
file srcfile = new file(fname); try{ workbook workbook = workbook.getworkbook(srcfile); //new file(dirname+filename)); sheet mysheet = workbook.getsheet(0); int p = mysheet.getrows(); (int row = 0; row <= mysheet.getrows()-1; row++) { string material = mysheet.getcell(0, row).getcontents(); double price = double.parsedouble(mysheet.getcell(1,row).getcontents()); //insert contents of excelfile temporary table db.sqlexecute("insert temp_4252013 (material,price) values ('" +material+"','"+price+"')"); db.sqlexecute("commit"); } }catch(exception e){ e.printstacktrace(); }
but when put code above getting contents of excel file error:
java.io.filenotfoundexception: test.xls (the system cannot find file specified) @ java.io.fileinputstream.open(native method) @ java.io.fileinputstream.<init>(fileinputstream.java:120) @ sources.uploadaip.actions.uploadfile.processrequest(uploadfile.java:71) @ com.ti.ffw.libs.webapp.appservlet.doaction(appservlet.java:368) @ com.ti.ffw.libs.webapp.appservlet.processrequest(appservlet.java:251) @ com.ti.ffw.libs.webapp.appservlet.dopost(appservlet.java:206) @ javax.servlet.http.httpservlet.service(httpservlet.java:641) @ javax.servlet.http.httpservlet.service(httpservlet.java:722) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:304) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:210) @ org.netbeans.modules.web.monitor.server.monitorfilter.dofilter(monitorfilter.java:393) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:243) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:210) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:224) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:169) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:472) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:168) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:100) @ org.apache.catalina.valves.accesslogvalve.invoke(accesslogvalve.java:929) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:118) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:405) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:964) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:515) @ org.apache.tomcat.util.net.jioendpoint$socketprocessor.run(jioendpoint.java:304) @ java.util.concurrent.threadpoolexecutor$worker.runtask(threadpoolexecutor.java:895) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:918) @ java.lang.thread.run(thread.java:662)
what wrong or need make work?
you wrote file filesavepathtemp + system.getproperty("file.separator") + fname
reading file fname
.
that's why there's java.io.filenotfoundexception
.
try:
file srcfile = new file(filesavepathtemp + system.getproperty("file.separator") + fname);
Comments
Post a Comment