在Android中编码多部分表单数据

时间:2015-10-27 09:42:42

标签: java android python http-post

Python有一个非常标准的代码,用于发送多部分表单数据,或者更确切地说,编码它:

#!/bin/bash
host=mysftp.dmz
port=22
username=john

/usr/bin/expect <<EOF
set timeout -1
spawn sftp -C -oPort=$port $username@$host
expect "password:"
send "Pas\\\$word\r"
expect "sftp>"
send "get data.txt\r"
expect "sftp>"
send "bye\r"
EOF

我想知道,我怎样才能在Android中对multipart formdata进行编码呢?

我找到了一些解决方案,但是没有一个解决方案与python中的这个解决方案完全相同。

1 个答案:

答案 0 :(得分:1)

我检查过我之前是否使用过此代码,我真的不记得这究竟是如何工作的,但是在这里:

    HttpURLConnection conn = null;
    DataOutputStream dOut = null;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";

      URL url = new URL("http://shrimptalusan.hostei.com/usbong/build-upload.php");

      conn = (HttpURLConnection) url.openConnection();
      conn.setDoInput(true);
      conn.setDoOutput(true);
      conn.setUseCaches(false);

设置多部分表单的开头:

      conn.setRequestMethod("POST");
      conn.setRequestProperty("Connection", "Keep-Alive");
      conn.setRequestProperty("ENCTYPE", "multipart/form-data");
      conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
      conn.setRequestProperty("utreeFile", utreeFilePath);

      dOut = new DataOutputStream(conn.getOutputStream());
      dOut.writeBytes(twoHyphens + boundary + lineEnd);
      dOut.writeBytes("Content-Disposition: form-data; name=\"utreeFile\";filename=\"" + utreeFilePath + "\"" + lineEnd);
      dOut.writeBytes(lineEnd);

      bytesAvailable = utreeFileIn.available();
      bufferSize = Math.min(bytesAvailable, maxBuffersize);
      buffer = new byte[bufferSize];
      bytesRead = utreeFileIn.read(buffer, 0, bufferSize);

将文件读入缓冲区:

      while (bytesRead > 0) {
          progress += bytesRead;
          dOut.write(buffer, 0, bufferSize);
          bytesAvailable = utreeFileIn.available();
          publishProgress((int) ((progress * 100) / (utreeFile.length())));
          bufferSize = Math.min(bytesAvailable, maxBuffersize);
          buffer = new byte[bufferSize]; //TEST
          bytesRead = utreeFileIn.read(buffer, 0, bufferSize);
      }

要包含在多部分实体中的其他参数:

      //PARAMETER FIELD NAME
      dOut.writeBytes(twoHyphens + boundary + lineEnd);
      dOut.writeBytes("Content-Disposition: form-data; name=\"youtubelink\"" + lineEnd);
      dOut.writeBytes(lineEnd);
      dOut.writeBytes(youtubeLink); // mobile_no is String variable
      dOut.writeBytes(lineEnd);
      //PARAMETER END

结束多部分:

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

通过HTTP发送多部分:

      if (conn.getResponseCode() != HttpsURLConnection.HTTP_OK) {
          utreeFileIn.close();
          throw new RuntimeException("Failed : HTTP error code : "
                  + conn.getResponseCode());
      } else {
          BufferedReader br = new BufferedReader(new InputStreamReader(
                  (conn.getInputStream())));
          String line;
          while ((line = br.readLine()) != null) {
              System.out.println(line);
              responseString += line;
          }
      }

关闭文件和连接:

    utreeFileIn.close();
    dOut.flush();
    dOut.close();

希望它能给你一个更好的主意。我不认为我使用过任何外部库。这是我的进口商品:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;