如何使用多上传图片到PHP服务器 [英] How to upload image in to php server using Multipart
问题描述
我一直头疼,如何上传图像到server.It是me.but新任务的大混乱了我,我搜索了计算器和Google搜索。但我得到的问题。
I have been headache , how to upload image in to server.It is new task for me.but big confusing for me.i searched on stackoverflow and googled. but i getting problems.
的我的本意是从SD卡上传的照片和相机拍照,并上传到服务器。的
My intention is Upload photo from sdcard and take photo from camera and upload to server.
在IOS,这个任务已经完成,在IOS PHP得到这个类型的:当发布PHP端简单的命中此信息
In ios, this task already done, in ios php get this type of: When posting php side simple hit this information.
和打印喜欢这一点;
$filedata = $_FILES['photos'];
$f = fopen(time().".txt",'wb');
fwrite($f, print_r($_REQUEST,true));
fclose($f);
[photos] => Array
(
[name] => game.png
[type] => image/png
[tmp_name] => /tmp/phpiQHIXQ
[error] => 0
[size] => 1664972
)
我做什么。
// yourSelectedImage is the bitmap image.
public void SaveImage() throws Exception {
try {
String url = "http://test.com/uploadImage.php";
ByteArrayOutputStream bos = new ByteArrayOutputStream();
yourSelectedImage.compress(CompressFormat.PNG, 75, bos);
byte[] data = bos.toByteArray();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(url);
ByteArrayBody bab = new ByteArrayBody(data, "image/jpeg");
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("photos", bab);
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
System.out.println("Response: " + s);
} catch (Exception e) {
// handle exception here
Log.e(e.getClass().getName(), e.getMessage());
}
}
在PHP服务器,获得这样的
In Php Server, Getting like this
$ Filedata上= $ _FILES ['照片'];
和PHP中进一步函数来完成。
and further function done in php.
但我不张贴到上面的文件,其中文件包括图像名,图像类型,临时的名字,和其他结构类型的多部分采用httppost。
But I am not posting using httppost in to the multipart above type of structure of file, where file includes image name, image type,temp name, and others.
编辑:
我有变化不大位code:,但没有张贴图像文件信息PHP服务器,用户标识确定,但图像文件的信息无法post.In这种情况下,图像保存成功在服务器上,我不能得到的信息图像的PHP断绝。
I have Change Little bit Code:, But failed to post image file information php server, userid ok but image file information could not post.In this case, Image save successfully on server, i could not get information on php sever of image.
下面是code:
公共类UploadImageHttpPost {
public class UploadImageHttpPost {
String testpath="/mnt/sdcard/14111.jpg";
String url = "http://test.com/uploadImage.php";
public static void sendPost(String url, String imagePath,String userId)
throws IOException, ClientProtocolException {
Log.w("TAG", "Start Upload" );
Log.w("TAG", "URL-> "+url );
Log.w("TAG", "Image Path-> "+imagePath );
Log.w("TAG", "User Id-> "+userId );
String responseBody;
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
File file = new File(imagePath);
FileBody encFile = new FileBody(file,"image/jpeg");
Log.w("TAG", "image file-> "+encFile );
entity.addPart("photos", encFile);
entity.addPart("UserId", new StringBody(userId));
HttpPost request = new HttpPost(url);
request.setEntity(entity);
HttpClient client = new DefaultHttpClient();
ResponseHandler<String> responsehandler = new BasicResponseHandler();
responseBody = client.execute(request, responsehandler);
if (responseBody != null && responseBody.length() > 0) {
Log.w("TAG", "Response from image upload->" + responseBody);
}
}
我得到这样的:
I get Like this:
Array
(
[UserId] => 914
)
但在这里以下部分缺失意味着我没有得到,没有任何遗漏code多部分POST方法请查看上面的Java code:
But here below part missing means i am not getting, is there any missing code in multipart post method please check above java code:
[photos] => Array
(
[name] => game.png
[type] => image/png
[tmp_name] => /tmp/phpiQHIXQ
[error] => 0
[size] => 1664972
)
请你纠正我,我如何使用PHP的,上面的图片信息。
Would you please correct me, How to post on php, above information of image.
推荐答案
使用多实体上传图片
File file1 = null;
FileBody bin1 = null;
HttpPost postRequest = new HttpPost(url);
// uri for your image path
if(uri != null)
{
file1 = new File(getPath(uri));
bin1 = new FileBody(file1);
}
MultipartEntity reqEntity = new MultipartEntity();
if(bin1!=null)
reqEntity.addPart("profile_image", bin1);
post.setEntity(reqEntity);
HttpResponse httpResponse = client.execute(post);
int mResponse_code = httpResponse.getStatusLine().getStatusCode();
resEntity = httpResponse.getEntity();
response = EntityUtils.toString(resEntity);
和解析你的JSON响应。这是非常简单的方式。
And Parse your json response. It is very simple way.
这篇关于如何使用多上传图片到PHP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!