php存储图片到服务器,如何将图像上传到Php服务器并存储在phpmyadmin中

我基本上是从Android上传图像并将其上传到php服务器,但在这里我没有得到任何与此代码或图像上传

的联系。

我收到这个错误。

Error in http connection java.net.UnknownHostException: host name但根据我的知识,我给了正确的连接和PHP文件也在正确的领域。

看看我的代码:

UploadImage.java

public class UploadImage extends Activity {

InputStream inputStream;

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

setContentView(R.layout.main);

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon);

ByteArrayOutputStream stream = new ByteArrayOutputStream();

bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.

byte [] byte_arr = stream.toByteArray();

String image_str = Base64.encodeBytes(byte_arr);

ArrayList nameValuePairs = new ArrayList();

nameValuePairs.add(new BasicNameValuePair("image",image_str));

try{

HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost("http://server.com/uploadimage/uploadimage.php");

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpResponse response = httpclient.execute(httppost);

String the_string_response = convertResponseToString(response);

Toast.makeText(UploadImage.this, "Response " + the_string_response, Toast.LENGTH_LONG).show();

}catch(Exception e){

Toast.makeText(UploadImage.this, "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show();

System.out.println("Error in http connection "+e.toString());

}

}

public String convertResponseToString(HttpResponse response) throws IllegalStateException, IOException{

String res = "";

StringBuffer buffer = new StringBuffer();

inputStream = response.getEntity().getContent();

int contentLength = (int) response.getEntity().getContentLength(); //getting content length…..

Toast.makeText(UploadImage.this, "contentLength : " + contentLength, Toast.LENGTH_LONG).show();

if (contentLength < 0){

}

else{

byte[] data = new byte[512];

int len = 0;

try

{

while (-1 != (len = inputStream.read(data)) )

{

buffer.append(new String(data, 0, len)); //converting to string and appending to stringbuffer…..

}

}

catch (IOException e)

{

e.printStackTrace();

}

try

{

inputStream.close(); // closing the stream…..

}

catch (IOException e)

{

e.printStackTrace();

}

res = buffer.toString(); // converting stringbuffer to string…..

Toast.makeText(UploadImage.this, "Result : " + res, Toast.LENGTH_LONG).show();

//System.out.println("Response => " + EntityUtils.toString(response.getEntity()));

}

return res;

}}

PHP代码:

$base=$_REQUEST['image'];

$binary=base64_decode($base);

header('Content-Type: bitmap; charset=utf-8');

$file = fopen('uploaded_image.jpg', 'wb');

fwrite($file, $binary);

fclose($file);

echo 'Image upload complete!!, Please check your php file directory……';?>任何人都知道这个问题?如果任何一个知道如何从PHP文件存储在MySQL数据库并获取viceversa请在这里建议我...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值