java - Uploading videos not working while images and text files uploaded in Android -
i trying upload video php server on localhost. have uploaded images , text files same code not work videos. please tell me what's wrong code.
here server side script:
<?php $mysql_host = "localhost"; $mysql_database = "test"; $mysql_user = "root"; $mysql_password = ""; $con = mysql_connect($mysql_host, $mysql_user, $mysql_password); mysql_select_db($mysql_database) or die("unable select database"); $target_path = "uploads/"; $target_path = $target_path . basename( $_files['uploadedfile']['name']); echo $target_path; if(move_uploaded_file($_files['uploadedfile']['tmp_name'], $target_path)) { echo "the file ". basename( $_files['uploadedfile']['name']). " has been uploaded"; $sql="insert images set url='$target_path'"; mysql_query($sql); } else{ echo "there error uploading file, please try again!"; } ?>
here client side code:
package com.example.imageupload; import java.io.bufferedreader; import java.io.datainputstream; import java.io.dataoutputstream; import java.io.file; import java.io.fileinputstream; import java.io.ioexception; import java.io.inputstreamreader; import java.net.httpurlconnection; import java.net.url; import android.app.activity; import android.content.intent; import android.database.cursor; import android.net.uri; import android.os.asynctask; import android.os.bundle; import android.provider.mediastore; import android.util.log; import android.view.menu; public class mainactivity extends activity { int req_code_pick_image = 1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); intent photopickerintent = new intent(intent.action_pick); photopickerintent.settype("image/*"); startactivityforresult(photopickerintent, req_code_pick_image); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { // todo auto-generated method stub super.onactivityresult(requestcode, resultcode, data); switch (requestcode) { case 1: if (resultcode == result_ok) { uri selectedimage = data.getdata(); string[] filepathcolumn = { mediastore.images.media.data }; cursor cursor = getcontentresolver().query(selectedimage, filepathcolumn, null, null, null); cursor.movetofirst(); int columnindex = cursor.getcolumnindex(filepathcolumn[0]); string filepath = cursor.getstring(columnindex); cursor.close(); // bitmap yourselectedimage = // bitmapfactory.decodefile(filepath); new uploadfile().execute(filepath); } } } private class uploadfile extends asynctask<string, void, void> { @override protected void onpreexecute() { // todo auto-generated method stub super.onpreexecute(); log.d("start", "start"); } @override protected void doinbackground(string... params) { // todo auto-generated method stub try { httpurlconnection connection = null; dataoutputstream outputstream = null; datainputstream inputstream = null; //string pathtoourfile="/mnt/sdcard/code.txt"; string pathtoourfile = params[0]; log.d("start", params[0]); string urlserver = "http://10.0.2.2/uploadfile.php"; string lineend = "\r\n"; string twohyphens = "--"; string boundary = "*****"; string response ; int bytesread, bytesavailable, buffersize; byte[] buffer; int maxbuffersize = 1 * 1024 * 1024; 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=\"uploadedfile\";filename=\"" + pathtoourfile + "\"" + lineend); outputstream.writebytes(lineend); bytesavailable = fileinputstream.available(); buffersize = math.min(bytesavailable, maxbuffersize); buffer = new byte[buffersize]; // read file bytesread = fileinputstream.read(buffer, 0, buffersize); while (bytesread > 0) { outputstream.write(buffer, 0, buffersize); bytesavailable = fileinputstream.available(); buffersize = math.min(bytesavailable, maxbuffersize); bytesread = fileinputstream.read(buffer, 0, buffersize); } outputstream.writebytes(lineend); outputstream.writebytes(twohyphens + boundary + twohyphens + lineend); bufferedreader in = new bufferedreader(new inputstreamreader( connection.getinputstream())); log.d("buffrerreader", "" + in); if (in != null) { response = convertstreamtostring(in); log.e("final_response-length",""+response.length()); log.e("final_response", response); } // responses server (code , message) int serverresponsecode = connection.getresponsecode(); string serverresponsemessage = connection .getresponsemessage(); fileinputstream.close(); outputstream.flush(); outputstream.close(); } catch (exception ex) { // exception handling } } catch (exception ex) { ex.printstacktrace(); } return (null); } @override protected void onpostexecute(void result) { // todo auto-generated method stub super.onpostexecute(result); log.d("end", "end"); } } public string convertstreamtostring(bufferedreader is) throws ioexception { if (is != null) { stringbuilder sb = new stringbuilder(); string line; try { while ((line = is.readline()) != null) { sb.append(line).append(""); } } { is.close(); } return sb.tostring(); } else { return ""; } }
}
change value of upload_max_filesize in php.ini default 2m desired limit
Comments
Post a Comment