android 无法将图像上传到服务器

发布于 2024-12-07 13:02:48 字数 1489 浏览 1 评论 0原文

我使用了来自互联网的一些代码来上传图像

public class ActUpload extends Activity {
InputStream is;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
R.drawable.blue);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeBytes(ba);
ArrayList<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",ba1));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://127.0.0.1:80/php/base.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
}
}

,甚至还使用了一些 php 代码复制了一些我使用 wamp 服务器在本地服务器中运行的 php 代码。本地服务器没有任何响应。 这是我的 php 代码。

<?php
$base=$_REQUEST['image'];
echo $base;
// base64 encoded utf-8 string
$binary=base64_decode($base);
// binary, utf-8 bytes
header('Content-Type: bitmap; charset=utf-8');
// print($binary);
//$theFile = base64_decode($image_data);
$file = fopen('test.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo '<img src=test.jpg>';
?>

任何人都可以帮助我吗?提前致谢。

I have used some code from the internet for uploading image

public class ActUpload extends Activity {
InputStream is;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
R.drawable.blue);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeBytes(ba);
ArrayList<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",ba1));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://127.0.0.1:80/php/base.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
}
}

and even took some php code copied some php code which i am running in local sever using wamp server. There is no response in the local server.
and this is my php code.

<?php
$base=$_REQUEST['image'];
echo $base;
// base64 encoded utf-8 string
$binary=base64_decode($base);
// binary, utf-8 bytes
header('Content-Type: bitmap; charset=utf-8');
// print($binary);
//$theFile = base64_decode($image_data);
$file = fopen('test.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo '<img src=test.jpg>';
?>

Can any one help me in this. thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏有森光若流苏 2024-12-14 13:02:48

一个很好的教程,用于将图像上传到服务器

- http:// /coderzheaven.com/2011/04/android-upload-an-image-to-a-server/

编辑:

您只需根据本地服务器更改此设置:

HttpPost httppost = new HttpPost("http://<local ip address>/android/upload_image.php");

// For example,in my code,i used:
// HttpPost httppost = new HttpPost("http://192.168.100.47/android/upload_image.php");

A nice tutorial for uploading image to server-

http://coderzheaven.com/2011/04/android-upload-an-image-to-a-server/

Edit:

You just need to change this according to your local server:

HttpPost httppost = new HttpPost("http://<local ip address>/android/upload_image.php");

// For example,in my code,i used:
// HttpPost httppost = new HttpPost("http://192.168.100.47/android/upload_image.php");
萤火眠眠 2024-12-14 13:02:48

你设法解决问题了吗?当我更改 $base=$_REQUEST['image']; 时,我确实让它工作了=> $base=$_POST['图片'];并注释掉以下行 header('Content-Type: bitmap; charset=utf-8');顺便说一句,是否尝试将代码转换为使用 HttpURLConnection api?

Did you manage to solve the problem? I did get it to work when i changed $base=$_REQUEST['image']; => $base=$_POST['image']; and commented out the following line header('Content-Type: bitmap; charset=utf-8'); Btw, did try to convert the code to use HttpURLConnection api?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文