IndexOutOfBoundsException while uploading file - Java -


i'm getting indexoutofboundsexception while uploading zip file. reason? how can solve it?

code:-

    string sf="";     string contenttype = req.getcontenttype();     datainputstream in = new datainputstream(req.getinputstream());     int formdatalength = req.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("\""));     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;     sf = "../" + file.separator + savefile;     fileoutputstream fileout = new fileoutputstream(sf);     fileout.write(databytes, startpos, (endpos - startpos));  //----- exception occurs on line.     fileout.flush();     fileout.close(); 

stacktrace:-

java.lang.indexoutofboundsexception @ java.io.fileoutputstream.writebytes(native method) @ java.io.fileoutputstream.write(fileoutputstream.java:297) @ com.apprika.servlets.upgradepatchservlet.uploadfile(upgradepatchservlet.java:77) @ com.apprika.servlets.upgradepatchservlet.upgradeserver(upgradepatchservlet.java:40) @ com.apprika.servlets.upgradepatchservlet.dopost(upgradepatchservlet.java:30) @ javax.servlet.http.httpservlet.service(httpservlet.java:637) @ javax.servlet.http.httpservlet.service(httpservlet.java:717) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:290) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:233) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:191) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:128) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:102) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:109) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:293) @ org.apache.coyote.http11.http11processor.process(http11processor.java:849) @ org.apache.coyote.http11.http11protocol$http11connectionhandler.process(http11protocol.java:583) @ org.apache.tomcat.util.net.jioendpoint$worker.run(jioendpoint.java:454) @ java.lang.thread.run(thread.java:636) 

server running on linux , uploading file windows.


i've tried apache commons file upload still not getting success.

    try {                  diskfileitemfactory factory = new diskfileitemfactory();                  servletfileupload upload = new servletfileupload(factory);                  list<fileitem> items = upload.parserequest(req);                  (fileitem item : items) {                     if (!item.isformfield()) {                         inputstream = item.getinputstream();                         fileoutputstream os = new fileoutputstream("/up/" + item.getname());                         byte[] b = new byte[4096];                         int bytesread = 0;                         while ((bytesread = is.read(b)) != -1) {                             os.write(b, 0, bytesread);                         }                         os.flush();                         os.close();                         is.close();                     }                  }             }catch (exception e) {                 e.printstacktrace();             } 

now gives error java.io.filenotfoundexception: /up (is directory) `

you converting uploaded bytes string without specifying encoding. default encoding on server not necessary same 1 used upload.
therefore strings length not guaranteed same byte arrays length, computing offsets array based on string lead sorts of problems.

i recommend using apache commons fileupload instead of trying parsing getting details right not easy.
starting point answer: how upload files server using jsp/servlet?


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -