Android allow multiple file upload (Max 150 MB) to PHP Server -
i have allow user upload multiple files(can image/video/audio)in single request android application php server. using rest web service.
for functionality, using following code:
/* upload multiple documents */ public void uploadfile() { string charset = "utf-8"; file[] uploadfilearray = new file[medialist.size()]; (int = 0; < medialist.size(); i++) { uploadfilearray[i] = new file(medialist.get(i).getmediapath()); } try { multipartutility multipart = new multipartutility(uploadserveruri, charset); (int = 0; < medialist.size(); i++) { if (isimage)) { multipart.addfilepart("image_doc[]", uploadfilearray[i]); } else if (isvideo) { multipart.addfilepart("video_doc[]", uploadfilearray[i]); } else if (isaudio) { multipart.addfilepart("audio_doc[]", uploadfilearray[i]); } } list<string> responseuploaddocument = multipart.finish(); system.out.println("server replied:"); (string line : responseuploaddocument) { system.out.println(line); responseuploaddocumentstring = line; } if (responseuploaddocumentstring != null) { jsonobject jsonobj = new jsonobject(responseuploaddocumentstring); statusuploaddoc = jsonobj.getstring("status"); } } catch (exception e) { e.printstacktrace(); } }
and multipartutility class follows:
public class multipartutility { private final string boundary; private static final string line_feed = "\r\n"; private httpurlconnection httpconn; private string charset; private outputstream outputstream; private printwriter writer; public multipartutility(string requesturl, string charset) throws ioexception { this.charset = charset; boundary = "===" + system.currenttimemillis() + "==="; url url = new url(requesturl); httpconn = (httpurlconnection) url.openconnection(); ***httpconn.setchunkedstreamingmode(0);*** httpconn.setusecaches(false); httpconn.setdooutput(true); // indicates post method httpconn.setdoinput(true); httpconn.setrequestproperty("content-type", "multipart/form-data; boundary=" + boundary); httpconn.setrequestproperty("user-agent", "codejava agent"); httpconn.setrequestproperty("test", "bonjour"); outputstream = httpconn.getoutputstream(); writer = new printwriter(new outputstreamwriter(outputstream, charset), true); } public void addformfield(string name, string value) { writer.append("--" + boundary).append(line_feed); writer.append("content-disposition: form-data; name=\"" + name + "\"") .append(line_feed); writer.append("content-type: text/plain; charset=" + charset).append( line_feed); writer.append(line_feed); writer.append(value).append(line_feed); writer.flush(); } public void addfilepart(string fieldname, file uploadfile) throws ioexception { string filename = uploadfile.getname(); writer.append("--" + boundary).append(line_feed); writer.append( "content-disposition: form-data; name=\"" + fieldname + "\"; filename=\"" + filename + "\"") .append(line_feed); writer.append( "content-type: " + urlconnection.guesscontenttypefromname(filename)) .append(line_feed); writer.append("content-transfer-encoding: binary").append(line_feed); writer.append(line_feed); writer.flush(); fileinputstream inputstream = new fileinputstream(uploadfile); byte[] buffer = new byte[4096]; int bytesread = -1; while ((bytesread = inputstream.read(buffer)) != -1) { outputstream.write(buffer, 0, bytesread); } outputstream.flush(); inputstream.close(); writer.append(line_feed); writer.flush(); } public void addheaderfield(string name, string value) { writer.append(name + ": " + value).append(line_feed); writer.flush(); } public list<string> finish() throws ioexception { list<string> response = new arraylist<string>(); writer.append(line_feed).flush(); writer.append("--" + boundary + "--").append(line_feed); writer.close(); // checks server's status code first int status = httpconn.getresponsecode(); if (status == httpurlconnection.http_ok) { bufferedreader reader = new bufferedreader(new inputstreamreader( httpconn.getinputstream())); string line = null; while ((line = reader.readline()) != null) { response.add(line); } reader.close(); httpconn.disconnect(); } else { throw new ioexception("server returned non-ok status: " + status); } return response; } }
but problem is:
on local server:
- allow upload till 16mb [if setchunkedstremmingmode(0) not set]
- allow upload till 150mb [if setchunkedstremmingmode(0) set]
on live server:
- allow upload till 16mb [if setchunkedstremmingmode(0) not set]
- disallow upload single kb file [if setchunkedstremmingmode(0) set]
my both local , live servers have same configurations. don't understand why setchunkedstremmingmode(0) doesn't work live server.
i have project can upload image/file size of upto 48-50 mb. [i'm using retrofit network communication.] here link sample project: https://github.com/deardhruv/androidprojectstarter
here upload code: https://github.com/deardhruv/androidprojectstarter/blob/master/project_starter/src/com/deardhruv/projectstarter/activities/uploadfileactivity.java
code:
private void startuploading(final file file) { if (file == null || !ispicturevalid(file.getabsolutepath())) { showmsg("file not valid, try again."); } else if (file.exists()) { showprogressdialog(); string mimetype = "image/jpeg" // "application/octet-stream" // "multipart/form-data" // "image/*" // "multipart/mixed" ; typedfile typedfile = new typedfile(mimetype, file); mapiclient.uploadfile(upload_file_request_tag, typedfile); } else { showmsg("file corrupted."); } }
here server side php upload code: https://github.com/deardhruv/androidprojectstarter/blob/master/3rd_party/html/php/upload.php
code:
class imageuploadstatus { public $result_code = 0; public $message = "image upload failed.";} class imageresults { public $name = ""; public $url = ""; public $size = ""; } $status = new imageuploadstatus (); $results = new imageresults (); if ($_files ["file"] ["error"] > 0) { header ( "http/1.1 400 bad request" ); // echo "error: " . $_files ["file"] ["error"] . "<br /> \n"; $status->message = "error: " . $_files ["file"] ["error"]; } else { if ($_files ["file"] ["error"] > 0) { // echo "error: " . $_files ["file"] ["error"] . "<br /> \n"; $status->message = "error: " . $_files ["file"] ["error"]; } else { // echo "upload: " . $_files ["file"] ["name"] . "<br /> \n"; // echo "type: " . $_files ["file"] ["type"] . "<br /> \n"; // echo "size: " . ($_files ["file"] ["size"] / 1024) . " kb<br /> \n"; // echo "stored in: " . $_files ["file"] ["tmp_name"] . "<br /> \n"; } $target_path = "../api/uploads/"; $target_path = $target_path . basename ( $_files ['file'] ['name'] ); //if (is_uploaded_file ( $_files ['uploadedfile'] ['tmp_name'] )) { // echo "there error uploading file, please try again!"; //$status->message = "there error uploading file, please try again!"; //} if (move_uploaded_file ( $_files ['file'] ['tmp_name'], $target_path )) { // echo "the file " . basename ( $_files ['file'] ['name'] ) . " has been uploaded"; $status->message = "image upload successful"; $status->result_code = 1; $results->name = $_files ["file"] ["name"] . ""; $results->url = $target_path . ""; $results->size = ($_files ["file"] ["size"] / 1); // 1024 convert in kb } else { // echo "there error moving file, please try again!"; //$status->message = "there error moving file, please try again!"; switch($_files['file']['error']){ case 0: //no error; possible file attack! $status->message = "there problem upload."; break; case 1: //uploaded file exceeds upload_max_filesize directive in php.ini $status->message = "the file trying upload big."; break; case 2: //uploaded file exceeds max_file_size directive specified in html form $status->message = "the file trying upload big."; break; case 3: //uploaded file partially uploaded $status->message = "the file trying upload partially uploaded."; break; case 4: //no file uploaded $status->message = "you must select image upload."; break; default: //a default error, in case! :) $status->message = "there problem upload."; break; } } $response = array ( 'status' => $status, 'results' => $results ); // echo (json_encode ( $status )); // echo (json_encode ( $results )); echo (json_encode ( $response )); }
i guess might helpful.
you might need give max size of upload file in php configuration.
Comments
Post a Comment