Although a bunch of online code reduces the complexity of the attempt, there are still various problems in the real operation. It is a big problem to directly run without source code, after trying for a day, I finally learned about the path and shared it with everyone. For native POST upload, because I will not upload files in this way, which is inefficient and difficult to use, so I
Although a bunch of online code reduces the complexity of the attempt, there are still various problems in the real operation. It is a big problem to directly run without source code, after trying for a day, I finally learned about the path and shared it with everyone. For native POST upload, because I will not upload files in this way, which is inefficient and difficult to use, so I
Although a bunch of online code reduces the complexity of the attempt, there are still various problems in the real operation. It is a big problem to directly run without source code, after trying for a day, I finally learned about the path and shared it with everyone. For native POST upload, because I will not upload files in this way, which is inefficient and difficult to use, so I did not go into the specific code and principles, but the code I provided is feasible. Let's start by uploading images to the server in PHP browser, then we will explain how android uploads and how android parameters are constructed.
1. upload images to the PHP server through the web page
For details, refer to my article "Do not refresh to implement the image upload function", which contains all the source code. Below I will only post part of the front and back-end code to explain a problem.
Front-end code:
Note that an input control type = 'file', name = 'upfile'
Some background code
$ FilePath = "temp/"; if (! File_exists ($ filePath) {// if the specified folder does not exist, create the folder mkdir ($ filePath, 0777);} // redefine the file path and file name // separate file path, the result of separation is pathinfo (). An associated array contains path information. It includes the following array units: dirname, basename, and extension. $ Houzhui = pathinfo ($ _ FILES ['upfile'] ['name']); // determines whether the format is image format. if (! In_array ($ houzhui ['extension'], array ('jpg ', 'gif', 'png', 'jpg ', 'gif', 'png '))) {$ result = 2;} else {$ name = $ filePath. "newName ". '. '. $ houzhui ['extension']; // move the uploaded temporary file to a new file. // if the file is successfully moved, output The relevant content if (move_uploaded_file ($ _ FILES ['upfile'] ['tmp _ name'], $ name) {$ result = 0 ;} // else {$ result =-1 ;}}
Pay attention to a variable: $ _ FILES ['upfile'] ['name']. The first parameter of $ _ FILES is upfile, which indicates receiving, The uploaded file
Ii. android native POST File Upload
I can't use it. I don't want to talk about it in detail. Let's look at the source code.(Source code is at the end)
Iii. httpClient 4.x: uploads files to the PHP server
On:
Android:
/* Upload the file to the Server, uploadUrl: Processing page of the received file */private void uploadFile (String uploadUrl) throws Exception {HttpClient httpclient = new DefaultHttpClient (); httpclient. getParams (). setParameter (CoreProtocolPNames. PROTOCOL_VERSION, HttpVersion. HTTP_1_1); HttpPost httppost = new HttpPost (uploadUrl); MultipartEntity entity = new MultipartEntity (); File file = new File (srcPath); FileBody fileBody = new FileBody (file); en Tity. addPart ("uploadedfile", fileBody); httppost. setEntity (entity); HttpResponse response = route cute (httppost); HttpEntity resEntity = response. getEntity (); if (resEntity! = Null) {Toast. makeText (this, EntityUtils. toString (resEntity), Toast. LENGTH_LONG). show () ;}httpclient. getConnectionManager (). shutdown ();}
Note the following code:
MultipartEntity entity = new MultipartEntity();File file = new File(srcPath);FileBody fileBody = new FileBody(file);entity.addPart("uploadedfile", fileBody);httppost.setEntity(entity);
2. Next sentence: entity. addPart ("uploadedfile", fileBody); to add our fileBody to the form, the previous uploadedfile is equivalent to the value of the name field of input type = file name = 'xxx', Which is why PHP receives, $ _ FILES ['uploadfile'] ['name']. The first parameter of $ _ FILES must be related to entity. addPart ("uploadedfile", fileBody); the first parameter is the same;
Check the server (PHP ):
It is not difficult. Pay attention to the following: the first parameter of $ _ FILES ['uploadfile'] ['tmp _ name'] must be the same as the entity in android code. addPart ("uploadedfile", fileBody); the first parameter name remains consistent;
In the source code, this method is slightly expanded and two images are uploaded to the PHP server at the same time;
Source code:Http://download.csdn.net/detail/harvic880925/6769671(Do not score, share only)
Instance running method:
1. Use a real machine for testing so that the server and the real machine are in the same LAN;
2. Copy desert.jpgand 1.jpg to the mobile phone sdcardroot directory, where Desert.jpg is located: sdcard/Desert.jpg and sdcard/1.jpg;
3. Place receive_file.php in the Apache server htdocs directory;
4. Change 222.195.151.19 In the android code to the address of the PHP server on your computer;
Http://blog.csdn.net/harvic880925/article/details/17565481