将图像上载到服务器时出现运行时异常

时间:2011-10-19 04:49:51

标签: php android image-uploading runtimeexception

以下是我在尝试将图像上传到服务器时获得的runtime execeptionenter image description here

我正在尝试使用我的本地服务器(WAMP)上传图像,而我的Android代码是

Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/Sunset.jpg");

     //   Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.background1); 
            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://192.168.1.49/android/upload_image.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  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 string buffer to string

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

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

             }
  return res;

        }

}

这是我从互联网上获取的PHP代码。

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

?>

任何人都可以帮助我解决我的问题。提前谢谢

2 个答案:

答案 0 :(得分:0)

Inetpub由SYSTEM用户创建。这意味着您不能在没有管理员权限的情况下对其或其任何子目录进行修改。尝试更改inetpub文件夹的权限,以便任何人都可以修改文件。右键单击 - &gt;属性 - &gt; ......我忘记了之后要做什么。 (我现在在Linux)。如果这不起作用,请确保以管理员身份运行IIS。

答案 1 :(得分:0)

您必须在c:\中创建一个额外的目录,然后为其提供读取,写入和执行权限。 在你的PHP代码中,请写下存储位置的绝对路径。

这可以解决您的问题。

请记住以管理员模式运行IIS服务器。