Android Video Upload to Php server?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-18 09:33:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!