将图像上传到服务器时运行时异常

发布于 2024-12-10 16:37:58 字数 3674 浏览 2 评论 0原文

下面是我在尝试将图像上传到服务器时遇到的运行时异常在此处输入图像描述

我正在尝试使用我的本地服务器(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……';

?>

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

The below is the runtime exeception which i got while am trying to upload a image to the server.
enter image description here

And am trying to upload a image using my local server(WAMP) and my android code is

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;

        }

}

and this is my php code which i got from internet .

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

?>

Can any one help me in this to solve my problem. thanks in advance

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

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

发布评论

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

评论(2

温折酒 2024-12-17 16:37:58

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

这可能会解决您的问题。

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

You have to create one additional directory in c:\ then provide read, write and execute permission on it.
In your php code please write the absolute path of the storage location.

This may solve your problem.

Remember to run the IIS server in Administrator mode.

2024-12-17 16:37:58

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

Inetpub is created by the SYSTEM user. This means that you aren't allowed to make modifications to it, or any of its subdirectories, without admin permissions. Try changing the permissions of the inetpub folder so anyone can modify the files. Right-click -> Properties -> ... I forget what to do after that. (I'm in Linux right now). If that doesn't work, make sure you are running IIS as an Administrator.

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