【问题标题】:Java/Android HttpURLConnection setChunkedStreamingMode not working with all PHP serversJava/Android HttpURLConnection setChunkedStreamingMode 不适用于所有 PHP 服务器
【发布时间】:2012-08-28 07:27:01
【问题描述】:

我花了最后一天左右的时间试图调试这个问题,但我没有想法。基本上我有一个 Android 应用程序将一些数据发布到 PHP/Apache Web 服务器。当我将它指向我的本地测试服务器时,这段代码似乎工作正常。当我将它指向我的生产服务器时,它似乎也可以正常工作,但只有当我注释掉 conn.setChunkedStreamingMode(maxBufferSize); 行时。启用该行后,该帖子仅适用于我的本地测试服务器,但在发布到生产服务器时,PHP $_FILES 数组为空。我尝试将许多值传递给 setChunkedStreamingMode(包括 0 和 1024),但这些似乎都不能解决问题。

此时我假设问题与配置生产服务器的 PHP 的方式有关,但据我所知,服务器上的所有重要参数都与我的测试实例上的相同。此外,它们都运行相同版本的 Apache 和 PHP。我的生产服务器由 Bluehost 运行。

这是我用来上传的 Java 代码:

HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "***************************************************";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 212144; // 1024*1024 = 1MB.  212144 is a quarter MB.
FileInputStream fileInputStream = null;
try
{
  // ------------------ CLIENT REQUEST
  fileInputStream = new FileInputStream(new File(existingFileWithFullPath));
  // open a URL connection to the Servlet
  URL url = new URL(BACKUP_POST_URL);
  // Open a HTTP connection to the URL
  conn = (HttpURLConnection) url.openConnection();
  // Allow Inputs
  conn.setDoInput(true);
  // Allow Outputs
  conn.setDoOutput(true);
  // Send in chunks (to avoid out of memory error)
  conn.setChunkedStreamingMode(maxBufferSize);
  // 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);
  conn.setReadTimeout(200000); // 200 seconds...
  dos = new DataOutputStream(conn.getOutputStream());
  dos.writeBytes(twoHyphens + boundary + lineEnd);
  dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
      + fileName + "\"" + lineEnd);
  dos.writeBytes(lineEnd);
  // create a buffer of maximum size
  bytesAvailable = fileInputStream.available();
  bufferSize = Math.min(bytesAvailable, maxBufferSize);
  buffer = new byte[bufferSize];
  // read file and write it into form...
  bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  while (bytesRead > 0)
  {
    try {
      dos.write(buffer, 0, bufferSize);          
    } catch (OutOfMemoryError oome) {
      Log.e(CommonStatic.LOG_NAME, "Out of memory error caught...");
      oome.printStackTrace();
      fileInputStream.close();
      throw new Exception("Out Of Memory!");
    }
    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
  }
  // send multipart form data necesssary after file data...
  dos.writeBytes(lineEnd);
  dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
  fileInputStream.close();
  dos.flush();
  dos.close();

  // close streams
  Log.d(CommonStatic.LOG_NAME, "Backup file written to server successfully...");
}
catch (Exception ex)
{
  Log.e(CommonStatic.LOG_NAME, "Backup File Upload Error: " + ex.getMessage(), ex);
  throw new Exception (c.getString(R.string.SAVE_TO_CLOUD_ERROR));
}

这是我在另一端用来接收的 PHP 代码:

$target = "userfiles/"; 
$target = $target . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target)) 
{ 
  echo "SUCCESS";
} 
else 
{ 
  echo "FAIL";
}

我在脚本的最开头添加了print_r($_FILES);,以确定 $_FILES 在生产实例上为空,但在测试实例上不为空。任何想法将不胜感激。

【问题讨论】:

  • 您的生产配置中的某些内容阻止了此模式的工作
  • 有什么建议吗?
  • 你解决了这个问题吗..?

标签: java php android


【解决方案1】:

很遗憾,我无法找到直接解决此问题的方法,但我找到了解决方法。我没有使用conn.setChunkedStreamingMode,而是使用了conn.setFixedLengthStreamingMode,我之前尝试过但没有成功。让conn.setFixedLengthStreamingMode 工作的关键是将您尝试发送的数据的全长(在本例中为文件)以及标头的长度传递给它。幸运的是,标题长度通常在这样的代码中是固定的,所以一旦你弄清楚你的标题有多大(记住文件名之类的东西,它在 Content-Disposition 下的标题中发送也是可变的)你可以把它在作为一个固定的数字。为了弄清楚我的标题长度,我首先运行了代码,但没有为标题指定任何长度。我收到的错误消息给了我发送字节数的预期值和实际值,这使我能够计算标头长度。

【讨论】:

  • 你能把你用来从conn中找出标题的代码放上来吗?
  • @BlueScreenOfTOM:通过使用 conn.setFixedLengthStreamingMode,我收到大型视频文件的 sockettimeout 错误。我看到了你的代码,上面写着 200 秒超时。那么您在什么基础上设置 200 秒?但是通过使用 conn.setFixedLengthStreamingMode 我能够同时消除内存不足错误和套接字超时错误。
  • 通过这样做,您是在告诉整个文件要作为一个块上传,这通常会违背此方法的目的
猜你喜欢
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多