问题
How to upload the video or image file to php server.Anyone provide some sample app or source to upload the file in php server please.
回答1:
I'd suggest creating an AsyncTask to handle your upload and then use the Apache Libs to do a POST to your server. I have a system in place now that posts images to a Linux server and then PHP accepts the POST and saves the image data. Try something like this, you'll need to get the commons-io jar and the httpmime jar from Apache if I remember correctly.
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://www.mywebserver.com/upload");
MultipartEntity multipart = new MultipartEntity();
multipart.addPart("photo",bitmapdata);
postRequest.setEntity(multipart);
HttpResponse response = httpClient.execute(postRequest);
InputStream content = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
StringBuilder serverMsg = new StringBuilder();
String line = "";
while((line = reader.readLine()) != null){ serverMsg.append(line + "\n"); }
content.close();
Then over in PHP, check for the POST and save the image using whatever library or code you prefer. I'm sure you could easily adjust this code to send video data instead of a photo.
来源:https://stackoverflow.com/questions/5089891/android-video-upload-to-php-server