java - File is not getting uploaded to server -
i uploading audio file android application server.upload code running on side not file not getting upload on server.here code m using .i have commented code file getting uploaded .here code :
public void uploadfile(string path, string id) { httpurlconnection connection = null; dataoutputstream outputstream = null; string pathtoourfile = "" + path; string urlserver = "http://txtapi.com/musicapp/uploadfile.php?fileid=" + id + "&sharedby=" + sp.getstring(commonutility.phone, commonutility.getphonenumber(ct)); string lineend = "\r\n"; string twohyphens = "--"; string boundary = "*****"; int bytesread, bytesavailable, buffersize; byte[] buffer; try { fileinputstream fileinputstream = new fileinputstream(new file(pathtoourfile)); url url = new url(urlserver); connection = (httpurlconnection) url.openconnection(); // allow inputs & outputs connection.setdoinput(true); connection.setdooutput(true); connection.setusecaches(false); // enable post method connection.setrequestmethod("post"); connection.setrequestproperty("connection", "keep-alive"); connection.setrequestproperty("content-type", "multipart/form-data;boundary=" + boundary); outputstream = new dataoutputstream(connection.getoutputstream()); outputstream.writebytes(twohyphens + boundary + lineend); outputstream.writebytes("content-disposition: form-data; name=\"fileid\";filename=\"" + path + "\"" + lineend); outputstream.writebytes(lineend); bytesavailable = fileinputstream.available(); // read file // bytesread = fileinputstream.read(buffer, 0, buffersize);//file getting upload code // int =0; // while (bytesread > 0) // { // i++; // // outputstream.write(buffer, 0, buffersize); // bytesavailable = fileinputstream.available(); // buffersize = math.min(bytesavailable, maxbuffersize); // bytesread = fileinputstream.read(buffer, 0, buffersize); // } buffer = new byte[1024]; int bufferlength = 0; while ((bufferlength = fileinputstream.read(buffer)) > 0) { //add data in buffer file in file output stream (the file on sd card log.i("bytes available ", "" + fileinputstream.available()); log.i("buffer length", "" + bufferlength); outputstream.write(buffer); //add size know how downloaded } outputstream.writebytes(lineend); outputstream.writebytes(twohyphens + boundary + twohyphens + lineend); // responses server (code , message) fileinputstream.close(); outputstream.flush(); outputstream.close(); } catch (exception ex) { ex.printstacktrace(); bugsensehandler.setlogging(true); //exception handling log.i("inside catch of ", "inside catch of upload "); bugsensehandler.sendexception(ex); } { system.gc(); } }
the amount of bytes read should amount of bytes written outputstream change following:
while ((bufferlength = fileinputstream.read(buffer)) > 0) { //add data in buffer file in file output stream (the file on sd card log.i("bytes available ", "" + fileinputstream.available()); log.i("buffer length", "" + bufferlength); outputstream.write(buffer, 0, bufferlength); //add size know how downloaded }
Comments
Post a Comment