将图像上传到android中的服务器 [英] Uploading an image to a server in android

查看:106
本文介绍了将图像上传到android中的服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我我的应用程序正在尝试通过PHP服务器发送图像.问题是PHP程序员说应该将其作为数据发送.以下是我当前正在使用的代码,请帮助我将图像转换为数据并从服务器获得响应.

I my app I am trying to send an image over a PHP server. The problem is the PHP programmers say that it should be sent as an data. Following is the code which I am currently using, please help me to convert the image to a data and get a response from the server.

InputStream is;
    private int serverResponseCode;
    private String serverResponseMessage;
    @Override
    public void onCreate(Bundle icicle) 
    {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        HttpURLConnection connection = null;
        DataOutputStream outputStream = null;
        DataInputStream inputStream = null;

        String pathToOurFile = "/sdcard/siva.PNG";
        Log.e("pathToOurFile",""+pathToOurFile);

        String urlServer = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/upload.php";
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary =  "*****";
        Log.e("URL Server",""+urlServer);

        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1*1024*1024;
        Log.e("maxBufferSize",""+maxBufferSize);

        try
        {
            FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );
            Log.e("FIS",""+fileInputStream);
            URL url = new URL(urlServer);
            connection = (HttpURLConnection) url.openConnection();

    //  Allow Inputs & Outputs
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);

            // Enable POST method
            connection.setRequestMethod("POST");

            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

            outputStream = new DataOutputStream( connection.getOutputStream() );
            outputStream.writeBytes(twoHyphens + boundary + lineEnd);
            outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
            outputStream.writeBytes(lineEnd);

            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];

            // Read file
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            while (bytesRead > 0)
            {
                outputStream.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }

            outputStream.writeBytes(lineEnd);
            outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

            // Responses from the server (code and message)
            Log.e("con",String.valueOf(connection.getDoOutput()));

            serverResponseCode = connection.getResponseCode();
            serverResponseMessage = connection.getResponseMessage();
            Log.e("response",""+serverResponseCode);
            Log.e("serverResponseMessage",""+serverResponseMessage);

            fileInputStream.close();
            outputStream.flush();
            outputStream.close();
        }
        catch (Exception ex)
        {
            Log.e("Exception Handling",""+ex);
        }
      }

推荐答案

最后我找到了答案,我犯了一个小错误....在上面的代码中,我仅更改了以下一行中的一个单词

Atlast i found the answer, i did a small mistake....In the above code i have changed only one word in the following line

outputStream.writeBytes("Content-Disposition:form-data; name = \" uploadedfile \; filename = \""+ pathToOurFile +" \" + lineEnd);

outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);

在上面的行中,主要部分是单词"uploadedfile".这个单词必须由php程序员指定,否则我们发送的文件将不会被替换.

In the above line the main part is the word "uploadedfile". This word must be specified by the php programmer or else the file which we are sending will not be replaced.

请在此处

这篇关于将图像上传到android中的服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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