0

データと画像を送信する前に成功しましたが、2つの異なる手順で行われました

これはデータを送信するための私のコードです

public class HTTPPostData extends AsyncTask {

    @Override
    protected String doInBackground(String... urls) {
        String Result = "";
        byte[] Bresult = null;
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(URL_TO_LOAD);
        try {
            List<NameValuePair> nameValuePairs = LPD;
            post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
            HttpResponse response = client.execute(post);
            StatusLine statusLine = response.getStatusLine();
            if (statusLine.getStatusCode() == HttpURLConnection.HTTP_OK) {
                Bresult = EntityUtils.toByteArray(response.getEntity());
                Result = new String(Bresult, "UTF-8");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (Exception e) {
        }
        return Result;
    }

    protected void onPostExecute(String result) {
        // dismiss the dialog after the file was downloaded
        if (!result.toString().trim().equals("")) {
            RunProcedure.StrParam = result;
            RunProcedure.run();
        }
    }
}

そして、これは写真を転送するための私のコードです

public boolean TransferFileToHttp(String address_to_handle, String file_name) {
    boolean result = false;
    HttpURLConnection connection = null;
    DataOutputStream outputStream = null;
    // DataInputStream inputStream = null;

    String pathToOurFile = file_name;
    String urlServer = address_to_handle;
    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");

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

        fileInputStream.close();
        outputStream.flush();
        outputStream.close();
        result = true;
    } catch (Exception ex) {
        // Exception handling
        result = false;
    }
    return result;
}

転送ファイル手順に参加してデータ手順を投稿し、結果として文字列を取得する方法は?

4

1 に答える 1

1

そうすることは絶対に可能です。ただし、いくつかの追加手順を実行する必要があります。

まず、画像を base 64 文字列に変換する必要があります。このドキュメントを参照してください http://developer.android.com/reference/android/util/Base64.html

これで、文字列を通常の json データとして送信できます。

サーバー側では、base64 文字列を画像に変換するメカニズムが必要になります。しかし、それは些細な作業です。

この方法には、json リクエストのサイズが大きいことや、エンコード/デコードの追加のオーバーヘッドなど、いくつかの欠点があります。

于 2012-12-11T03:23:49.807 に答える