使用HTTP POST将图像上传到Android中的服务器;服务器似乎没有收到图像

时间:2013-08-19 13:33:00

标签: php android image http post

我正在尝试将图像文件从Android的SD存储上传到服务器。我使用的代码将遵循,但是在检查服务器已收到的数据时,服务器验证通信是否成功,但未显示数据已发布。

private class postData extends AsyncTask<String, Integer, Void> {
     protected void doInBackground() {

    HttpURLConnection connection = null;
    DataOutputStream outputStream = null;
    DataInputStream inputStream = null;

    String pathToOurFile = "/sdcard/confirmed.jpg";
    String urlServer = "irrelevant";
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";

    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1*1024*1024;

    try
    {
        FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );

        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");
        System.out.println("posting");
        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)
        int serverResponseCode = connection.getResponseCode();
        String serverResponseMessage = connection.getResponseMessage();

        System.out.println(serverResponseCode+serverResponseMessage);

        fileInputStream.close();
        outputStream.flush();
        outputStream.close();
    }
    catch (Exception ex)
    {
    //Exception handling
    }
    }

    protected void onProgressUpdate() {
        //called when the background task makes any progress
     }

      protected void onPreExecute() {
         //called before doInBackground() is started
     }
     protected void onPostExecute() {
         //called after doInBackground() has finished 
     }

    @Override
    protected Void doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        return null;
    }
} 

我很难提供具体细节,但如果需要,请询问,我会尽力给他们。

0 个答案:

没有答案
相关问题