【问题标题】:Uploading an image to a server in android将图像上传到android中的服务器
【发布时间】:2011-08-31 20:57:56
【问题描述】:

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

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);
        }
      }

【问题讨论】:

    标签: java php android image upload


    【解决方案1】:

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

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

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

    请参考here

    【讨论】:

      猜你喜欢
      • 2013-12-17
      • 2014-03-21
      • 2014-03-24
      • 2014-04-19
      • 1970-01-01
      • 2012-02-06
      • 1970-01-01
      • 2011-02-02
      • 2015-03-29
      相关资源
      最近更新 更多