PhoneGap file Upload (java,php)
Phpnegap File Upload
Description of the Filetransfer object in PhoneGap:
Http://docs.phonegap.com/en/1.6.1/cordova_file_file.md.html#FileTransfer
?
Today's code for the students to organize. Write it down here for future reference
?
Filetransfer? is an object, that allows, upload files to a server, or download files from a server.
?
Used to transfer files to the server side
?
It has an example, which is written in a very detailed, one of the following:
var options = new Fileuploadoptions (); options.filekey= "File"; Options.filename=fileuri.substr (Fileuri.lastindexof (' /') +1); options.mimetype= "Text/plain";
?
The full version of the front end can be consulted: http://www.oschina.net/question/200138_34919
?
Today in the image upload, how can not be in the back end to get the file stream, the Java logic is as follows:
int max_size = 102400 * 102400;datainputstream in = null; FileOutputStream fileout = null; String ContentType = Request.getcontenttype (); try {if (Contenttype.indexof ("Multipart/form-data") >= 0) {in = new data InputStream (Request.getinputstream ()); int formdatalength = Request.getcontentlength (); if (Formdatalength > MAX_ SIZE) {return;} byte databytes[] = new Byte[formdatalength];int Byteread = 0;int Totalbytesread = 0;while (Totalbytesread < FormDataLen gth) {byteread = In.read (Databytes, Totalbytesread, formdatalength); Totalbytesread + = Byteread;} String file = new string (databytes); String saveFile = file.substring (File.indexof ("filename=\") + ten) 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 ;//Gets the start position of the file data int startpos = ((file.substring (0, POS)). GetBytes ()). length;//Get the end of the file data int endpos = (( File.substring (0, Boundarylocation)). GetBytes ()). length;//Create the file's write-out class fileout = new FileOutputStream ( System.getproperty ("Java.io.tmpdir") + "/aa.jpg");//Save the file Data Fileout.write (Databytes, Startpos, (Endpos-startpos)); Fileout.close ();} } catch (Exception ex) {throw new Servletexception (Ex.getmessage ());}
? later found that the original is less a fatal parameter: Options.chunkedmode = false;
?
For an analysis of Chunkedmode, see: Http://www.issociate.de/board/post/368589/How_to_force_the_apache_transfer_the_data_in_ Chunked_mode?. Html
?
The main idea is: If the file length can not be predicted, using Chuckedmode mode transmission, now transmission is the picture, size already know, do not know why Apache server processing, must be set to False chunkedmode, this upload success, feel classmates pointing
?
For this parameter, I have also compared the PHP version of the file upload: PHP server side of this write:
If retrieving an imageelse if (isset ($_get[' image ')) {$file = $dirname. " /". $_get[' image ']; Specify as JPEG header (' content-type:image/jpeg '); Resize image for Mobile list ($width, $height) = getimagesize ($file); $newWidth = 120.0; $size = $newWidth/$width; $newHeight = $height * $size; $resizedImage = Imagecreatetruecolor ($newWidth, $newHeight); $image = Imagecreatefromjpeg ($file); Imagecopyresampled ($resizedImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); imagejpeg ($resizedImage, NULL, 80); }If displaying imageselse {$baseURI = "/http". $_server[' server_name ']. ': '. $_server[' server_port '].$_server[' Request_uri ']; $images = Scandir ($dirname); $ignore = Array (".", ".."); if ($images) {foreach ($images as $curimg) {if (!in_array ($curimg, $ignore)) {echo "Ima GE: ". $curimg."
"; echo "
"; }}} else {echo "No images on server"; }}?>
PHP Code reference: Https://github.com/brycecurtis/articles/tree/master/CameraUpload
?
Code description and commentary in: https://www.ibm.com/developerworks/mydeveloperworks/blogs/94e7fded-7162-445e-8ceb-97a2140866a9/entry/ upload_a_picture_using_phonegap_on_android8?lang=en
?
PHP version of this front-end code:
Verify server has been entered server = document.getElementById (' ServerURL '). Value; if (server) { //Specify transfer options var options = new Fileuploadoptions (); options.filekey= "File"; Options.filename=imageuri.substr (Imageuri.lastindexof ('/') +1); Options.mimetype= "Image/jpeg"; Options.chunkedmode = false; Transfer picture to server var ft = new Filetransfer (); Ft.upload (Imageuri, Server, function (r) { document.getElementById (' Camera_status '). InnerHTML = "Upload Successful: "+r.bytessent+" bytes uploaded. "; }, Function (Error) { document.getElementById (' Camera_ Status '). InnerHTML = "Upload Failed:code =" +error.code; }, Options); }
? is indeed a setting that contains Chunkedmode=false and runs through the native.