【问题标题】:How do I add name value data to an android HTTP file upload POST request如何将名称值数据添加到 android HTTP 文件上传 POST 请求
【发布时间】:2011-02-15 20:41:46
【问题描述】:

我有一个 android 照片上传脚本,如果我使用 $_GET 读取一些数据,它可以很好地上传,我想使用所有 $_POST。问题是我需要在发布的请求中附加数据以包括基本名称值对以及文件上传。如果我让服务器使用 $_GET 并在 url 字符串中添加额外数据,我可以让它工作,但我想使用 post.有没有办法在文件上传中对服务器可以使用 $_POST 读取的其他帖子数据进行编码。这是函数,请注意 URL 字符串,我正在尝试获取变量 api_key、session_key 和要在 POST 请求中发送的方法。

public void uploadPhoto(FileInputStream fileInputStream, String sessionKey) throws IOException, ClientProtocolException {

    URL connectURL = new URL(API_URL +"?api_key=" +API_KEY+ "&session_key=" + sessionKey + "&method=" + ApiMethods.UPLOAD_PHOTO); //server ignores this data

    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";

    HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();

    // Allow Inputs
    conn.setDoInput(true);

    // Allow Outputs
    conn.setDoOutput(true);

    // Don't use a cached copy.
    conn.setUseCaches(false);

    // Use a post method.
    conn.setRequestMethod("POST");

    conn.setRequestProperty("Connection", "Keep-Alive");

    conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+ boundary);

    DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

    dos.writeBytes(twoHyphens + boundary + lineEnd);
    dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""+ "file.png" + "\"" + lineEnd);
    dos.writeBytes(lineEnd);


    // create a buffer of maximum size

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

    // read file and write it into form...

    int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
    while (bytesRead > 0) {
        dos.write(buffer, 0, bufferSize);
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        /*
         * dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary +
         * twoHyphens + lineEnd);
         */
    }

    // send multipart form data necesssary after file data...
    dos.writeBytes(lineEnd);
    dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
    dos.flush();
    // Log.d(TAG, "  dos5: " + dos.toString());
    InputStream is = conn.getInputStream();
    // retrieve the response from server
    int ch;

    StringBuffer b = new StringBuffer();
    while ((ch = is.read()) != -1) {
        b.append((char) ch);
    }
    String s = b.toString();
    dos.close();

}

编辑 我认为每个添加的参数都需要以下内容

    dos.writeBytes("--" + boundary + lineEnd);
    dos.writeBytes("Content-Disposition: form-data;name=api_key=" + lineEnd + lineEnd + API_KEY);
    dos.writeBytes(lineEnd);
    dos.writeBytes("--" + boundary + "--" + lineEnd);

【问题讨论】:

    标签: php android post file-upload httprequest


    【解决方案1】:

    有一些现有的开放库支持 mime 编码,这可能会为您节省一些时间。我把这里的代码用在了一些我为了发布而破解的东西中:

    http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/

    您可以从这里获取 apache-mime4j 和 httpmime 库:

    http://code.google.com/p/osmdroid/source/browse/trunk/osmdroid-google-wrapper/lib/?r=769

    【讨论】:

    • 注意:通过包含来自 AOSP 而不是 apache 库的多部分类的副本,您可以在 apk 中节省大量空间:android.git.kernel.org/?p=platform/frameworks/…
    • 感谢指点!还没有深入了解 Android 开源项目,很高兴知道资源可用。
    • @nEx.Software 你能给我指一个教程吗?该教程展示了如何在不导入可从 Apache 网站下载的类的情况下导入和使用 MultiPart 类?
    • @codebreaker 这是我不久前整理的东西...github.com/MobiDevelop/android-samples/tree/master/post-image
    • @nEx.Software +1 你知道你应该单独发布它..我想发布一个问题并回答它; MOD应该粘住它:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 2019-10-16
    相关资源
    最近更新 更多