如何上传图片到PHP服务器和存储在phpMyAdmin [英] How to Upload images to Php server and store in phpmyadmin

查看:244
本文介绍了如何上传图片到PHP服务器和存储在phpMyAdmin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上特林从Android的上传图片,并将其上传到PHP服务器,但在这里我没有得到这个code或图片上传
的任何连接。 我得到这个错误。

I am basically tring to upload image from android and upload it to php server but here i'm not getting any connection with this code or image upload
. I'm getting this error .

Error in http connection java.net.UnknownHostException: host name

但按我的知​​识,我给予正确的连接和php文件还以正确的领域。 看我的code: UploadImage.java

but as per my knowledge i given correct connection and php file also in correct domain. Look at my code : 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<NameValuePair> nameValuePairs = new  ArrayList<NameValuePair>();

        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的code:

<?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……';?>

任何一个已知的这个问题?如果任何人知道如何在MySQL数据库中存储的PHP文件,并获取反之亦然请建议我在这里...

Any one known this issue ? if any one know how to store in mysql database from php file and fetch viceversa please suggest me here...

推荐答案

现在的问题是很清楚的......

The problem is very clear ...

Error in http connection java.net.UnknownHostException: host name

意味着使用您提供的主机名 HttpPost 无法建立连接 - 因为您提供的主机名不知道

means that the HttpPost cannot make a connection using the hostname you supplied - because the hostname you supplied isn't known.

如果你从该行采取的主机名:

If you take the hostname from this line :

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

和把它在同一设备会发生什么上的浏览器...我建议你将得到一个错误说无法连接到主机。如果一切正常的话,我建议你检查下面一行是在你的清单:

and put it in a browser on the same device what happens ... i suggest you will get an error saying unable to connect to host. If this works then i suggest you check the following line is in your manifest :

<uses-permission android:name="android.permission.INTERNET" />

还要确保PHP文件中包含下面的头,如果你使用的是 JPEG

header('Content-Type: image/jpg');

这篇关于如何上传图片到PHP服务器和存储在phpMyAdmin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆